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 const struct got_error *error = NULL;
88 *fd = dup(*priv_fd);
90 if (*fd < 0)
91 return NULL;
93 *f = fdopen(*fd, "w+");
94 if (*f == NULL) {
95 close(*fd);
96 error = got_error(GOT_ERR_PRIVSEP_NO_FD);
97 }
99 return error;
102 static const struct got_error *
103 got_gotweb_dupfd(int *priv_fd, int *fd)
105 const struct got_error *error = NULL;
107 *fd = dup(*priv_fd);
109 if (*fd < 0)
110 return NULL;
112 return error;
115 const struct got_error *
116 got_get_repo_owner(char **owner, struct request *c, char *dir)
118 const struct got_error *error = NULL;
119 struct server *srv = c->srv;
120 struct transport *t = c->t;
121 struct got_repository *repo = t->repo;
122 const char *gitconfig_owner;
124 *owner = NULL;
126 if (srv->show_repo_owner == 0)
127 return NULL;
129 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
130 if (gitconfig_owner) {
131 *owner = strdup(gitconfig_owner);
132 if (*owner == NULL)
133 return got_error_from_errno("strdup");
135 return error;
138 const struct got_error *
139 got_get_repo_age(char **repo_age, struct request *c, char *dir,
140 const char *refname, int ref_tm)
142 const struct got_error *error = NULL;
143 struct server *srv = c->srv;
144 struct transport *t = c->t;
145 struct got_repository *repo = t->repo;
146 struct got_commit_object *commit = NULL;
147 struct got_reflist_head refs;
148 struct got_reflist_entry *re;
149 time_t committer_time = 0, cmp_time = 0;
151 *repo_age = NULL;
152 TAILQ_INIT(&refs);
154 if (srv->show_repo_age == 0)
155 return NULL;
157 error = got_ref_list(&refs, repo, "refs/heads",
158 got_ref_cmp_by_name, NULL);
159 if (error)
160 goto done;
162 /*
163 * Find the youngest branch tip in the repository, or the age of
164 * the a specific branch tip if a name was provided by the caller.
165 */
166 TAILQ_FOREACH(re, &refs, entry) {
167 struct got_object_id *id = NULL;
169 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
170 continue;
172 error = got_ref_resolve(&id, repo, re->ref);
173 if (error)
174 goto done;
176 error = got_object_open_as_commit(&commit, repo, id);
177 free(id);
178 if (error)
179 goto done;
181 committer_time =
182 got_object_commit_get_committer_time(commit);
183 got_object_commit_close(commit);
184 if (cmp_time < committer_time)
185 cmp_time = committer_time;
187 if (refname)
188 break;
191 if (cmp_time != 0) {
192 committer_time = cmp_time;
193 error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
195 done:
196 got_ref_list_free(&refs);
197 return error;
200 static const struct got_error *
201 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
202 struct got_commit_object *commit, struct got_reflist_head *refs,
203 struct got_object_id *id)
205 const struct got_error *error = NULL;
206 struct got_reflist_entry *re;
207 struct got_object_id *id2 = NULL;
208 struct got_object_qid *parent_id;
209 struct transport *t = c->t;
210 struct querystring *qs = c->t->qs;
211 char *commit_msg = NULL, *commit_msg0;
213 TAILQ_FOREACH(re, refs, entry) {
214 char *s;
215 const char *name;
216 struct got_tag_object *tag = NULL;
217 struct got_object_id *ref_id;
218 int cmp;
220 if (got_ref_is_symbolic(re->ref))
221 continue;
223 name = got_ref_get_name(re->ref);
224 if (strncmp(name, "refs/", 5) == 0)
225 name += 5;
226 if (strncmp(name, "got/", 4) == 0)
227 continue;
228 if (strncmp(name, "heads/", 6) == 0)
229 name += 6;
230 if (strncmp(name, "remotes/", 8) == 0) {
231 name += 8;
232 s = strstr(name, "/" GOT_REF_HEAD);
233 if (s != NULL && s[strlen(s)] == '\0')
234 continue;
236 error = got_ref_resolve(&ref_id, t->repo, re->ref);
237 if (error)
238 return error;
239 if (strncmp(name, "tags/", 5) == 0) {
240 error = got_object_open_as_tag(&tag, t->repo, ref_id);
241 if (error) {
242 if (error->code != GOT_ERR_OBJ_TYPE) {
243 free(ref_id);
244 continue;
246 /*
247 * Ref points at something other
248 * than a tag.
249 */
250 error = NULL;
251 tag = NULL;
254 cmp = got_object_id_cmp(tag ?
255 got_object_tag_get_object_id(tag) : ref_id, id);
256 free(ref_id);
257 if (tag)
258 got_object_tag_close(tag);
259 if (cmp != 0)
260 continue;
261 s = repo_commit->refs_str;
262 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
263 s ? ", " : "", name) == -1) {
264 error = got_error_from_errno("asprintf");
265 free(s);
266 repo_commit->refs_str = NULL;
267 return error;
269 free(s);
272 error = got_object_id_str(&repo_commit->commit_id, id);
273 if (error)
274 return error;
276 error = got_object_id_str(&repo_commit->tree_id,
277 got_object_commit_get_tree_id(commit));
278 if (error)
279 return error;
281 if (qs->action == DIFF) {
282 parent_id = STAILQ_FIRST(
283 got_object_commit_get_parent_ids(commit));
284 if (parent_id != NULL) {
285 id2 = got_object_id_dup(&parent_id->id);
286 error = got_object_id_str(&repo_commit->parent_id, id2);
287 if (error)
288 return error;
289 free(id2);
290 } else {
291 repo_commit->parent_id = strdup("/dev/null");
292 if (repo_commit->parent_id == NULL) {
293 error = got_error_from_errno("strdup");
294 return error;
299 repo_commit->committer_time =
300 got_object_commit_get_committer_time(commit);
302 repo_commit->author =
303 strdup(got_object_commit_get_author(commit));
304 if (repo_commit->author == NULL) {
305 error = got_error_from_errno("strdup");
306 return error;
308 repo_commit->committer =
309 strdup(got_object_commit_get_committer(commit));
310 if (repo_commit->committer == NULL) {
311 error = got_error_from_errno("strdup");
312 return error;
314 error = got_object_commit_get_logmsg(&commit_msg0, commit);
315 if (error)
316 return error;
318 commit_msg = commit_msg0;
319 while (*commit_msg == '\n')
320 commit_msg++;
322 repo_commit->commit_msg = strdup(commit_msg);
323 if (repo_commit->commit_msg == NULL)
324 error = got_error_from_errno("strdup");
325 free(commit_msg0);
326 return error;
329 const struct got_error *
330 got_get_repo_commits(struct request *c, int limit)
332 const struct got_error *error = NULL;
333 struct got_object_id *id = NULL;
334 struct got_commit_graph *graph = NULL;
335 struct got_commit_object *commit = NULL;
336 struct got_reflist_head refs;
337 struct got_reference *ref;
338 struct repo_commit *repo_commit = NULL;
339 struct server *srv = c->srv;
340 struct transport *t = c->t;
341 struct got_repository *repo = t->repo;
342 struct querystring *qs = t->qs;
343 struct repo_dir *repo_dir = t->repo_dir;
344 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
345 int chk_next = 0, chk_multi = 0, commit_found = 0;
346 int obj_type, limit_chk = 0;
348 TAILQ_INIT(&refs);
350 if (qs->file != NULL && strlen(qs->file) > 0)
351 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
352 qs->file) == -1)
353 return got_error_from_errno("asprintf");
355 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
356 repo_dir->name) == -1)
357 return got_error_from_errno("asprintf");
359 error = got_init_repo_commit(&repo_commit);
360 if (error)
361 return error;
363 /*
364 * XXX: jumping directly to a commit id via
365 * got_repo_match_object_id_prefix significantly improves performance,
366 * but does not allow us to create a PREVIOUS button, since commits can
367 * only be itereated forward. So, we have to match as we iterate from
368 * the headref.
369 */
370 if (qs->action == BRIEFS || qs->action == COMMITS ||
371 (qs->action == TREE && qs->commit == NULL)) {
372 error = got_ref_open(&ref, repo, qs->headref, 0);
373 if (error)
374 goto done;
376 error = got_ref_resolve(&id, repo, ref);
377 got_ref_close(ref);
378 if (error)
379 goto done;
380 } else if (qs->commit != NULL) {
381 error = got_ref_open(&ref, repo, qs->commit, 0);
382 if (error == NULL) {
383 error = got_ref_resolve(&id, repo, ref);
384 if (error)
385 goto done;
386 error = got_object_get_type(&obj_type, repo, id);
387 got_ref_close(ref);
388 if (error)
389 goto done;
390 if (obj_type == GOT_OBJ_TYPE_TAG) {
391 struct got_tag_object *tag;
392 error = got_object_open_as_tag(&tag, repo, id);
393 if (error)
394 goto done;
395 if (got_object_tag_get_object_type(tag) !=
396 GOT_OBJ_TYPE_COMMIT) {
397 got_object_tag_close(tag);
398 error = got_error(GOT_ERR_OBJ_TYPE);
399 goto done;
401 free(id);
402 id = got_object_id_dup(
403 got_object_tag_get_object_id(tag));
404 if (id == NULL)
405 error = got_error_from_errno(
406 "got_object_id_dup");
407 got_object_tag_close(tag);
408 if (error)
409 goto done;
410 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
411 error = got_error(GOT_ERR_OBJ_TYPE);
412 goto done;
415 error = got_repo_match_object_id_prefix(&id, qs->commit,
416 GOT_OBJ_TYPE_COMMIT, repo);
417 if (error)
418 goto done;
421 error = got_repo_map_path(&in_repo_path, repo, repo_path);
422 if (error)
423 goto done;
425 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
426 if (error)
427 goto done;
429 if (qs->file != NULL && strlen(qs->file) > 0) {
430 error = got_commit_graph_open(&graph, file_path, 0);
431 if (error)
432 goto done;
433 } else {
434 error = got_commit_graph_open(&graph, in_repo_path, 0);
435 if (error)
436 goto done;
439 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
440 if (error)
441 goto done;
443 for (;;) {
444 if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
445 commit_found == 0 && repo_commit->commit_id != NULL) {
446 t->prev_id = strdup(repo_commit->commit_id);
447 if (t->prev_id == NULL) {
448 error = got_error_from_errno("strdup");
449 goto done;
453 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
454 NULL);
455 if (error) {
456 if (error->code == GOT_ERR_ITER_COMPLETED)
457 error = NULL;
458 goto done;
460 if (id == NULL)
461 goto done;
463 error = got_object_open_as_commit(&commit, repo, id);
464 if (error)
465 goto done;
467 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
468 NULL);
469 if (error)
470 goto done;
472 error = got_get_repo_commit(c, repo_commit, commit,
473 &refs, id);
474 if (error)
475 goto done;
477 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
478 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
479 commit_found = 1;
480 else if (qs->file != NULL && strlen(qs->file) > 0 &&
481 qs->page == 0)
482 commit_found = 1;
483 else {
484 limit_chk++;
485 free(id);
486 id = NULL;
487 continue;
491 struct repo_commit *new_repo_commit = NULL;
492 error = got_init_repo_commit(&new_repo_commit);
493 if (error)
494 goto done;
496 TAILQ_INSERT_TAIL(&t->repo_commits, new_repo_commit, entry);
498 error = got_get_repo_commit(c, new_repo_commit, commit,
499 &refs, id);
500 if (error)
501 goto done;
503 free(id);
504 id = NULL;
506 if (limit == 1 && chk_multi == 0 &&
507 srv->max_commits_display != 1)
508 commit_found = 1;
509 else {
510 chk_multi = 1;
512 /*
513 * check for one more commit before breaking,
514 * so we know whether to navigate through briefs
515 * commits and summary
516 */
517 if (chk_next && (qs->action == BRIEFS ||
518 qs->action == COMMITS || qs->action == SUMMARY)) {
519 t->next_id = strdup(new_repo_commit->commit_id);
520 if (t->next_id == NULL) {
521 error = got_error_from_errno("strdup");
522 goto done;
524 if (commit) {
525 got_object_commit_close(commit);
526 commit = NULL;
528 if (t->next_id == NULL) {
529 error = got_error_from_errno("strdup");
530 goto done;
532 TAILQ_REMOVE(&t->repo_commits, new_repo_commit,
533 entry);
534 gotweb_free_repo_commit(new_repo_commit);
535 goto done;
538 got_ref_list_free(&refs);
539 if (error || (limit && --limit == 0)) {
540 if (commit_found || (qs->file != NULL &&
541 strlen(qs->file) > 0))
542 if (chk_multi == 0)
543 break;
544 chk_next = 1;
546 if (commit) {
547 got_object_commit_close(commit);
548 commit = NULL;
551 done:
552 gotweb_free_repo_commit(repo_commit);
553 if (commit)
554 got_object_commit_close(commit);
555 if (graph)
556 got_commit_graph_close(graph);
557 got_ref_list_free(&refs);
558 free(file_path);
559 free(repo_path);
560 free(id);
561 return error;
564 const struct got_error *
565 got_get_repo_tags(struct request *c, int limit)
567 const struct got_error *error = NULL;
568 struct got_object_id *id = NULL;
569 struct got_commit_object *commit = NULL;
570 struct got_reflist_head refs;
571 struct got_reference *ref;
572 struct got_reflist_entry *re;
573 struct server *srv = c->srv;
574 struct transport *t = c->t;
575 struct got_repository *repo = t->repo;
576 struct querystring *qs = t->qs;
577 struct repo_dir *repo_dir = t->repo_dir;
578 struct got_tag_object *tag = NULL;
579 struct repo_tag *rt = NULL, *trt = NULL;
580 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
581 char *commit_msg = NULL, *commit_msg0 = NULL;
582 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
584 TAILQ_INIT(&refs);
586 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
587 repo_dir->name) == -1)
588 return got_error_from_errno("asprintf");
590 if (error)
591 return error;
593 if (qs->commit == NULL && qs->action == TAGS) {
594 error = got_ref_open(&ref, repo, qs->headref, 0);
595 if (error)
596 goto err;
597 error = got_ref_resolve(&id, repo, ref);
598 got_ref_close(ref);
599 if (error)
600 goto err;
601 } else if (qs->commit == NULL && qs->action == TAG) {
602 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
603 goto err;
604 } else {
605 error = got_repo_match_object_id_prefix(&id, qs->commit,
606 GOT_OBJ_TYPE_COMMIT, repo);
607 if (error)
608 goto err;
611 if (qs->action != SUMMARY && qs->action != TAGS) {
612 error = got_object_open_as_commit(&commit, repo, id);
613 if (error)
614 goto err;
615 error = got_object_commit_get_logmsg(&commit_msg0, commit);
616 if (error)
617 goto err;
618 if (commit) {
619 got_object_commit_close(commit);
620 commit = NULL;
624 error = got_repo_map_path(&in_repo_path, repo, repo_path);
625 if (error)
626 goto err;
628 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
629 repo);
630 if (error)
631 goto err;
633 if (limit == 1)
634 chk_multi = 0;
636 /*
637 * XXX: again, see previous message about caching
638 */
640 TAILQ_FOREACH(re, &refs, entry) {
641 struct repo_tag *new_repo_tag = NULL;
642 error = got_init_repo_tag(&new_repo_tag);
643 if (error)
644 goto err;
646 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
648 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
649 if (new_repo_tag->tag_name == NULL) {
650 error = got_error_from_errno("strdup");
651 goto err;
654 error = got_ref_resolve(&id, repo, re->ref);
655 if (error)
656 goto done;
658 error = got_object_open_as_tag(&tag, repo, id);
659 if (error) {
660 if (error->code != GOT_ERR_OBJ_TYPE) {
661 free(id);
662 id = NULL;
663 goto done;
665 /* "lightweight" tag */
666 error = got_object_open_as_commit(&commit, repo, id);
667 if (error) {
668 free(id);
669 id = NULL;
670 goto done;
672 new_repo_tag->tagger =
673 strdup(got_object_commit_get_committer(commit));
674 if (new_repo_tag->tagger == NULL) {
675 error = got_error_from_errno("strdup");
676 goto err;
678 new_repo_tag->tagger_time =
679 got_object_commit_get_committer_time(commit);
680 error = got_object_id_str(&id_str, id);
681 if (error)
682 goto err;
683 free(id);
684 id = NULL;
685 } else {
686 free(id);
687 id = NULL;
688 new_repo_tag->tagger =
689 strdup(got_object_tag_get_tagger(tag));
690 if (new_repo_tag->tagger == NULL) {
691 error = got_error_from_errno("strdup");
692 goto err;
694 new_repo_tag->tagger_time =
695 got_object_tag_get_tagger_time(tag);
696 error = got_object_id_str(&id_str,
697 got_object_tag_get_object_id(tag));
698 if (error)
699 goto err;
702 new_repo_tag->commit_id = strdup(id_str);
703 if (new_repo_tag->commit_id == NULL)
704 goto err;
706 if (commit_found == 0 && qs->commit != NULL &&
707 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
708 continue;
709 else
710 commit_found = 1;
712 t->tag_count++;
714 /*
715 * check for one more commit before breaking,
716 * so we know whether to navigate through briefs
717 * commits and summary
718 */
719 if (chk_next) {
720 t->next_id = strdup(new_repo_tag->commit_id);
721 if (t->next_id == NULL) {
722 error = got_error_from_errno("strdup");
723 goto err;
725 if (commit) {
726 got_object_commit_close(commit);
727 commit = NULL;
729 if (t->next_id == NULL) {
730 error = got_error_from_errno("strdup");
731 goto err;
733 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
734 gotweb_free_repo_tag(new_repo_tag);
735 goto done;
738 if (commit) {
739 error = got_object_commit_get_logmsg(&new_repo_tag->
740 tag_commit, commit);
741 if (error)
742 goto done;
743 got_object_commit_close(commit);
744 commit = NULL;
745 } else {
746 new_repo_tag->tag_commit =
747 strdup(got_object_tag_get_message(tag));
748 if (new_repo_tag->tag_commit == NULL) {
749 error = got_error_from_errno("strdup");
750 goto done;
754 while (*new_repo_tag->tag_commit == '\n')
755 new_repo_tag->tag_commit++;
757 if (qs->action != SUMMARY && qs->action != TAGS) {
758 commit_msg = commit_msg0;
759 while (*commit_msg == '\n')
760 commit_msg++;
762 new_repo_tag->commit_msg = strdup(commit_msg);
763 if (new_repo_tag->commit_msg == NULL) {
764 error = got_error_from_errno("strdup");
765 free(commit_msg0);
766 goto err;
768 free(commit_msg0);
771 if (limit && --limit == 0) {
772 if (chk_multi == 0)
773 break;
774 chk_next = 1;
776 free(id);
777 id = NULL;
780 done:
781 /*
782 * we have tailq populated, so find previous commit id
783 * for navigation through briefs and commits
784 */
785 if (t->tag_count == 0) {
786 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
787 TAILQ_REMOVE(&t->repo_tags, rt, entry);
788 gotweb_free_repo_tag(rt);
791 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
792 commit_found = 0;
793 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
794 entry) {
795 if (commit_found == 0 && rt->commit_id != NULL &&
796 strcmp(qs->commit, rt->commit_id) != 0) {
797 continue;
798 } else
799 commit_found = 1;
800 if (c_cnt == srv->max_commits_display ||
801 rt == TAILQ_FIRST(&t->repo_tags)) {
802 t->prev_id = strdup(rt->commit_id);
803 if (t->prev_id == NULL)
804 error = got_error_from_errno("strdup");
805 break;
807 c_cnt++;
810 err:
811 if (commit)
812 got_object_commit_close(commit);
813 got_ref_list_free(&refs);
814 free(repo_path);
815 free(id);
816 return error;
819 const struct got_error *
820 got_output_repo_tree(struct request *c)
822 const struct got_error *error = NULL;
823 struct transport *t = c->t;
824 struct got_commit_object *commit = NULL;
825 struct got_repository *repo = t->repo;
826 struct querystring *qs = t->qs;
827 struct repo_commit *rc = NULL;
828 struct got_object_id *tree_id = NULL, *commit_id = NULL;
829 struct got_reflist_head refs;
830 struct got_tree_object *tree = NULL;
831 struct repo_dir *repo_dir = t->repo_dir;
832 const char *name, *index_page_str, *folder;
833 char *id_str = NULL, *escaped_name = NULL;
834 char *path = NULL, *in_repo_path = NULL, *modestr = NULL;
835 int nentries, i, r;
837 TAILQ_INIT(&refs);
839 rc = TAILQ_FIRST(&t->repo_commits);
841 if (qs->folder != NULL) {
842 path = strdup(qs->folder);
843 if (path == NULL) {
844 error = got_error_from_errno("strdup");
845 goto done;
847 } else {
848 error = got_repo_map_path(&in_repo_path, repo, repo_dir->path);
849 if (error)
850 goto done;
851 free(path);
852 path = in_repo_path;
855 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
856 GOT_OBJ_TYPE_COMMIT, &refs, repo);
857 if (error)
858 goto done;
860 error = got_object_open_as_commit(&commit, repo, commit_id);
861 if (error)
862 goto done;
864 error = got_object_id_by_path(&tree_id, repo, commit, path);
865 if (error)
866 goto done;
868 error = got_object_open_as_tree(&tree, repo, tree_id);
869 if (error)
870 goto done;
872 nentries = got_object_tree_get_nentries(tree);
874 index_page_str = qs->index_page_str ? qs->index_page_str : "";
875 folder = qs->folder ? qs->folder : "";
877 for (i = 0; i < nentries; i++) {
878 struct got_tree_entry *te;
879 mode_t mode;
881 te = got_object_tree_get_entry(tree, i);
883 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
884 if (error)
885 goto done;
887 modestr = strdup("");
888 if (modestr == NULL) {
889 error = got_error_from_errno("strdup");
890 goto done;
892 mode = got_tree_entry_get_mode(te);
893 if (got_object_tree_entry_is_submodule(te)) {
894 free(modestr);
895 modestr = strdup("$");
896 if (modestr == NULL) {
897 error = got_error_from_errno("strdup");
898 goto done;
900 } else if (S_ISLNK(mode)) {
901 free(modestr);
902 modestr = strdup("@");
903 if (modestr == NULL) {
904 error = got_error_from_errno("strdup");
905 goto done;
907 } else if (S_ISDIR(mode)) {
908 free(modestr);
909 modestr = strdup("/");
910 if (modestr == NULL) {
911 error = got_error_from_errno("strdup");
912 goto done;
914 } else if (mode & S_IXUSR) {
915 free(modestr);
916 modestr = strdup("*");
917 if (modestr == NULL) {
918 error = got_error_from_errno("strdup");
919 goto done;
923 name = got_tree_entry_get_name(te);
924 error = gotweb_escape_html(&escaped_name, name);
925 if (error)
926 goto done;
928 if (S_ISDIR(mode)) {
929 r = fcgi_printf(c,
930 "<div class='tree_wrapper'>\n"
931 "<div class='tree_line'>"
932 "<a class='diff_directory' "
933 "href='?index_page=%s&path=%s&action=tree"
934 "&commit=%s&folder=%s/%s'>%s%s</a>"
935 "</div>\n" /* .tree_line */
936 "<div class='tree_line_blank'>&nbsp;</div>\n"
937 "</div>\n", /* .tree_wrapper */
938 index_page_str, qs->path, rc->commit_id,
939 folder, name, escaped_name, modestr);
940 if (r == -1)
941 goto done;
942 } else {
943 r = fcgi_printf(c,
944 "<div class='tree_wrapper'>\n"
945 "<div class='tree_line'>"
946 "<a href='?index_page=%s&path=%s&action=blob"
947 "&commit=%s&folder=%s&file=%s'>%s%s</a>"
948 "</div>\n" /* .tree_line */
949 "<div class='tree_line_blank'>"
950 "<a href='?index_page=%s&path=%s&action=commits"
951 "&commit=%s&folder=%s&file=%s'>commits</a>\n"
952 " | \n"
953 "<a href='?index_page=%s&path=%s&action=blame"
954 "&commit=%s&folder=%s&file=%s'>blame</a>\n"
955 "</div>\n" /* .tree_line_blank */
956 "</div>\n", /* .tree_wrapper */
957 index_page_str, qs->path, rc->commit_id,
958 folder, name, escaped_name, modestr,
959 index_page_str, qs->path, rc->commit_id,
960 folder, name,
961 index_page_str, qs->path, rc->commit_id,
962 folder, name);
963 if (r == -1)
964 goto done;
966 free(id_str);
967 id_str = NULL;
968 free(modestr);
969 modestr = NULL;
970 free(escaped_name);
971 escaped_name = NULL;
973 done:
974 free(escaped_name);
975 free(id_str);
976 free(modestr);
977 free(path);
978 got_ref_list_free(&refs);
979 if (commit)
980 got_object_commit_close(commit);
981 free(commit_id);
982 free(tree_id);
983 return error;
986 const struct got_error *
987 got_output_file_blob(struct request *c)
989 const struct got_error *error = NULL;
990 struct transport *t = c->t;
991 struct got_repository *repo = t->repo;
992 struct querystring *qs = c->t->qs;
993 struct got_commit_object *commit = NULL;
994 struct got_object_id *commit_id = NULL;
995 struct got_reflist_head refs;
996 struct got_blob_object *blob = NULL;
997 char *path = NULL, *in_repo_path = NULL;
998 int obj_type, set_mime = 0, fd = -1;
999 size_t len, hdrlen;
1000 const uint8_t *buf;
1002 TAILQ_INIT(&refs);
1004 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1005 qs->folder ? "/" : "", qs->file) == -1) {
1006 error = got_error_from_errno("asprintf");
1007 goto done;
1010 error = got_repo_map_path(&in_repo_path, repo, path);
1011 if (error)
1012 goto done;
1014 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1015 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1016 if (error)
1017 goto done;
1019 error = got_object_open_as_commit(&commit, repo, commit_id);
1020 if (error)
1021 goto done;
1023 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1024 if (error)
1025 goto done;
1027 if (commit_id == NULL) {
1028 error = got_error(GOT_ERR_NO_OBJ);
1029 goto done;
1032 error = got_object_get_type(&obj_type, repo, commit_id);
1033 if (error)
1034 goto done;
1036 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1037 error = got_error(GOT_ERR_OBJ_TYPE);
1038 goto done;
1041 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1042 if (error)
1043 goto done;
1045 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1046 if (error)
1047 goto done;
1048 hdrlen = got_object_blob_get_hdrlen(blob);
1049 do {
1050 error = got_object_blob_read_block(&len, blob);
1051 if (error)
1052 goto done;
1053 buf = got_object_blob_get_read_buf(blob);
1056 * Skip blob object header first time around,
1057 * which also contains a zero byte.
1059 buf += hdrlen;
1060 if (set_mime == 0) {
1061 if (isbinary(buf, len - hdrlen)) {
1062 error = gotweb_render_content_type_file(c,
1063 "application/octet-stream",
1064 qs->file);
1065 if (error) {
1066 log_warnx("%s: %s", __func__,
1067 error->msg);
1068 goto done;
1070 } else {
1071 error = gotweb_render_content_type(c,
1072 "text/plain");
1073 if (error) {
1074 log_warnx("%s: %s", __func__,
1075 error->msg);
1076 goto done;
1080 set_mime = 1;
1081 fcgi_gen_binary_response(c, buf, len - hdrlen);
1083 hdrlen = 0;
1084 } while (len != 0);
1085 done:
1086 if (commit)
1087 got_object_commit_close(commit);
1088 if (fd != -1 && close(fd) == -1 && error == NULL)
1089 error = got_error_from_errno("close");
1090 if (blob)
1091 got_object_blob_close(blob);
1092 free(in_repo_path);
1093 free(commit_id);
1094 free(path);
1095 return error;
1098 struct blame_line {
1099 int annotated;
1100 char *id_str;
1101 char *committer;
1102 char datebuf[11]; /* YYYY-MM-DD + NUL */
1105 struct blame_cb_args {
1106 struct blame_line *lines;
1107 int nlines;
1108 int nlines_prec;
1109 int lineno_cur;
1110 off_t *line_offsets;
1111 FILE *f;
1112 struct got_repository *repo;
1113 struct request *c;
1116 static const struct got_error *
1117 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1118 struct got_commit_object *commit, struct got_object_id *id)
1120 const struct got_error *err = NULL;
1121 struct blame_cb_args *a = arg;
1122 struct blame_line *bline;
1123 struct request *c = a->c;
1124 struct transport *t = c->t;
1125 struct querystring *qs = t->qs;
1126 struct repo_dir *repo_dir = t->repo_dir;
1127 const char *index_page_str;
1128 char *line = NULL, *eline = NULL;
1129 size_t linesize = 0;
1130 off_t offset;
1131 struct tm tm;
1132 time_t committer_time;
1133 int r;
1135 if (nlines != a->nlines ||
1136 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1137 return got_error(GOT_ERR_RANGE);
1139 if (lineno == -1)
1140 return NULL; /* no change in this commit */
1142 /* Annotate this line. */
1143 bline = &a->lines[lineno - 1];
1144 if (bline->annotated)
1145 return NULL;
1146 err = got_object_id_str(&bline->id_str, id);
1147 if (err)
1148 return err;
1150 bline->committer = strdup(got_object_commit_get_committer(commit));
1151 if (bline->committer == NULL) {
1152 err = got_error_from_errno("strdup");
1153 goto done;
1156 committer_time = got_object_commit_get_committer_time(commit);
1157 if (gmtime_r(&committer_time, &tm) == NULL)
1158 return got_error_from_errno("gmtime_r");
1159 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1160 &tm) == 0) {
1161 err = got_error(GOT_ERR_NO_SPACE);
1162 goto done;
1164 bline->annotated = 1;
1166 /* Print lines annotated so far. */
1167 bline = &a->lines[a->lineno_cur - 1];
1168 if (!bline->annotated)
1169 goto done;
1171 offset = a->line_offsets[a->lineno_cur - 1];
1172 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1173 err = got_error_from_errno("fseeko");
1174 goto done;
1177 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1179 while (a->lineno_cur <= a->nlines && bline->annotated) {
1180 char *smallerthan, *at, *nl, *committer;
1181 size_t len;
1183 if (getline(&line, &linesize, a->f) == -1) {
1184 if (ferror(a->f))
1185 err = got_error_from_errno("getline");
1186 break;
1189 committer = bline->committer;
1190 smallerthan = strchr(committer, '<');
1191 if (smallerthan && smallerthan[1] != '\0')
1192 committer = smallerthan + 1;
1193 at = strchr(committer, '@');
1194 if (at)
1195 *at = '\0';
1196 len = strlen(committer);
1197 if (len >= 9)
1198 committer[8] = '\0';
1200 nl = strchr(line, '\n');
1201 if (nl)
1202 *nl = '\0';
1204 err = gotweb_escape_html(&eline, line);
1205 if (err)
1206 goto done;
1208 r = fcgi_printf(c, "<div class='blame_wrapper'>"
1209 "<div class='blame_number'>%.*d</div>"
1210 "<div class='blame_hash'>"
1211 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1212 "%.8s</a>"
1213 "</div>"
1214 "<div class='blame_date'>%s</div>"
1215 "<div class='blame_author'>%s</div>"
1216 "<div class='blame_code'>%s</div>"
1217 "</div>", /* .blame_wrapper */
1218 a->nlines_prec, a->lineno_cur,
1219 index_page_str, repo_dir->name, bline->id_str,
1220 bline->id_str,
1221 bline->datebuf,
1222 committer,
1223 eline);
1224 if (r == -1)
1225 goto done;
1227 a->lineno_cur++;
1228 bline = &a->lines[a->lineno_cur - 1];
1230 done:
1231 free(line);
1232 free(eline);
1233 return err;
1236 const struct got_error *
1237 got_output_file_blame(struct request *c)
1239 const struct got_error *error = NULL;
1240 struct transport *t = c->t;
1241 struct got_repository *repo = t->repo;
1242 struct querystring *qs = c->t->qs;
1243 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1244 struct got_commit_object *commit = NULL;
1245 struct got_reflist_head refs;
1246 struct got_blob_object *blob = NULL;
1247 char *path = NULL, *in_repo_path = NULL;
1248 struct blame_cb_args bca;
1249 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1250 int fd6 = -1;
1251 off_t filesize;
1252 FILE *f1 = NULL, *f2 = NULL;
1254 TAILQ_INIT(&refs);
1255 bca.f = NULL;
1256 bca.lines = NULL;
1258 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1259 qs->folder ? "/" : "", qs->file) == -1) {
1260 error = got_error_from_errno("asprintf");
1261 goto done;
1264 error = got_repo_map_path(&in_repo_path, repo, path);
1265 if (error)
1266 goto done;
1268 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1269 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1270 if (error)
1271 goto done;
1273 error = got_object_open_as_commit(&commit, repo, commit_id);
1274 if (error)
1275 goto done;
1277 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1278 if (error)
1279 goto done;
1281 if (commit_id == NULL) {
1282 error = got_error(GOT_ERR_NO_OBJ);
1283 goto done;
1286 error = got_object_get_type(&obj_type, repo, obj_id);
1287 if (error)
1288 goto done;
1290 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1291 error = got_error(GOT_ERR_OBJ_TYPE);
1292 goto done;
1295 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1296 if (error)
1297 goto done;
1299 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1300 if (error)
1301 goto done;
1303 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1304 if (error)
1305 goto done;
1307 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1308 &bca.line_offsets, bca.f, blob);
1309 if (error || bca.nlines == 0)
1310 goto done;
1312 /* Don't include \n at EOF in the blame line count. */
1313 if (bca.line_offsets[bca.nlines - 1] == filesize)
1314 bca.nlines--;
1316 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1317 if (bca.lines == NULL) {
1318 error = got_error_from_errno("calloc");
1319 goto done;
1321 bca.lineno_cur = 1;
1322 bca.nlines_prec = 0;
1323 i = bca.nlines;
1324 while (i > 0) {
1325 i /= 10;
1326 bca.nlines_prec++;
1328 bca.repo = repo;
1329 bca.c = c;
1331 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1332 if (error)
1333 goto done;
1335 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1336 if (error)
1337 goto done;
1339 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1340 if (error)
1341 goto done;
1343 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1344 if (error)
1345 goto done;
1347 error = got_blame(in_repo_path, commit_id, repo,
1348 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1349 fd3, fd4, f1, f2);
1351 if (blob) {
1352 free(bca.line_offsets);
1353 for (i = 0; i < bca.nlines; i++) {
1354 struct blame_line *bline = &bca.lines[i];
1355 free(bline->id_str);
1356 free(bline->committer);
1359 done:
1360 free(bca.lines);
1361 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1362 error = got_error_from_errno("close");
1363 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1364 error = got_error_from_errno("close");
1365 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1366 error = got_error_from_errno("close");
1367 if (bca.f) {
1368 const struct got_error *bca_err =
1369 got_gotweb_flushfile(bca.f, fd1);
1370 if (error == NULL)
1371 error = bca_err;
1373 if (f1) {
1374 const struct got_error *f1_err =
1375 got_gotweb_flushfile(f1, fd5);
1376 if (error == NULL)
1377 error = f1_err;
1379 if (f2) {
1380 const struct got_error *f2_err =
1381 got_gotweb_flushfile(f2, fd6);
1382 if (error == NULL)
1383 error = f2_err;
1385 if (commit)
1386 got_object_commit_close(commit);
1387 if (blob)
1388 got_object_blob_close(blob);
1389 free(in_repo_path);
1390 free(commit_id);
1391 free(path);
1392 return error;
1395 const struct got_error *
1396 got_output_repo_diff(struct request *c)
1398 const struct got_error *error = NULL;
1399 struct transport *t = c->t;
1400 struct got_repository *repo = t->repo;
1401 struct repo_commit *rc = NULL;
1402 struct got_object_id *id1 = NULL, *id2 = NULL;
1403 struct got_reflist_head refs;
1404 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1405 char *label1 = NULL, *label2 = NULL, *line = NULL;
1406 char *newline, *eline = NULL, *color = NULL;
1407 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1408 size_t linesize = 0;
1409 ssize_t linelen;
1410 int wrlen = 0;
1412 TAILQ_INIT(&refs);
1414 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1415 if (error)
1416 return error;
1418 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1419 if (error)
1420 return error;
1422 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1423 if (error)
1424 return error;
1426 rc = TAILQ_FIRST(&t->repo_commits);
1428 if (rc->parent_id != NULL &&
1429 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1430 error = got_repo_match_object_id(&id1, &label1,
1431 rc->parent_id, GOT_OBJ_TYPE_ANY,
1432 &refs, repo);
1433 if (error)
1434 goto done;
1437 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1438 GOT_OBJ_TYPE_ANY, &refs, repo);
1439 if (error)
1440 goto done;
1442 error = got_object_get_type(&obj_type, repo, id2);
1443 if (error)
1444 goto done;
1446 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1447 if (error)
1448 goto done;
1450 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1451 if (error)
1452 goto done;
1454 switch (obj_type) {
1455 case GOT_OBJ_TYPE_BLOB:
1456 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1457 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1458 repo, f3);
1459 break;
1460 case GOT_OBJ_TYPE_TREE:
1461 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1462 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1463 repo, f3);
1464 break;
1465 case GOT_OBJ_TYPE_COMMIT:
1466 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1467 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1468 repo, f3);
1469 break;
1470 default:
1471 error = got_error(GOT_ERR_OBJ_TYPE);
1473 if (error)
1474 goto done;
1476 if (fseek(f1, 0, SEEK_SET) == -1) {
1477 error = got_ferror(f1, GOT_ERR_IO);
1478 goto done;
1481 if (fseek(f2, 0, SEEK_SET) == -1) {
1482 error = got_ferror(f2, GOT_ERR_IO);
1483 goto done;
1486 if (fseek(f3, 0, SEEK_SET) == -1) {
1487 error = got_ferror(f3, GOT_ERR_IO);
1488 goto done;
1491 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1492 if (strncmp(line, "-", 1) == 0) {
1493 color = strdup("diff_minus");
1494 if (color == NULL) {
1495 error = got_error_from_errno("strdup");
1496 goto done;
1498 } else if (strncmp(line, "+", 1) == 0) {
1499 color = strdup("diff_plus");
1500 if (color == NULL) {
1501 error = got_error_from_errno("strdup");
1502 goto done;
1504 } else if (strncmp(line, "@@", 2) == 0) {
1505 color = strdup("diff_chunk_header");
1506 if (color == NULL) {
1507 error = got_error_from_errno("strdup");
1508 goto done;
1510 } else if (strncmp(line, "@@", 2) == 0) {
1511 color = strdup("diff_chunk_header");
1512 if (color == NULL) {
1513 error = got_error_from_errno("strdup");
1514 goto done;
1516 } else if (strncmp(line, "commit +", 8) == 0) {
1517 color = strdup("diff_meta");
1518 if (color == NULL) {
1519 error = got_error_from_errno("strdup");
1520 goto done;
1522 } else if (strncmp(line, "commit -", 8) == 0) {
1523 color = strdup("diff_meta");
1524 if (color == NULL) {
1525 error = got_error_from_errno("strdup");
1526 goto done;
1528 } else if (strncmp(line, "blob +", 6) == 0) {
1529 color = strdup("diff_meta");
1530 if (color == NULL) {
1531 error = got_error_from_errno("strdup");
1532 goto done;
1534 } else if (strncmp(line, "blob -", 6) == 0) {
1535 color = strdup("diff_meta");
1536 if (color == NULL) {
1537 error = got_error_from_errno("strdup");
1538 goto done;
1540 } else if (strncmp(line, "file +", 6) == 0) {
1541 color = strdup("diff_meta");
1542 if (color == NULL) {
1543 error = got_error_from_errno("strdup");
1544 goto done;
1546 } else if (strncmp(line, "file -", 6) == 0) {
1547 color = strdup("diff_meta");
1548 if (color == NULL) {
1549 error = got_error_from_errno("strdup");
1550 goto done;
1552 } else if (strncmp(line, "from:", 5) == 0) {
1553 color = strdup("diff_author");
1554 if (color == NULL) {
1555 error = got_error_from_errno("strdup");
1556 goto done;
1558 } else if (strncmp(line, "via:", 4) == 0) {
1559 color = strdup("diff_author");
1560 if (color == NULL) {
1561 error = got_error_from_errno("strdup");
1562 goto done;
1564 } else if (strncmp(line, "date:", 5) == 0) {
1565 color = strdup("diff_date");
1566 if (color == NULL) {
1567 error = got_error_from_errno("strdup");
1568 goto done;
1572 newline = strchr(line, '\n');
1573 if (newline)
1574 *newline = '\0';
1576 error = gotweb_escape_html(&eline, line);
1577 if (error)
1578 goto done;
1580 fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1581 color ? color : "", eline);
1582 free(eline);
1583 eline = NULL;
1585 if (linelen > 0)
1586 wrlen = wrlen + linelen;
1587 free(color);
1588 color = NULL;
1590 if (linelen == -1 && ferror(f3))
1591 error = got_error_from_errno("getline");
1592 done:
1593 free(color);
1594 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1595 error = got_error_from_errno("close");
1596 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1597 error = got_error_from_errno("close");
1598 if (f1) {
1599 const struct got_error *f1_err =
1600 got_gotweb_flushfile(f1, fd1);
1601 if (error == NULL)
1602 error = f1_err;
1604 if (f2) {
1605 const struct got_error *f2_err =
1606 got_gotweb_flushfile(f2, fd2);
1607 if (error == NULL)
1608 error = f2_err;
1610 if (f3) {
1611 const struct got_error *f3_err =
1612 got_gotweb_flushfile(f3, fd3);
1613 if (error == NULL)
1614 error = f3_err;
1616 got_ref_list_free(&refs);
1617 free(line);
1618 free(eline);
1619 free(label1);
1620 free(label2);
1621 free(id1);
1622 free(id2);
1623 return error;
1626 static const struct got_error *
1627 got_init_repo_commit(struct repo_commit **rc)
1629 const struct got_error *error = NULL;
1631 *rc = calloc(1, sizeof(**rc));
1632 if (*rc == NULL)
1633 return got_error_from_errno2("%s: calloc", __func__);
1635 (*rc)->path = NULL;
1636 (*rc)->refs_str = NULL;
1637 (*rc)->commit_id = NULL;
1638 (*rc)->committer = NULL;
1639 (*rc)->author = NULL;
1640 (*rc)->parent_id = NULL;
1641 (*rc)->tree_id = NULL;
1642 (*rc)->commit_msg = NULL;
1644 return error;
1647 static const struct got_error *
1648 got_init_repo_tag(struct repo_tag **rt)
1650 const struct got_error *error = NULL;
1652 *rt = calloc(1, sizeof(**rt));
1653 if (*rt == NULL)
1654 return got_error_from_errno2("%s: calloc", __func__);
1656 (*rt)->commit_id = NULL;
1657 (*rt)->tag_name = NULL;
1658 (*rt)->tag_commit = NULL;
1659 (*rt)->commit_msg = NULL;
1660 (*rt)->tagger = NULL;
1662 return error;