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 s = strstr(name, "/" GOT_REF_HEAD);
231 if (s != NULL && s[strlen(s)] == '\0')
232 continue;
234 error = got_ref_resolve(&ref_id, t->repo, re->ref);
235 if (error)
236 return error;
237 if (strncmp(name, "tags/", 5) == 0) {
238 error = got_object_open_as_tag(&tag, t->repo, ref_id);
239 if (error) {
240 if (error->code != GOT_ERR_OBJ_TYPE) {
241 free(ref_id);
242 continue;
244 /*
245 * Ref points at something other
246 * than a tag.
247 */
248 error = NULL;
249 tag = NULL;
252 cmp = got_object_id_cmp(tag ?
253 got_object_tag_get_object_id(tag) : ref_id, id);
254 free(ref_id);
255 if (tag)
256 got_object_tag_close(tag);
257 if (cmp != 0)
258 continue;
259 s = repo_commit->refs_str;
260 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
261 s ? ", " : "", name) == -1) {
262 error = got_error_from_errno("asprintf");
263 free(s);
264 repo_commit->refs_str = NULL;
265 return error;
267 free(s);
270 error = got_object_id_str(&repo_commit->commit_id, id);
271 if (error)
272 return error;
274 error = got_object_id_str(&repo_commit->tree_id,
275 got_object_commit_get_tree_id(commit));
276 if (error)
277 return error;
279 if (qs->action == DIFF) {
280 parent_id = STAILQ_FIRST(
281 got_object_commit_get_parent_ids(commit));
282 if (parent_id != NULL) {
283 id2 = got_object_id_dup(&parent_id->id);
284 error = got_object_id_str(&repo_commit->parent_id, id2);
285 if (error)
286 return error;
287 free(id2);
288 } else {
289 repo_commit->parent_id = strdup("/dev/null");
290 if (repo_commit->parent_id == NULL) {
291 error = got_error_from_errno("strdup");
292 return error;
297 repo_commit->committer_time =
298 got_object_commit_get_committer_time(commit);
300 repo_commit->author =
301 strdup(got_object_commit_get_author(commit));
302 if (repo_commit->author == NULL) {
303 error = got_error_from_errno("strdup");
304 return error;
306 repo_commit->committer =
307 strdup(got_object_commit_get_committer(commit));
308 if (repo_commit->committer == NULL) {
309 error = got_error_from_errno("strdup");
310 return error;
312 error = got_object_commit_get_logmsg(&commit_msg0, commit);
313 if (error)
314 return error;
316 commit_msg = commit_msg0;
317 while (*commit_msg == '\n')
318 commit_msg++;
320 repo_commit->commit_msg = strdup(commit_msg);
321 if (repo_commit->commit_msg == NULL)
322 error = got_error_from_errno("strdup");
323 free(commit_msg0);
324 return error;
327 const struct got_error *
328 got_get_repo_commits(struct request *c, int limit)
330 const struct got_error *error = NULL;
331 struct got_object_id *id = NULL;
332 struct got_commit_graph *graph = NULL;
333 struct got_commit_object *commit = NULL;
334 struct got_reflist_head refs;
335 struct got_reference *ref;
336 struct repo_commit *repo_commit = NULL;
337 struct server *srv = c->srv;
338 struct transport *t = c->t;
339 struct got_repository *repo = t->repo;
340 struct querystring *qs = t->qs;
341 struct repo_dir *repo_dir = t->repo_dir;
342 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
343 int chk_next = 0, chk_multi = 0, commit_found = 0;
344 int obj_type, limit_chk = 0;
346 TAILQ_INIT(&refs);
348 if (qs->file != NULL && strlen(qs->file) > 0)
349 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
350 qs->file) == -1)
351 return got_error_from_errno("asprintf");
353 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
354 repo_dir->name) == -1)
355 return got_error_from_errno("asprintf");
357 error = got_init_repo_commit(&repo_commit);
358 if (error)
359 return error;
361 /*
362 * XXX: jumping directly to a commit id via
363 * got_repo_match_object_id_prefix significantly improves performance,
364 * but does not allow us to create a PREVIOUS button, since commits can
365 * only be itereated forward. So, we have to match as we iterate from
366 * the headref.
367 */
368 if (qs->action == BRIEFS || qs->action == COMMITS ||
369 (qs->action == TREE && qs->commit == NULL)) {
370 error = got_ref_open(&ref, repo, qs->headref, 0);
371 if (error)
372 goto done;
374 error = got_ref_resolve(&id, repo, ref);
375 got_ref_close(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 got_ref_close(ref);
386 if (error)
387 goto done;
388 if (obj_type == GOT_OBJ_TYPE_TAG) {
389 struct got_tag_object *tag;
390 error = got_object_open_as_tag(&tag, repo, id);
391 if (error)
392 goto done;
393 if (got_object_tag_get_object_type(tag) !=
394 GOT_OBJ_TYPE_COMMIT) {
395 got_object_tag_close(tag);
396 error = got_error(GOT_ERR_OBJ_TYPE);
397 goto done;
399 free(id);
400 id = got_object_id_dup(
401 got_object_tag_get_object_id(tag));
402 if (id == NULL)
403 error = got_error_from_errno(
404 "got_object_id_dup");
405 got_object_tag_close(tag);
406 if (error)
407 goto done;
408 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
409 error = got_error(GOT_ERR_OBJ_TYPE);
410 goto done;
413 error = got_repo_match_object_id_prefix(&id, qs->commit,
414 GOT_OBJ_TYPE_COMMIT, repo);
415 if (error)
416 goto done;
419 error = got_repo_map_path(&in_repo_path, repo, repo_path);
420 if (error)
421 goto done;
423 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
424 if (error)
425 goto done;
427 if (qs->file != NULL && strlen(qs->file) > 0) {
428 error = got_commit_graph_open(&graph, file_path, 0);
429 if (error)
430 goto done;
431 } else {
432 error = got_commit_graph_open(&graph, in_repo_path, 0);
433 if (error)
434 goto done;
437 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
438 if (error)
439 goto done;
441 for (;;) {
442 if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
443 commit_found == 0 && repo_commit->commit_id != NULL) {
444 t->prev_id = strdup(repo_commit->commit_id);
445 if (t->prev_id == NULL) {
446 error = got_error_from_errno("strdup");
447 goto done;
451 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
452 NULL);
453 if (error) {
454 if (error->code == GOT_ERR_ITER_COMPLETED)
455 error = NULL;
456 goto done;
458 if (id == NULL)
459 goto done;
461 error = got_object_open_as_commit(&commit, repo, id);
462 if (error)
463 goto done;
465 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
466 NULL);
467 if (error)
468 goto done;
470 error = got_get_repo_commit(c, repo_commit, commit,
471 &refs, id);
472 if (error)
473 goto done;
475 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
476 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
477 commit_found = 1;
478 else if (qs->file != NULL && strlen(qs->file) > 0 &&
479 qs->page == 0)
480 commit_found = 1;
481 else {
482 limit_chk++;
483 free(id);
484 id = NULL;
485 continue;
489 struct repo_commit *new_repo_commit = NULL;
490 error = got_init_repo_commit(&new_repo_commit);
491 if (error)
492 goto done;
494 TAILQ_INSERT_TAIL(&t->repo_commits, new_repo_commit, entry);
496 error = got_get_repo_commit(c, new_repo_commit, commit,
497 &refs, id);
498 if (error)
499 goto done;
501 free(id);
502 id = NULL;
504 if (limit == 1 && chk_multi == 0 &&
505 srv->max_commits_display != 1)
506 commit_found = 1;
507 else {
508 chk_multi = 1;
510 /*
511 * check for one more commit before breaking,
512 * so we know whether to navigate through briefs
513 * commits and summary
514 */
515 if (chk_next && (qs->action == BRIEFS ||
516 qs->action == COMMITS || qs->action == SUMMARY)) {
517 t->next_id = strdup(new_repo_commit->commit_id);
518 if (t->next_id == NULL) {
519 error = got_error_from_errno("strdup");
520 goto done;
522 if (commit) {
523 got_object_commit_close(commit);
524 commit = NULL;
526 if (t->next_id == NULL) {
527 error = got_error_from_errno("strdup");
528 goto done;
530 TAILQ_REMOVE(&t->repo_commits, new_repo_commit,
531 entry);
532 gotweb_free_repo_commit(new_repo_commit);
533 goto done;
536 got_ref_list_free(&refs);
537 if (error || (limit && --limit == 0)) {
538 if (commit_found || (qs->file != NULL &&
539 strlen(qs->file) > 0))
540 if (chk_multi == 0)
541 break;
542 chk_next = 1;
544 if (commit) {
545 got_object_commit_close(commit);
546 commit = NULL;
549 done:
550 gotweb_free_repo_commit(repo_commit);
551 if (commit)
552 got_object_commit_close(commit);
553 if (graph)
554 got_commit_graph_close(graph);
555 got_ref_list_free(&refs);
556 free(file_path);
557 free(repo_path);
558 free(id);
559 return error;
562 const struct got_error *
563 got_get_repo_tags(struct request *c, int limit)
565 const struct got_error *error = NULL;
566 struct got_object_id *id = NULL;
567 struct got_commit_object *commit = NULL;
568 struct got_reflist_head refs;
569 struct got_reference *ref;
570 struct got_reflist_entry *re;
571 struct server *srv = c->srv;
572 struct transport *t = c->t;
573 struct got_repository *repo = t->repo;
574 struct querystring *qs = t->qs;
575 struct repo_dir *repo_dir = t->repo_dir;
576 struct got_tag_object *tag = NULL;
577 struct repo_tag *rt = NULL, *trt = NULL;
578 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
579 char *commit_msg = NULL, *commit_msg0 = NULL;
580 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
582 TAILQ_INIT(&refs);
584 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
585 repo_dir->name) == -1)
586 return got_error_from_errno("asprintf");
588 if (error)
589 return error;
591 if (qs->commit == NULL && qs->action == TAGS) {
592 error = got_ref_open(&ref, repo, qs->headref, 0);
593 if (error)
594 goto err;
595 error = got_ref_resolve(&id, repo, ref);
596 got_ref_close(ref);
597 if (error)
598 goto err;
599 } else if (qs->commit == NULL && qs->action == TAG) {
600 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
601 goto err;
602 } else {
603 error = got_repo_match_object_id_prefix(&id, qs->commit,
604 GOT_OBJ_TYPE_COMMIT, repo);
605 if (error)
606 goto err;
609 if (qs->action != SUMMARY && qs->action != TAGS) {
610 error = got_object_open_as_commit(&commit, repo, id);
611 if (error)
612 goto err;
613 error = got_object_commit_get_logmsg(&commit_msg0, commit);
614 if (error)
615 goto err;
616 if (commit) {
617 got_object_commit_close(commit);
618 commit = NULL;
622 error = got_repo_map_path(&in_repo_path, repo, repo_path);
623 if (error)
624 goto err;
626 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
627 repo);
628 if (error)
629 goto err;
631 if (limit == 1)
632 chk_multi = 0;
634 /*
635 * XXX: again, see previous message about caching
636 */
638 TAILQ_FOREACH(re, &refs, entry) {
639 struct repo_tag *new_repo_tag = NULL;
640 error = got_init_repo_tag(&new_repo_tag);
641 if (error)
642 goto err;
644 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
646 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
647 if (new_repo_tag->tag_name == NULL) {
648 error = got_error_from_errno("strdup");
649 goto err;
652 error = got_ref_resolve(&id, repo, re->ref);
653 if (error)
654 goto done;
656 error = got_object_open_as_tag(&tag, repo, id);
657 if (error) {
658 if (error->code != GOT_ERR_OBJ_TYPE) {
659 free(id);
660 id = NULL;
661 goto done;
663 /* "lightweight" tag */
664 error = got_object_open_as_commit(&commit, repo, id);
665 if (error) {
666 free(id);
667 id = NULL;
668 goto done;
670 new_repo_tag->tagger =
671 strdup(got_object_commit_get_committer(commit));
672 if (new_repo_tag->tagger == NULL) {
673 error = got_error_from_errno("strdup");
674 goto err;
676 new_repo_tag->tagger_time =
677 got_object_commit_get_committer_time(commit);
678 error = got_object_id_str(&id_str, id);
679 if (error)
680 goto err;
681 free(id);
682 id = NULL;
683 } else {
684 free(id);
685 id = NULL;
686 new_repo_tag->tagger =
687 strdup(got_object_tag_get_tagger(tag));
688 if (new_repo_tag->tagger == NULL) {
689 error = got_error_from_errno("strdup");
690 goto err;
692 new_repo_tag->tagger_time =
693 got_object_tag_get_tagger_time(tag);
694 error = got_object_id_str(&id_str,
695 got_object_tag_get_object_id(tag));
696 if (error)
697 goto err;
700 new_repo_tag->commit_id = strdup(id_str);
701 if (new_repo_tag->commit_id == NULL)
702 goto err;
704 if (commit_found == 0 && qs->commit != NULL &&
705 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
706 continue;
707 else
708 commit_found = 1;
710 t->tag_count++;
712 /*
713 * check for one more commit before breaking,
714 * so we know whether to navigate through briefs
715 * commits and summary
716 */
717 if (chk_next) {
718 t->next_id = strdup(new_repo_tag->commit_id);
719 if (t->next_id == NULL) {
720 error = got_error_from_errno("strdup");
721 goto err;
723 if (commit) {
724 got_object_commit_close(commit);
725 commit = NULL;
727 if (t->next_id == NULL) {
728 error = got_error_from_errno("strdup");
729 goto err;
731 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
732 gotweb_free_repo_tag(new_repo_tag);
733 goto done;
736 if (commit) {
737 error = got_object_commit_get_logmsg(&new_repo_tag->
738 tag_commit, commit);
739 if (error)
740 goto done;
741 got_object_commit_close(commit);
742 commit = NULL;
743 } else {
744 new_repo_tag->tag_commit =
745 strdup(got_object_tag_get_message(tag));
746 if (new_repo_tag->tag_commit == NULL) {
747 error = got_error_from_errno("strdup");
748 goto done;
752 while (*new_repo_tag->tag_commit == '\n')
753 new_repo_tag->tag_commit++;
755 if (qs->action != SUMMARY && qs->action != TAGS) {
756 commit_msg = commit_msg0;
757 while (*commit_msg == '\n')
758 commit_msg++;
760 new_repo_tag->commit_msg = strdup(commit_msg);
761 if (new_repo_tag->commit_msg == NULL) {
762 error = got_error_from_errno("strdup");
763 free(commit_msg0);
764 goto err;
766 free(commit_msg0);
769 if (limit && --limit == 0) {
770 if (chk_multi == 0)
771 break;
772 chk_next = 1;
774 free(id);
775 id = NULL;
778 done:
779 /*
780 * we have tailq populated, so find previous commit id
781 * for navigation through briefs and commits
782 */
783 if (t->tag_count == 0) {
784 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
785 TAILQ_REMOVE(&t->repo_tags, rt, entry);
786 gotweb_free_repo_tag(rt);
789 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
790 commit_found = 0;
791 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
792 entry) {
793 if (commit_found == 0 && rt->commit_id != NULL &&
794 strcmp(qs->commit, rt->commit_id) != 0) {
795 continue;
796 } else
797 commit_found = 1;
798 if (c_cnt == srv->max_commits_display ||
799 rt == TAILQ_FIRST(&t->repo_tags)) {
800 t->prev_id = strdup(rt->commit_id);
801 if (t->prev_id == NULL)
802 error = got_error_from_errno("strdup");
803 break;
805 c_cnt++;
808 err:
809 if (commit)
810 got_object_commit_close(commit);
811 got_ref_list_free(&refs);
812 free(repo_path);
813 free(id);
814 return error;
817 const struct got_error *
818 got_output_repo_tree(struct request *c)
820 const struct got_error *error = NULL;
821 struct transport *t = c->t;
822 struct got_commit_object *commit = NULL;
823 struct got_repository *repo = t->repo;
824 struct querystring *qs = t->qs;
825 struct repo_commit *rc = NULL;
826 struct got_object_id *tree_id = NULL, *commit_id = NULL;
827 struct got_reflist_head refs;
828 struct got_tree_object *tree = NULL;
829 struct repo_dir *repo_dir = t->repo_dir;
830 const char *name, *index_page_str, *folder;
831 char *id_str = NULL, *escaped_name = NULL;
832 char *path = NULL, *in_repo_path = NULL, *modestr = NULL;
833 int nentries, i, r;
835 TAILQ_INIT(&refs);
837 rc = TAILQ_FIRST(&t->repo_commits);
839 if (qs->folder != NULL) {
840 path = strdup(qs->folder);
841 if (path == NULL) {
842 error = got_error_from_errno("strdup");
843 goto done;
845 } else {
846 error = got_repo_map_path(&in_repo_path, repo, repo_dir->path);
847 if (error)
848 goto done;
849 free(path);
850 path = in_repo_path;
853 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
854 GOT_OBJ_TYPE_COMMIT, &refs, repo);
855 if (error)
856 goto done;
858 error = got_object_open_as_commit(&commit, repo, commit_id);
859 if (error)
860 goto done;
862 error = got_object_id_by_path(&tree_id, repo, commit, path);
863 if (error)
864 goto done;
866 error = got_object_open_as_tree(&tree, repo, tree_id);
867 if (error)
868 goto done;
870 nentries = got_object_tree_get_nentries(tree);
872 index_page_str = qs->index_page_str ? qs->index_page_str : "";
873 folder = qs->folder ? qs->folder : "";
875 for (i = 0; i < nentries; i++) {
876 struct got_tree_entry *te;
877 mode_t mode;
879 te = got_object_tree_get_entry(tree, i);
881 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
882 if (error)
883 goto done;
885 modestr = strdup("");
886 if (modestr == NULL) {
887 error = got_error_from_errno("strdup");
888 goto done;
890 mode = got_tree_entry_get_mode(te);
891 if (got_object_tree_entry_is_submodule(te)) {
892 free(modestr);
893 modestr = strdup("$");
894 if (modestr == NULL) {
895 error = got_error_from_errno("strdup");
896 goto done;
898 } else if (S_ISLNK(mode)) {
899 free(modestr);
900 modestr = strdup("@");
901 if (modestr == NULL) {
902 error = got_error_from_errno("strdup");
903 goto done;
905 } else if (S_ISDIR(mode)) {
906 free(modestr);
907 modestr = strdup("/");
908 if (modestr == NULL) {
909 error = got_error_from_errno("strdup");
910 goto done;
912 } else if (mode & S_IXUSR) {
913 free(modestr);
914 modestr = strdup("*");
915 if (modestr == NULL) {
916 error = got_error_from_errno("strdup");
917 goto done;
921 name = got_tree_entry_get_name(te);
922 error = gotweb_escape_html(&escaped_name, name);
923 if (error)
924 goto done;
926 if (S_ISDIR(mode)) {
927 r = fcgi_printf(c,
928 "<div class='tree_wrapper'>\n"
929 "<div class='tree_line'>"
930 "<a class='diff_directory' "
931 "href='?index_page=%s&path=%s&action=tree"
932 "&commit=%s&folder=%s/%s'>%s%s</a>"
933 "</div>\n" /* .tree_line */
934 "<div class='tree_line_blank'>&nbsp;</div>\n"
935 "</div>\n", /* .tree_wrapper */
936 index_page_str, qs->path, rc->commit_id,
937 folder, name, escaped_name, modestr);
938 if (r == -1)
939 goto done;
940 } else {
941 r = fcgi_printf(c,
942 "<div class='tree_wrapper'>\n"
943 "<div class='tree_line'>"
944 "<a href='?index_page=%s&path=%s&action=blob"
945 "&commit=%s&folder=%s&file=%s'>%s%s</a>"
946 "</div>\n" /* .tree_line */
947 "<div class='tree_line_blank'>"
948 "<a href='?index_page=%s&path=%s&action=commits"
949 "&commit=%s&folder=%s&file=%s'>commits</a>\n"
950 " | \n"
951 "<a href='?index_page=%s&path=%s&action=blame"
952 "&commit=%s&folder=%s&file=%s'>blame</a>\n"
953 "</div>\n" /* .tree_line_blank */
954 "</div>\n", /* .tree_wrapper */
955 index_page_str, qs->path, rc->commit_id,
956 folder, name, escaped_name, modestr,
957 index_page_str, qs->path, rc->commit_id,
958 folder, name,
959 index_page_str, qs->path, rc->commit_id,
960 folder, name);
961 if (r == -1)
962 goto done;
964 free(id_str);
965 id_str = NULL;
966 free(modestr);
967 modestr = NULL;
968 free(escaped_name);
969 escaped_name = NULL;
971 done:
972 free(escaped_name);
973 free(id_str);
974 free(modestr);
975 free(path);
976 got_ref_list_free(&refs);
977 if (commit)
978 got_object_commit_close(commit);
979 free(commit_id);
980 free(tree_id);
981 return error;
984 const struct got_error *
985 got_output_file_blob(struct request *c)
987 const struct got_error *error = NULL;
988 struct transport *t = c->t;
989 struct got_repository *repo = t->repo;
990 struct querystring *qs = c->t->qs;
991 struct got_commit_object *commit = NULL;
992 struct got_object_id *commit_id = NULL;
993 struct got_reflist_head refs;
994 struct got_blob_object *blob = NULL;
995 char *path = NULL, *in_repo_path = NULL;
996 int obj_type, set_mime = 0, fd = -1;
997 size_t len, hdrlen;
998 const uint8_t *buf;
1000 TAILQ_INIT(&refs);
1002 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1003 qs->folder ? "/" : "", qs->file) == -1) {
1004 error = got_error_from_errno("asprintf");
1005 goto done;
1008 error = got_repo_map_path(&in_repo_path, repo, path);
1009 if (error)
1010 goto done;
1012 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1013 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1014 if (error)
1015 goto done;
1017 error = got_object_open_as_commit(&commit, repo, commit_id);
1018 if (error)
1019 goto done;
1021 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1022 if (error)
1023 goto done;
1025 if (commit_id == NULL) {
1026 error = got_error(GOT_ERR_NO_OBJ);
1027 goto done;
1030 error = got_object_get_type(&obj_type, repo, commit_id);
1031 if (error)
1032 goto done;
1034 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1035 error = got_error(GOT_ERR_OBJ_TYPE);
1036 goto done;
1039 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1040 if (error)
1041 goto done;
1043 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1044 if (error)
1045 goto done;
1046 hdrlen = got_object_blob_get_hdrlen(blob);
1047 do {
1048 error = got_object_blob_read_block(&len, blob);
1049 if (error)
1050 goto done;
1051 buf = got_object_blob_get_read_buf(blob);
1054 * Skip blob object header first time around,
1055 * which also contains a zero byte.
1057 buf += hdrlen;
1058 if (set_mime == 0) {
1059 if (isbinary(buf, len - hdrlen)) {
1060 error = gotweb_render_content_type_file(c,
1061 "application/octet-stream",
1062 qs->file);
1063 if (error) {
1064 log_warnx("%s: %s", __func__,
1065 error->msg);
1066 goto done;
1068 } else {
1069 error = gotweb_render_content_type(c,
1070 "text/plain");
1071 if (error) {
1072 log_warnx("%s: %s", __func__,
1073 error->msg);
1074 goto done;
1078 set_mime = 1;
1079 fcgi_gen_binary_response(c, buf, len - hdrlen);
1081 hdrlen = 0;
1082 } while (len != 0);
1083 done:
1084 if (commit)
1085 got_object_commit_close(commit);
1086 if (fd != -1 && close(fd) == -1 && error == NULL)
1087 error = got_error_from_errno("close");
1088 if (blob)
1089 got_object_blob_close(blob);
1090 free(in_repo_path);
1091 free(commit_id);
1092 free(path);
1093 return error;
1096 struct blame_line {
1097 int annotated;
1098 char *id_str;
1099 char *committer;
1100 char datebuf[11]; /* YYYY-MM-DD + NUL */
1103 struct blame_cb_args {
1104 struct blame_line *lines;
1105 int nlines;
1106 int nlines_prec;
1107 int lineno_cur;
1108 off_t *line_offsets;
1109 FILE *f;
1110 struct got_repository *repo;
1111 struct request *c;
1114 static const struct got_error *
1115 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1116 struct got_commit_object *commit, struct got_object_id *id)
1118 const struct got_error *err = NULL;
1119 struct blame_cb_args *a = arg;
1120 struct blame_line *bline;
1121 struct request *c = a->c;
1122 struct transport *t = c->t;
1123 struct querystring *qs = t->qs;
1124 struct repo_dir *repo_dir = t->repo_dir;
1125 const char *index_page_str;
1126 char *line = NULL, *eline = NULL;
1127 size_t linesize = 0;
1128 off_t offset;
1129 struct tm tm;
1130 time_t committer_time;
1131 int r;
1133 if (nlines != a->nlines ||
1134 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1135 return got_error(GOT_ERR_RANGE);
1137 if (lineno == -1)
1138 return NULL; /* no change in this commit */
1140 /* Annotate this line. */
1141 bline = &a->lines[lineno - 1];
1142 if (bline->annotated)
1143 return NULL;
1144 err = got_object_id_str(&bline->id_str, id);
1145 if (err)
1146 return err;
1148 bline->committer = strdup(got_object_commit_get_committer(commit));
1149 if (bline->committer == NULL) {
1150 err = got_error_from_errno("strdup");
1151 goto done;
1154 committer_time = got_object_commit_get_committer_time(commit);
1155 if (gmtime_r(&committer_time, &tm) == NULL)
1156 return got_error_from_errno("gmtime_r");
1157 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1158 &tm) == 0) {
1159 err = got_error(GOT_ERR_NO_SPACE);
1160 goto done;
1162 bline->annotated = 1;
1164 /* Print lines annotated so far. */
1165 bline = &a->lines[a->lineno_cur - 1];
1166 if (!bline->annotated)
1167 goto done;
1169 offset = a->line_offsets[a->lineno_cur - 1];
1170 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1171 err = got_error_from_errno("fseeko");
1172 goto done;
1175 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1177 while (a->lineno_cur <= a->nlines && bline->annotated) {
1178 char *smallerthan, *at, *nl, *committer;
1179 size_t len;
1181 if (getline(&line, &linesize, a->f) == -1) {
1182 if (ferror(a->f))
1183 err = got_error_from_errno("getline");
1184 break;
1187 committer = bline->committer;
1188 smallerthan = strchr(committer, '<');
1189 if (smallerthan && smallerthan[1] != '\0')
1190 committer = smallerthan + 1;
1191 at = strchr(committer, '@');
1192 if (at)
1193 *at = '\0';
1194 len = strlen(committer);
1195 if (len >= 9)
1196 committer[8] = '\0';
1198 nl = strchr(line, '\n');
1199 if (nl)
1200 *nl = '\0';
1202 err = gotweb_escape_html(&eline, line);
1203 if (err)
1204 goto done;
1206 r = fcgi_printf(c, "<div class='blame_wrapper'>"
1207 "<div class='blame_number'>%.*d</div>"
1208 "<div class='blame_hash'>"
1209 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1210 "%.8s</a>"
1211 "</div>"
1212 "<div class='blame_date'>%s</div>"
1213 "<div class='blame_author'>%s</div>"
1214 "<div class='blame_code'>%s</div>"
1215 "</div>", /* .blame_wrapper */
1216 a->nlines_prec, a->lineno_cur,
1217 index_page_str, repo_dir->name, bline->id_str,
1218 bline->id_str,
1219 bline->datebuf,
1220 committer,
1221 eline);
1222 if (r == -1)
1223 goto done;
1225 a->lineno_cur++;
1226 bline = &a->lines[a->lineno_cur - 1];
1228 done:
1229 free(line);
1230 free(eline);
1231 return err;
1234 const struct got_error *
1235 got_output_file_blame(struct request *c)
1237 const struct got_error *error = NULL;
1238 struct transport *t = c->t;
1239 struct got_repository *repo = t->repo;
1240 struct querystring *qs = c->t->qs;
1241 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1242 struct got_commit_object *commit = NULL;
1243 struct got_reflist_head refs;
1244 struct got_blob_object *blob = NULL;
1245 char *path = NULL, *in_repo_path = NULL;
1246 struct blame_cb_args bca;
1247 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1248 int fd6 = -1;
1249 off_t filesize;
1250 FILE *f1 = NULL, *f2 = NULL;
1252 TAILQ_INIT(&refs);
1253 bca.f = NULL;
1254 bca.lines = NULL;
1256 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1257 qs->folder ? "/" : "", qs->file) == -1) {
1258 error = got_error_from_errno("asprintf");
1259 goto done;
1262 error = got_repo_map_path(&in_repo_path, repo, path);
1263 if (error)
1264 goto done;
1266 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1267 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1268 if (error)
1269 goto done;
1271 error = got_object_open_as_commit(&commit, repo, commit_id);
1272 if (error)
1273 goto done;
1275 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1276 if (error)
1277 goto done;
1279 if (commit_id == NULL) {
1280 error = got_error(GOT_ERR_NO_OBJ);
1281 goto done;
1284 error = got_object_get_type(&obj_type, repo, obj_id);
1285 if (error)
1286 goto done;
1288 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1289 error = got_error(GOT_ERR_OBJ_TYPE);
1290 goto done;
1293 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1294 if (error)
1295 goto done;
1297 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1298 if (error)
1299 goto done;
1301 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1302 if (error)
1303 goto done;
1305 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1306 &bca.line_offsets, bca.f, blob);
1307 if (error || bca.nlines == 0)
1308 goto done;
1310 /* Don't include \n at EOF in the blame line count. */
1311 if (bca.line_offsets[bca.nlines - 1] == filesize)
1312 bca.nlines--;
1314 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1315 if (bca.lines == NULL) {
1316 error = got_error_from_errno("calloc");
1317 goto done;
1319 bca.lineno_cur = 1;
1320 bca.nlines_prec = 0;
1321 i = bca.nlines;
1322 while (i > 0) {
1323 i /= 10;
1324 bca.nlines_prec++;
1326 bca.repo = repo;
1327 bca.c = c;
1329 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1330 if (error)
1331 goto done;
1333 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1334 if (error)
1335 goto done;
1337 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1338 if (error)
1339 goto done;
1341 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1342 if (error)
1343 goto done;
1345 error = got_blame(in_repo_path, commit_id, repo,
1346 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1347 fd3, fd4, f1, f2);
1349 if (blob) {
1350 free(bca.line_offsets);
1351 for (i = 0; i < bca.nlines; i++) {
1352 struct blame_line *bline = &bca.lines[i];
1353 free(bline->id_str);
1354 free(bline->committer);
1357 done:
1358 free(bca.lines);
1359 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1360 error = got_error_from_errno("close");
1361 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1362 error = got_error_from_errno("close");
1363 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1364 error = got_error_from_errno("close");
1365 if (bca.f) {
1366 const struct got_error *bca_err =
1367 got_gotweb_flushfile(bca.f, fd1);
1368 if (error == NULL)
1369 error = bca_err;
1371 if (f1) {
1372 const struct got_error *f1_err =
1373 got_gotweb_flushfile(f1, fd5);
1374 if (error == NULL)
1375 error = f1_err;
1377 if (f2) {
1378 const struct got_error *f2_err =
1379 got_gotweb_flushfile(f2, fd6);
1380 if (error == NULL)
1381 error = f2_err;
1383 if (commit)
1384 got_object_commit_close(commit);
1385 if (blob)
1386 got_object_blob_close(blob);
1387 free(in_repo_path);
1388 free(commit_id);
1389 free(path);
1390 return error;
1393 const struct got_error *
1394 got_output_repo_diff(struct request *c)
1396 const struct got_error *error = NULL;
1397 struct transport *t = c->t;
1398 struct got_repository *repo = t->repo;
1399 struct repo_commit *rc = NULL;
1400 struct got_object_id *id1 = NULL, *id2 = NULL;
1401 struct got_reflist_head refs;
1402 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1403 char *label1 = NULL, *label2 = NULL, *line = NULL;
1404 char *newline, *eline = NULL, *color = NULL;
1405 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1406 size_t linesize = 0;
1407 ssize_t linelen;
1408 int wrlen = 0;
1410 TAILQ_INIT(&refs);
1412 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1413 if (error)
1414 return error;
1416 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1417 if (error)
1418 return error;
1420 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1421 if (error)
1422 return error;
1424 rc = TAILQ_FIRST(&t->repo_commits);
1426 if (rc->parent_id != NULL &&
1427 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1428 error = got_repo_match_object_id(&id1, &label1,
1429 rc->parent_id, GOT_OBJ_TYPE_ANY,
1430 &refs, repo);
1431 if (error)
1432 goto done;
1435 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1436 GOT_OBJ_TYPE_ANY, &refs, repo);
1437 if (error)
1438 goto done;
1440 error = got_object_get_type(&obj_type, repo, id2);
1441 if (error)
1442 goto done;
1444 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1445 if (error)
1446 goto done;
1448 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1449 if (error)
1450 goto done;
1452 switch (obj_type) {
1453 case GOT_OBJ_TYPE_BLOB:
1454 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1455 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1456 repo, f3);
1457 break;
1458 case GOT_OBJ_TYPE_TREE:
1459 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1460 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1461 repo, f3);
1462 break;
1463 case GOT_OBJ_TYPE_COMMIT:
1464 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1465 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1466 repo, f3);
1467 break;
1468 default:
1469 error = got_error(GOT_ERR_OBJ_TYPE);
1471 if (error)
1472 goto done;
1474 if (fseek(f1, 0, SEEK_SET) == -1) {
1475 error = got_ferror(f1, GOT_ERR_IO);
1476 goto done;
1479 if (fseek(f2, 0, SEEK_SET) == -1) {
1480 error = got_ferror(f2, GOT_ERR_IO);
1481 goto done;
1484 if (fseek(f3, 0, SEEK_SET) == -1) {
1485 error = got_ferror(f3, GOT_ERR_IO);
1486 goto done;
1489 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1490 if (strncmp(line, "-", 1) == 0) {
1491 color = strdup("diff_minus");
1492 if (color == NULL) {
1493 error = got_error_from_errno("strdup");
1494 goto done;
1496 } else if (strncmp(line, "+", 1) == 0) {
1497 color = strdup("diff_plus");
1498 if (color == NULL) {
1499 error = got_error_from_errno("strdup");
1500 goto done;
1502 } else if (strncmp(line, "@@", 2) == 0) {
1503 color = strdup("diff_chunk_header");
1504 if (color == NULL) {
1505 error = got_error_from_errno("strdup");
1506 goto done;
1508 } else if (strncmp(line, "@@", 2) == 0) {
1509 color = strdup("diff_chunk_header");
1510 if (color == NULL) {
1511 error = got_error_from_errno("strdup");
1512 goto done;
1514 } else if (strncmp(line, "commit +", 8) == 0) {
1515 color = strdup("diff_meta");
1516 if (color == NULL) {
1517 error = got_error_from_errno("strdup");
1518 goto done;
1520 } else if (strncmp(line, "commit -", 8) == 0) {
1521 color = strdup("diff_meta");
1522 if (color == NULL) {
1523 error = got_error_from_errno("strdup");
1524 goto done;
1526 } else if (strncmp(line, "blob +", 6) == 0) {
1527 color = strdup("diff_meta");
1528 if (color == NULL) {
1529 error = got_error_from_errno("strdup");
1530 goto done;
1532 } else if (strncmp(line, "blob -", 6) == 0) {
1533 color = strdup("diff_meta");
1534 if (color == NULL) {
1535 error = got_error_from_errno("strdup");
1536 goto done;
1538 } else if (strncmp(line, "file +", 6) == 0) {
1539 color = strdup("diff_meta");
1540 if (color == NULL) {
1541 error = got_error_from_errno("strdup");
1542 goto done;
1544 } else if (strncmp(line, "file -", 6) == 0) {
1545 color = strdup("diff_meta");
1546 if (color == NULL) {
1547 error = got_error_from_errno("strdup");
1548 goto done;
1550 } else if (strncmp(line, "from:", 5) == 0) {
1551 color = strdup("diff_author");
1552 if (color == NULL) {
1553 error = got_error_from_errno("strdup");
1554 goto done;
1556 } else if (strncmp(line, "via:", 4) == 0) {
1557 color = strdup("diff_author");
1558 if (color == NULL) {
1559 error = got_error_from_errno("strdup");
1560 goto done;
1562 } else if (strncmp(line, "date:", 5) == 0) {
1563 color = strdup("diff_date");
1564 if (color == NULL) {
1565 error = got_error_from_errno("strdup");
1566 goto done;
1570 newline = strchr(line, '\n');
1571 if (newline)
1572 *newline = '\0';
1574 error = gotweb_escape_html(&eline, line);
1575 if (error)
1576 goto done;
1578 fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1579 color ? color : "", eline);
1580 free(eline);
1581 eline = NULL;
1583 if (linelen > 0)
1584 wrlen = wrlen + linelen;
1585 free(color);
1586 color = NULL;
1588 if (linelen == -1 && ferror(f3))
1589 error = got_error_from_errno("getline");
1590 done:
1591 free(color);
1592 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1593 error = got_error_from_errno("close");
1594 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1595 error = got_error_from_errno("close");
1596 if (f1) {
1597 const struct got_error *f1_err =
1598 got_gotweb_flushfile(f1, fd1);
1599 if (error == NULL)
1600 error = f1_err;
1602 if (f2) {
1603 const struct got_error *f2_err =
1604 got_gotweb_flushfile(f2, fd2);
1605 if (error == NULL)
1606 error = f2_err;
1608 if (f3) {
1609 const struct got_error *f3_err =
1610 got_gotweb_flushfile(f3, fd3);
1611 if (error == NULL)
1612 error = f3_err;
1614 got_ref_list_free(&refs);
1615 free(line);
1616 free(eline);
1617 free(label1);
1618 free(label2);
1619 free(id1);
1620 free(id2);
1621 return error;
1624 static const struct got_error *
1625 got_init_repo_commit(struct repo_commit **rc)
1627 *rc = calloc(1, sizeof(**rc));
1628 if (*rc == NULL)
1629 return got_error_from_errno2("%s: calloc", __func__);
1631 (*rc)->path = NULL;
1632 (*rc)->refs_str = NULL;
1633 (*rc)->commit_id = NULL;
1634 (*rc)->committer = NULL;
1635 (*rc)->author = NULL;
1636 (*rc)->parent_id = NULL;
1637 (*rc)->tree_id = NULL;
1638 (*rc)->commit_msg = NULL;
1640 return NULL;
1643 static const struct got_error *
1644 got_init_repo_tag(struct repo_tag **rt)
1646 *rt = calloc(1, sizeof(**rt));
1647 if (*rt == NULL)
1648 return got_error_from_errno2("%s: calloc", __func__);
1650 (*rt)->commit_id = NULL;
1651 (*rt)->tag_name = NULL;
1652 (*rt)->tag_commit = NULL;
1653 (*rt)->commit_msg = NULL;
1654 (*rt)->tagger = NULL;
1656 return NULL;