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;
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 got_ref_close(ref);
377 if (error)
378 goto done;
379 } else if (qs->commit != NULL) {
380 error = got_ref_open(&ref, repo, qs->commit, 0);
381 if (error == NULL) {
382 error = got_ref_resolve(&id, repo, ref);
383 if (error)
384 goto done;
385 error = got_object_get_type(&obj_type, repo, id);
386 got_ref_close(ref);
387 if (error)
388 goto done;
389 if (obj_type == GOT_OBJ_TYPE_TAG) {
390 struct got_tag_object *tag;
391 error = got_object_open_as_tag(&tag, repo, id);
392 if (error)
393 goto done;
394 if (got_object_tag_get_object_type(tag) !=
395 GOT_OBJ_TYPE_COMMIT) {
396 got_object_tag_close(tag);
397 error = got_error(GOT_ERR_OBJ_TYPE);
398 goto done;
400 free(id);
401 id = got_object_id_dup(
402 got_object_tag_get_object_id(tag));
403 if (id == NULL)
404 error = got_error_from_errno(
405 "got_object_id_dup");
406 got_object_tag_close(tag);
407 if (error)
408 goto done;
409 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
410 error = got_error(GOT_ERR_OBJ_TYPE);
411 goto done;
414 error = got_repo_match_object_id_prefix(&id, qs->commit,
415 GOT_OBJ_TYPE_COMMIT, repo);
416 if (error)
417 goto done;
420 error = got_repo_map_path(&in_repo_path, repo, repo_path);
421 if (error)
422 goto done;
424 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
425 if (error)
426 goto done;
428 if (qs->file != NULL && strlen(qs->file) > 0) {
429 error = got_commit_graph_open(&graph, file_path, 0);
430 if (error)
431 goto done;
432 } else {
433 error = got_commit_graph_open(&graph, in_repo_path, 0);
434 if (error)
435 goto done;
438 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
439 if (error)
440 goto done;
442 for (;;) {
443 if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
444 commit_found == 0 && repo_commit->commit_id != NULL) {
445 t->prev_id = strdup(repo_commit->commit_id);
446 if (t->prev_id == NULL) {
447 error = got_error_from_errno("strdup");
448 goto done;
452 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
453 NULL);
454 if (error) {
455 if (error->code == GOT_ERR_ITER_COMPLETED)
456 error = NULL;
457 goto done;
459 if (id == NULL)
460 goto done;
462 error = got_object_open_as_commit(&commit, repo, id);
463 if (error)
464 goto done;
466 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
467 NULL);
468 if (error)
469 goto done;
471 error = got_get_repo_commit(c, repo_commit, commit,
472 &refs, id);
473 if (error)
474 goto done;
476 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
477 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
478 commit_found = 1;
479 else if (qs->file != NULL && strlen(qs->file) > 0 &&
480 qs->page == 0)
481 commit_found = 1;
482 else {
483 limit_chk++;
484 free(id);
485 id = NULL;
486 continue;
490 struct repo_commit *new_repo_commit = NULL;
491 error = got_init_repo_commit(&new_repo_commit);
492 if (error)
493 goto done;
495 TAILQ_INSERT_TAIL(&t->repo_commits, new_repo_commit, entry);
497 error = got_get_repo_commit(c, new_repo_commit, commit,
498 &refs, id);
499 if (error)
500 goto done;
502 free(id);
503 id = NULL;
505 if (limit == 1 && chk_multi == 0 &&
506 srv->max_commits_display != 1)
507 commit_found = 1;
508 else {
509 chk_multi = 1;
511 /*
512 * check for one more commit before breaking,
513 * so we know whether to navigate through briefs
514 * commits and summary
515 */
516 if (chk_next && (qs->action == BRIEFS ||
517 qs->action == COMMITS || qs->action == SUMMARY)) {
518 t->next_id = strdup(new_repo_commit->commit_id);
519 if (t->next_id == NULL) {
520 error = got_error_from_errno("strdup");
521 goto done;
523 if (commit) {
524 got_object_commit_close(commit);
525 commit = NULL;
527 TAILQ_REMOVE(&t->repo_commits, new_repo_commit,
528 entry);
529 gotweb_free_repo_commit(new_repo_commit);
530 goto done;
533 got_ref_list_free(&refs);
534 if (error || (limit && --limit == 0)) {
535 if (commit_found || (qs->file != NULL &&
536 strlen(qs->file) > 0))
537 if (chk_multi == 0)
538 break;
539 chk_next = 1;
541 if (commit) {
542 got_object_commit_close(commit);
543 commit = NULL;
546 done:
547 gotweb_free_repo_commit(repo_commit);
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(file_path);
554 free(repo_path);
555 free(id);
556 return error;
559 const struct got_error *
560 got_get_repo_tags(struct request *c, int limit)
562 const struct got_error *error = NULL;
563 struct got_object_id *id = NULL;
564 struct got_commit_object *commit = NULL;
565 struct got_reflist_head refs;
566 struct got_reference *ref;
567 struct got_reflist_entry *re;
568 struct server *srv = c->srv;
569 struct transport *t = c->t;
570 struct got_repository *repo = t->repo;
571 struct querystring *qs = t->qs;
572 struct repo_dir *repo_dir = t->repo_dir;
573 struct got_tag_object *tag = NULL;
574 struct repo_tag *rt = NULL, *trt = NULL;
575 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
576 char *tag_commit = NULL, *tag_commit0 = NULL;
577 char *commit_msg = NULL, *commit_msg0 = NULL;
578 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
580 TAILQ_INIT(&refs);
582 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
583 repo_dir->name) == -1)
584 return got_error_from_errno("asprintf");
586 if (qs->commit == NULL && qs->action == TAGS) {
587 error = got_ref_open(&ref, repo, qs->headref, 0);
588 if (error)
589 goto err;
590 error = got_ref_resolve(&id, repo, ref);
591 got_ref_close(ref);
592 if (error)
593 goto err;
594 } else if (qs->commit == NULL && qs->action == TAG) {
595 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
596 goto err;
597 } else {
598 error = got_repo_match_object_id_prefix(&id, qs->commit,
599 GOT_OBJ_TYPE_COMMIT, repo);
600 if (error)
601 goto err;
604 if (qs->action != SUMMARY && qs->action != TAGS) {
605 error = got_object_open_as_commit(&commit, repo, id);
606 if (error)
607 goto err;
608 error = got_object_commit_get_logmsg(&commit_msg0, commit);
609 if (error)
610 goto err;
611 if (commit) {
612 got_object_commit_close(commit);
613 commit = NULL;
617 error = got_repo_map_path(&in_repo_path, repo, repo_path);
618 if (error)
619 goto err;
621 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
622 repo);
623 if (error)
624 goto err;
626 if (limit == 1)
627 chk_multi = 0;
629 /*
630 * XXX: again, see previous message about caching
631 */
633 TAILQ_FOREACH(re, &refs, entry) {
634 struct repo_tag *new_repo_tag = NULL;
635 error = got_init_repo_tag(&new_repo_tag);
636 if (error)
637 goto err;
639 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
641 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
642 if (new_repo_tag->tag_name == NULL) {
643 error = got_error_from_errno("strdup");
644 goto err;
647 free(id);
648 id = NULL;
650 free(id_str);
651 id_str = NULL;
653 error = got_ref_resolve(&id, repo, re->ref);
654 if (error)
655 goto done;
657 error = got_object_open_as_tag(&tag, repo, id);
658 if (error) {
659 if (error->code != GOT_ERR_OBJ_TYPE)
660 goto done;
661 /* "lightweight" tag */
662 error = got_object_open_as_commit(&commit, repo, id);
663 if (error)
664 goto done;
665 new_repo_tag->tagger =
666 strdup(got_object_commit_get_committer(commit));
667 if (new_repo_tag->tagger == NULL) {
668 error = got_error_from_errno("strdup");
669 goto err;
671 new_repo_tag->tagger_time =
672 got_object_commit_get_committer_time(commit);
673 error = got_object_id_str(&id_str, id);
674 if (error)
675 goto err;
676 } else {
677 new_repo_tag->tagger =
678 strdup(got_object_tag_get_tagger(tag));
679 if (new_repo_tag->tagger == NULL) {
680 error = got_error_from_errno("strdup");
681 goto err;
683 new_repo_tag->tagger_time =
684 got_object_tag_get_tagger_time(tag);
685 error = got_object_id_str(&id_str,
686 got_object_tag_get_object_id(tag));
687 if (error)
688 goto err;
690 got_object_tag_close(tag);
691 tag = NULL;
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 free(commit_msg0);
761 goto err;
763 free(commit_msg0);
766 if (limit && --limit == 0) {
767 if (chk_multi == 0)
768 break;
769 chk_next = 1;
773 done:
774 /*
775 * we have tailq populated, so find previous commit id
776 * for navigation through briefs and commits
777 */
778 if (t->tag_count == 0) {
779 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
780 TAILQ_REMOVE(&t->repo_tags, rt, entry);
781 gotweb_free_repo_tag(rt);
784 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
785 commit_found = 0;
786 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
787 entry) {
788 if (commit_found == 0 && rt->commit_id != NULL &&
789 strcmp(qs->commit, rt->commit_id) != 0) {
790 continue;
791 } else
792 commit_found = 1;
793 if (c_cnt == srv->max_commits_display ||
794 rt == TAILQ_FIRST(&t->repo_tags)) {
795 t->prev_id = strdup(rt->commit_id);
796 if (t->prev_id == NULL)
797 error = got_error_from_errno("strdup");
798 break;
800 c_cnt++;
803 err:
804 if (commit)
805 got_object_commit_close(commit);
806 if (tag)
807 got_object_tag_close(tag);
808 got_ref_list_free(&refs);
809 free(in_repo_path);
810 free(repo_path);
811 free(id);
812 free(id_str);
813 return error;
816 const struct got_error *
817 got_output_repo_tree(struct request *c)
819 const struct got_error *error = NULL;
820 struct transport *t = c->t;
821 struct got_commit_object *commit = NULL;
822 struct got_repository *repo = t->repo;
823 struct querystring *qs = t->qs;
824 struct repo_commit *rc = NULL;
825 struct got_object_id *tree_id = NULL, *commit_id = NULL;
826 struct got_reflist_head refs;
827 struct got_tree_object *tree = NULL;
828 struct repo_dir *repo_dir = t->repo_dir;
829 const char *name, *index_page_str, *folder;
830 char *id_str = NULL, *escaped_name = NULL;
831 char *path = NULL, *in_repo_path = NULL, *modestr = NULL;
832 int nentries, i, r;
834 TAILQ_INIT(&refs);
836 rc = TAILQ_FIRST(&t->repo_commits);
838 if (qs->folder != NULL) {
839 path = strdup(qs->folder);
840 if (path == NULL) {
841 error = got_error_from_errno("strdup");
842 goto done;
844 } else {
845 error = got_repo_map_path(&in_repo_path, repo, repo_dir->path);
846 if (error)
847 goto done;
848 free(path);
849 path = in_repo_path;
852 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
853 GOT_OBJ_TYPE_COMMIT, &refs, repo);
854 if (error)
855 goto done;
857 error = got_object_open_as_commit(&commit, repo, commit_id);
858 if (error)
859 goto done;
861 error = got_object_id_by_path(&tree_id, repo, commit, path);
862 if (error)
863 goto done;
865 error = got_object_open_as_tree(&tree, repo, tree_id);
866 if (error)
867 goto done;
869 nentries = got_object_tree_get_nentries(tree);
871 index_page_str = qs->index_page_str ? qs->index_page_str : "";
872 folder = qs->folder ? qs->folder : "";
874 for (i = 0; i < nentries; i++) {
875 struct got_tree_entry *te;
876 mode_t mode;
878 te = got_object_tree_get_entry(tree, i);
880 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
881 if (error)
882 goto done;
884 modestr = strdup("");
885 if (modestr == NULL) {
886 error = got_error_from_errno("strdup");
887 goto done;
889 mode = got_tree_entry_get_mode(te);
890 if (got_object_tree_entry_is_submodule(te)) {
891 free(modestr);
892 modestr = strdup("$");
893 if (modestr == NULL) {
894 error = got_error_from_errno("strdup");
895 goto done;
897 } else if (S_ISLNK(mode)) {
898 free(modestr);
899 modestr = strdup("@");
900 if (modestr == NULL) {
901 error = got_error_from_errno("strdup");
902 goto done;
904 } else if (S_ISDIR(mode)) {
905 free(modestr);
906 modestr = strdup("/");
907 if (modestr == NULL) {
908 error = got_error_from_errno("strdup");
909 goto done;
911 } else if (mode & S_IXUSR) {
912 free(modestr);
913 modestr = strdup("*");
914 if (modestr == NULL) {
915 error = got_error_from_errno("strdup");
916 goto done;
920 name = got_tree_entry_get_name(te);
921 error = gotweb_escape_html(&escaped_name, name);
922 if (error)
923 goto done;
925 if (S_ISDIR(mode)) {
926 r = fcgi_printf(c,
927 "<div class='tree_wrapper'>\n"
928 "<div class='tree_line'>"
929 "<a class='diff_directory' "
930 "href='?index_page=%s&path=%s&action=tree"
931 "&commit=%s&folder=%s/%s'>%s%s</a>"
932 "</div>\n" /* .tree_line */
933 "<div class='tree_line_blank'>&nbsp;</div>\n"
934 "</div>\n", /* .tree_wrapper */
935 index_page_str, qs->path, rc->commit_id,
936 folder, name, escaped_name, modestr);
937 if (r == -1)
938 goto done;
939 } else {
940 r = fcgi_printf(c,
941 "<div class='tree_wrapper'>\n"
942 "<div class='tree_line'>"
943 "<a href='?index_page=%s&path=%s&action=blob"
944 "&commit=%s&folder=%s&file=%s'>%s%s</a>"
945 "</div>\n" /* .tree_line */
946 "<div class='tree_line_blank'>"
947 "<a href='?index_page=%s&path=%s&action=commits"
948 "&commit=%s&folder=%s&file=%s'>commits</a>\n"
949 " | \n"
950 "<a href='?index_page=%s&path=%s&action=blame"
951 "&commit=%s&folder=%s&file=%s'>blame</a>\n"
952 "</div>\n" /* .tree_line_blank */
953 "</div>\n", /* .tree_wrapper */
954 index_page_str, qs->path, rc->commit_id,
955 folder, name, escaped_name, modestr,
956 index_page_str, qs->path, rc->commit_id,
957 folder, name,
958 index_page_str, qs->path, rc->commit_id,
959 folder, name);
960 if (r == -1)
961 goto done;
963 free(id_str);
964 id_str = NULL;
965 free(modestr);
966 modestr = NULL;
967 free(escaped_name);
968 escaped_name = NULL;
970 done:
971 free(escaped_name);
972 free(id_str);
973 free(modestr);
974 free(path);
975 got_ref_list_free(&refs);
976 if (commit)
977 got_object_commit_close(commit);
978 free(commit_id);
979 free(tree_id);
980 return error;
983 const struct got_error *
984 got_output_file_blob(struct request *c)
986 const struct got_error *error = NULL;
987 struct transport *t = c->t;
988 struct got_repository *repo = t->repo;
989 struct querystring *qs = c->t->qs;
990 struct got_commit_object *commit = NULL;
991 struct got_object_id *commit_id = NULL;
992 struct got_reflist_head refs;
993 struct got_blob_object *blob = NULL;
994 char *path = NULL, *in_repo_path = NULL;
995 int obj_type, set_mime = 0, fd = -1;
996 size_t len, hdrlen;
997 const uint8_t *buf;
999 TAILQ_INIT(&refs);
1001 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1002 qs->folder ? "/" : "", qs->file) == -1) {
1003 error = got_error_from_errno("asprintf");
1004 goto done;
1007 error = got_repo_map_path(&in_repo_path, repo, path);
1008 if (error)
1009 goto done;
1011 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1012 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1013 if (error)
1014 goto done;
1016 error = got_object_open_as_commit(&commit, repo, commit_id);
1017 if (error)
1018 goto done;
1020 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1021 if (error)
1022 goto done;
1024 if (commit_id == NULL) {
1025 error = got_error(GOT_ERR_NO_OBJ);
1026 goto done;
1029 error = got_object_get_type(&obj_type, repo, commit_id);
1030 if (error)
1031 goto done;
1033 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1034 error = got_error(GOT_ERR_OBJ_TYPE);
1035 goto done;
1038 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1039 if (error)
1040 goto done;
1042 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1043 if (error)
1044 goto done;
1045 hdrlen = got_object_blob_get_hdrlen(blob);
1046 do {
1047 error = got_object_blob_read_block(&len, blob);
1048 if (error)
1049 goto done;
1050 buf = got_object_blob_get_read_buf(blob);
1053 * Skip blob object header first time around,
1054 * which also contains a zero byte.
1056 buf += hdrlen;
1057 if (set_mime == 0) {
1058 if (isbinary(buf, len - hdrlen)) {
1059 error = gotweb_render_content_type_file(c,
1060 "application/octet-stream",
1061 qs->file);
1062 if (error) {
1063 log_warnx("%s: %s", __func__,
1064 error->msg);
1065 goto done;
1067 } else {
1068 error = gotweb_render_content_type(c,
1069 "text/plain");
1070 if (error) {
1071 log_warnx("%s: %s", __func__,
1072 error->msg);
1073 goto done;
1077 set_mime = 1;
1078 fcgi_gen_binary_response(c, buf, len - hdrlen);
1080 hdrlen = 0;
1081 } while (len != 0);
1082 done:
1083 if (commit)
1084 got_object_commit_close(commit);
1085 if (fd != -1 && close(fd) == -1 && error == NULL)
1086 error = got_error_from_errno("close");
1087 if (blob)
1088 got_object_blob_close(blob);
1089 free(in_repo_path);
1090 free(commit_id);
1091 free(path);
1092 return error;
1095 struct blame_line {
1096 int annotated;
1097 char *id_str;
1098 char *committer;
1099 char datebuf[11]; /* YYYY-MM-DD + NUL */
1102 struct blame_cb_args {
1103 struct blame_line *lines;
1104 int nlines;
1105 int nlines_prec;
1106 int lineno_cur;
1107 off_t *line_offsets;
1108 FILE *f;
1109 struct got_repository *repo;
1110 struct request *c;
1113 static const struct got_error *
1114 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1115 struct got_commit_object *commit, struct got_object_id *id)
1117 const struct got_error *err = NULL;
1118 struct blame_cb_args *a = arg;
1119 struct blame_line *bline;
1120 struct request *c = a->c;
1121 struct transport *t = c->t;
1122 struct querystring *qs = t->qs;
1123 struct repo_dir *repo_dir = t->repo_dir;
1124 const char *index_page_str;
1125 char *line = NULL, *eline = NULL;
1126 size_t linesize = 0;
1127 off_t offset;
1128 struct tm tm;
1129 time_t committer_time;
1130 int r;
1132 if (nlines != a->nlines ||
1133 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1134 return got_error(GOT_ERR_RANGE);
1136 if (lineno == -1)
1137 return NULL; /* no change in this commit */
1139 /* Annotate this line. */
1140 bline = &a->lines[lineno - 1];
1141 if (bline->annotated)
1142 return NULL;
1143 err = got_object_id_str(&bline->id_str, id);
1144 if (err)
1145 return err;
1147 bline->committer = strdup(got_object_commit_get_committer(commit));
1148 if (bline->committer == NULL) {
1149 err = got_error_from_errno("strdup");
1150 goto done;
1153 committer_time = got_object_commit_get_committer_time(commit);
1154 if (gmtime_r(&committer_time, &tm) == NULL)
1155 return got_error_from_errno("gmtime_r");
1156 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1157 &tm) == 0) {
1158 err = got_error(GOT_ERR_NO_SPACE);
1159 goto done;
1161 bline->annotated = 1;
1163 /* Print lines annotated so far. */
1164 bline = &a->lines[a->lineno_cur - 1];
1165 if (!bline->annotated)
1166 goto done;
1168 offset = a->line_offsets[a->lineno_cur - 1];
1169 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1170 err = got_error_from_errno("fseeko");
1171 goto done;
1174 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1176 while (a->lineno_cur <= a->nlines && bline->annotated) {
1177 char *smallerthan, *at, *nl, *committer;
1178 size_t len;
1180 if (getline(&line, &linesize, a->f) == -1) {
1181 if (ferror(a->f))
1182 err = got_error_from_errno("getline");
1183 break;
1186 committer = bline->committer;
1187 smallerthan = strchr(committer, '<');
1188 if (smallerthan && smallerthan[1] != '\0')
1189 committer = smallerthan + 1;
1190 at = strchr(committer, '@');
1191 if (at)
1192 *at = '\0';
1193 len = strlen(committer);
1194 if (len >= 9)
1195 committer[8] = '\0';
1197 nl = strchr(line, '\n');
1198 if (nl)
1199 *nl = '\0';
1201 err = gotweb_escape_html(&eline, line);
1202 if (err)
1203 goto done;
1205 r = fcgi_printf(c, "<div class='blame_wrapper'>"
1206 "<div class='blame_number'>%.*d</div>"
1207 "<div class='blame_hash'>"
1208 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1209 "%.8s</a>"
1210 "</div>"
1211 "<div class='blame_date'>%s</div>"
1212 "<div class='blame_author'>%s</div>"
1213 "<div class='blame_code'>%s</div>"
1214 "</div>", /* .blame_wrapper */
1215 a->nlines_prec, a->lineno_cur,
1216 index_page_str, repo_dir->name, bline->id_str,
1217 bline->id_str,
1218 bline->datebuf,
1219 committer,
1220 eline);
1221 if (r == -1)
1222 goto done;
1224 a->lineno_cur++;
1225 bline = &a->lines[a->lineno_cur - 1];
1227 done:
1228 free(line);
1229 free(eline);
1230 return err;
1233 const struct got_error *
1234 got_output_file_blame(struct request *c)
1236 const struct got_error *error = NULL;
1237 struct transport *t = c->t;
1238 struct got_repository *repo = t->repo;
1239 struct querystring *qs = c->t->qs;
1240 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1241 struct got_commit_object *commit = NULL;
1242 struct got_reflist_head refs;
1243 struct got_blob_object *blob = NULL;
1244 char *path = NULL, *in_repo_path = NULL;
1245 struct blame_cb_args bca;
1246 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1247 int fd6 = -1;
1248 off_t filesize;
1249 FILE *f1 = NULL, *f2 = NULL;
1251 TAILQ_INIT(&refs);
1252 bca.f = NULL;
1253 bca.lines = NULL;
1255 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1256 qs->folder ? "/" : "", qs->file) == -1) {
1257 error = got_error_from_errno("asprintf");
1258 goto done;
1261 error = got_repo_map_path(&in_repo_path, repo, path);
1262 if (error)
1263 goto done;
1265 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1266 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1267 if (error)
1268 goto done;
1270 error = got_object_open_as_commit(&commit, repo, commit_id);
1271 if (error)
1272 goto done;
1274 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1275 if (error)
1276 goto done;
1278 if (commit_id == NULL) {
1279 error = got_error(GOT_ERR_NO_OBJ);
1280 goto done;
1283 error = got_object_get_type(&obj_type, repo, obj_id);
1284 if (error)
1285 goto done;
1287 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1288 error = got_error(GOT_ERR_OBJ_TYPE);
1289 goto done;
1292 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1293 if (error)
1294 goto done;
1296 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1297 if (error)
1298 goto done;
1300 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1301 if (error)
1302 goto done;
1304 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1305 &bca.line_offsets, bca.f, blob);
1306 if (error || bca.nlines == 0)
1307 goto done;
1309 /* Don't include \n at EOF in the blame line count. */
1310 if (bca.line_offsets[bca.nlines - 1] == filesize)
1311 bca.nlines--;
1313 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1314 if (bca.lines == NULL) {
1315 error = got_error_from_errno("calloc");
1316 goto done;
1318 bca.lineno_cur = 1;
1319 bca.nlines_prec = 0;
1320 i = bca.nlines;
1321 while (i > 0) {
1322 i /= 10;
1323 bca.nlines_prec++;
1325 bca.repo = repo;
1326 bca.c = c;
1328 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1329 if (error)
1330 goto done;
1332 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1333 if (error)
1334 goto done;
1336 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1337 if (error)
1338 goto done;
1340 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1341 if (error)
1342 goto done;
1344 error = got_blame(in_repo_path, commit_id, repo,
1345 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1346 fd3, fd4, f1, f2);
1348 if (blob) {
1349 free(bca.line_offsets);
1350 for (i = 0; i < bca.nlines; i++) {
1351 struct blame_line *bline = &bca.lines[i];
1352 free(bline->id_str);
1353 free(bline->committer);
1356 done:
1357 free(bca.lines);
1358 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1359 error = got_error_from_errno("close");
1360 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1361 error = got_error_from_errno("close");
1362 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1363 error = got_error_from_errno("close");
1364 if (bca.f) {
1365 const struct got_error *bca_err =
1366 got_gotweb_flushfile(bca.f, fd1);
1367 if (error == NULL)
1368 error = bca_err;
1370 if (f1) {
1371 const struct got_error *f1_err =
1372 got_gotweb_flushfile(f1, fd5);
1373 if (error == NULL)
1374 error = f1_err;
1376 if (f2) {
1377 const struct got_error *f2_err =
1378 got_gotweb_flushfile(f2, fd6);
1379 if (error == NULL)
1380 error = f2_err;
1382 if (commit)
1383 got_object_commit_close(commit);
1384 if (blob)
1385 got_object_blob_close(blob);
1386 free(in_repo_path);
1387 free(commit_id);
1388 free(path);
1389 return error;
1392 const struct got_error *
1393 got_output_repo_diff(struct request *c)
1395 const struct got_error *error = NULL;
1396 struct transport *t = c->t;
1397 struct got_repository *repo = t->repo;
1398 struct repo_commit *rc = NULL;
1399 struct got_object_id *id1 = NULL, *id2 = NULL;
1400 struct got_reflist_head refs;
1401 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1402 char *label1 = NULL, *label2 = NULL, *line = NULL;
1403 char *newline, *eline = NULL, *color = NULL;
1404 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1405 size_t linesize = 0;
1406 ssize_t linelen;
1407 int wrlen = 0;
1409 TAILQ_INIT(&refs);
1411 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1412 if (error)
1413 return error;
1415 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1416 if (error)
1417 return error;
1419 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1420 if (error)
1421 return error;
1423 rc = TAILQ_FIRST(&t->repo_commits);
1425 if (rc->parent_id != NULL &&
1426 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1427 error = got_repo_match_object_id(&id1, &label1,
1428 rc->parent_id, GOT_OBJ_TYPE_ANY,
1429 &refs, repo);
1430 if (error)
1431 goto done;
1434 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1435 GOT_OBJ_TYPE_ANY, &refs, repo);
1436 if (error)
1437 goto done;
1439 error = got_object_get_type(&obj_type, repo, id2);
1440 if (error)
1441 goto done;
1443 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1444 if (error)
1445 goto done;
1447 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1448 if (error)
1449 goto done;
1451 switch (obj_type) {
1452 case GOT_OBJ_TYPE_BLOB:
1453 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1454 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1455 repo, f3);
1456 break;
1457 case GOT_OBJ_TYPE_TREE:
1458 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1459 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1460 repo, f3);
1461 break;
1462 case GOT_OBJ_TYPE_COMMIT:
1463 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1464 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1465 repo, f3);
1466 break;
1467 default:
1468 error = got_error(GOT_ERR_OBJ_TYPE);
1470 if (error)
1471 goto done;
1473 if (fseek(f1, 0, SEEK_SET) == -1) {
1474 error = got_ferror(f1, GOT_ERR_IO);
1475 goto done;
1478 if (fseek(f2, 0, SEEK_SET) == -1) {
1479 error = got_ferror(f2, GOT_ERR_IO);
1480 goto done;
1483 if (fseek(f3, 0, SEEK_SET) == -1) {
1484 error = got_ferror(f3, GOT_ERR_IO);
1485 goto done;
1488 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1489 if (strncmp(line, "-", 1) == 0) {
1490 color = strdup("diff_minus");
1491 if (color == NULL) {
1492 error = got_error_from_errno("strdup");
1493 goto done;
1495 } else if (strncmp(line, "+", 1) == 0) {
1496 color = strdup("diff_plus");
1497 if (color == NULL) {
1498 error = got_error_from_errno("strdup");
1499 goto done;
1501 } else if (strncmp(line, "@@", 2) == 0) {
1502 color = strdup("diff_chunk_header");
1503 if (color == NULL) {
1504 error = got_error_from_errno("strdup");
1505 goto done;
1507 } else if (strncmp(line, "@@", 2) == 0) {
1508 color = strdup("diff_chunk_header");
1509 if (color == NULL) {
1510 error = got_error_from_errno("strdup");
1511 goto done;
1513 } else if (strncmp(line, "commit +", 8) == 0) {
1514 color = strdup("diff_meta");
1515 if (color == NULL) {
1516 error = got_error_from_errno("strdup");
1517 goto done;
1519 } else if (strncmp(line, "commit -", 8) == 0) {
1520 color = strdup("diff_meta");
1521 if (color == NULL) {
1522 error = got_error_from_errno("strdup");
1523 goto done;
1525 } else if (strncmp(line, "blob +", 6) == 0) {
1526 color = strdup("diff_meta");
1527 if (color == NULL) {
1528 error = got_error_from_errno("strdup");
1529 goto done;
1531 } else if (strncmp(line, "blob -", 6) == 0) {
1532 color = strdup("diff_meta");
1533 if (color == NULL) {
1534 error = got_error_from_errno("strdup");
1535 goto done;
1537 } else if (strncmp(line, "file +", 6) == 0) {
1538 color = strdup("diff_meta");
1539 if (color == NULL) {
1540 error = got_error_from_errno("strdup");
1541 goto done;
1543 } else if (strncmp(line, "file -", 6) == 0) {
1544 color = strdup("diff_meta");
1545 if (color == NULL) {
1546 error = got_error_from_errno("strdup");
1547 goto done;
1549 } else if (strncmp(line, "from:", 5) == 0) {
1550 color = strdup("diff_author");
1551 if (color == NULL) {
1552 error = got_error_from_errno("strdup");
1553 goto done;
1555 } else if (strncmp(line, "via:", 4) == 0) {
1556 color = strdup("diff_author");
1557 if (color == NULL) {
1558 error = got_error_from_errno("strdup");
1559 goto done;
1561 } else if (strncmp(line, "date:", 5) == 0) {
1562 color = strdup("diff_date");
1563 if (color == NULL) {
1564 error = got_error_from_errno("strdup");
1565 goto done;
1569 newline = strchr(line, '\n');
1570 if (newline)
1571 *newline = '\0';
1573 error = gotweb_escape_html(&eline, line);
1574 if (error)
1575 goto done;
1577 fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1578 color ? color : "", eline);
1579 free(eline);
1580 eline = NULL;
1582 if (linelen > 0)
1583 wrlen = wrlen + linelen;
1584 free(color);
1585 color = NULL;
1587 if (linelen == -1 && ferror(f3))
1588 error = got_error_from_errno("getline");
1589 done:
1590 free(color);
1591 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1592 error = got_error_from_errno("close");
1593 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1594 error = got_error_from_errno("close");
1595 if (f1) {
1596 const struct got_error *f1_err =
1597 got_gotweb_flushfile(f1, fd1);
1598 if (error == NULL)
1599 error = f1_err;
1601 if (f2) {
1602 const struct got_error *f2_err =
1603 got_gotweb_flushfile(f2, fd2);
1604 if (error == NULL)
1605 error = f2_err;
1607 if (f3) {
1608 const struct got_error *f3_err =
1609 got_gotweb_flushfile(f3, fd3);
1610 if (error == NULL)
1611 error = f3_err;
1613 got_ref_list_free(&refs);
1614 free(line);
1615 free(eline);
1616 free(label1);
1617 free(label2);
1618 free(id1);
1619 free(id2);
1620 return error;
1623 static const struct got_error *
1624 got_init_repo_commit(struct repo_commit **rc)
1626 *rc = calloc(1, sizeof(**rc));
1627 if (*rc == NULL)
1628 return got_error_from_errno2("%s: calloc", __func__);
1630 (*rc)->path = NULL;
1631 (*rc)->refs_str = NULL;
1632 (*rc)->commit_id = NULL;
1633 (*rc)->committer = NULL;
1634 (*rc)->author = NULL;
1635 (*rc)->parent_id = NULL;
1636 (*rc)->tree_id = NULL;
1637 (*rc)->commit_msg = NULL;
1639 return NULL;
1642 static const struct got_error *
1643 got_init_repo_tag(struct repo_tag **rt)
1645 *rt = calloc(1, sizeof(**rt));
1646 if (*rt == NULL)
1647 return got_error_from_errno2("%s: calloc", __func__);
1649 (*rt)->commit_id = NULL;
1650 (*rt)->tag_name = NULL;
1651 (*rt)->tag_commit = NULL;
1652 (*rt)->commit_msg = NULL;
1653 (*rt)->tagger = NULL;
1655 return NULL;