Blob


1 /*
2 * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
22 #include <event.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_reference.h"
31 #include "got_repository.h"
32 #include "got_path.h"
33 #include "got_cancel.h"
34 #include "got_diff.h"
35 #include "got_commit_graph.h"
36 #include "got_blame.h"
37 #include "got_privsep.h"
39 #include "got_compat.h"
41 #include "proc.h"
42 #include "gotwebd.h"
44 static const struct got_error *got_init_repo_commit(struct repo_commit **);
45 static const struct got_error *got_init_repo_tag(struct repo_tag **);
46 static const struct got_error *got_get_repo_commit(struct request *,
47 struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
48 struct got_object_id *);
49 static const struct got_error *got_gotweb_dupfd(int *, int *);
50 static const struct got_error *got_gotweb_openfile(FILE **, int *, int *);
51 static const struct got_error *got_gotweb_flushfile(FILE *, int);
52 static const struct got_error *got_gotweb_blame_cb(void *, int, int,
53 struct got_commit_object *,struct got_object_id *);
55 static int
56 isbinary(const uint8_t *buf, size_t n)
57 {
58 size_t i;
60 for (i = 0; i < n; i++)
61 if (buf[i] == 0)
62 return 1;
63 return 0;
64 }
67 static const struct got_error *
68 got_gotweb_flushfile(FILE *f, int fd)
69 {
70 if (fseek(f, 0, SEEK_SET) == -1)
71 return got_error_from_errno("fseek");
73 if (ftruncate(fd, 0) == -1)
74 return got_error_from_errno("ftruncate");
76 if (fsync(fd) == -1)
77 return got_error_from_errno("fsync");
79 if (f && fclose(f) == EOF)
80 return got_error_from_errno("fclose");
82 if (fd != -1 && close(fd) != -1)
83 return got_error_from_errno("close");
85 return NULL;
86 }
88 static const struct got_error *
89 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
90 {
91 const struct got_error *error = NULL;
93 *fd = dup(*priv_fd);
95 if (*fd < 0)
96 return NULL;
98 *f = fdopen(*fd, "w+");
99 if (*f == NULL) {
100 close(*fd);
101 error = got_error(GOT_ERR_PRIVSEP_NO_FD);
104 return error;
107 static const struct got_error *
108 got_gotweb_dupfd(int *priv_fd, int *fd)
110 const struct got_error *error = NULL;
112 *fd = dup(*priv_fd);
114 if (*fd < 0)
115 return NULL;
117 return error;
120 const struct got_error *
121 got_get_repo_owner(char **owner, struct request *c, char *dir)
123 const struct got_error *error = NULL;
124 struct server *srv = c->srv;
125 struct transport *t = c->t;
126 struct got_repository *repo = t->repo;
127 const char *gitconfig_owner;
129 *owner = NULL;
131 if (srv->show_repo_owner == 0)
132 return NULL;
134 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
135 if (gitconfig_owner) {
136 *owner = strdup(gitconfig_owner);
137 if (*owner == NULL)
138 return got_error_from_errno("strdup");
140 return error;
143 const struct got_error *
144 got_get_repo_age(char **repo_age, struct request *c, char *dir,
145 const char *refname, int ref_tm)
147 const struct got_error *error = NULL;
148 struct server *srv = c->srv;
149 struct transport *t = c->t;
150 struct got_repository *repo = t->repo;
151 struct got_commit_object *commit = NULL;
152 struct got_reflist_head refs;
153 struct got_reflist_entry *re;
154 time_t committer_time = 0, cmp_time = 0;
156 *repo_age = NULL;
157 TAILQ_INIT(&refs);
159 if (srv->show_repo_age == 0)
160 return NULL;
162 error = got_ref_list(&refs, repo, "refs/heads",
163 got_ref_cmp_by_name, NULL);
164 if (error)
165 goto done;
167 /*
168 * Find the youngest branch tip in the repository, or the age of
169 * the a specific branch tip if a name was provided by the caller.
170 */
171 TAILQ_FOREACH(re, &refs, entry) {
172 struct got_object_id *id = NULL;
174 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
175 continue;
177 error = got_ref_resolve(&id, repo, re->ref);
178 if (error)
179 goto done;
181 error = got_object_open_as_commit(&commit, repo, id);
182 free(id);
183 if (error)
184 goto done;
186 committer_time =
187 got_object_commit_get_committer_time(commit);
188 got_object_commit_close(commit);
189 if (cmp_time < committer_time)
190 cmp_time = committer_time;
192 if (refname)
193 break;
196 if (cmp_time != 0) {
197 committer_time = cmp_time;
198 error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
200 done:
201 got_ref_list_free(&refs);
202 return error;
205 static const struct got_error *
206 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
207 struct got_commit_object *commit, struct got_reflist_head *refs,
208 struct got_object_id *id)
210 const struct got_error *error = NULL;
211 struct got_reflist_entry *re;
212 struct got_object_id *id2 = NULL;
213 struct got_object_qid *parent_id;
214 struct transport *t = c->t;
215 struct querystring *qs = c->t->qs;
216 char *commit_msg = NULL, *commit_msg0;
218 TAILQ_FOREACH(re, refs, entry) {
219 char *s;
220 const char *name;
221 struct got_tag_object *tag = NULL;
222 struct got_object_id *ref_id;
223 int cmp;
225 if (got_ref_is_symbolic(re->ref))
226 continue;
228 name = got_ref_get_name(re->ref);
229 if (strncmp(name, "refs/", 5) == 0)
230 name += 5;
231 if (strncmp(name, "got/", 4) == 0)
232 continue;
233 if (strncmp(name, "heads/", 6) == 0)
234 name += 6;
235 if (strncmp(name, "remotes/", 8) == 0) {
236 name += 8;
237 s = strstr(name, "/" GOT_REF_HEAD);
238 if (s != NULL && s[strlen(s)] == '\0')
239 continue;
241 error = got_ref_resolve(&ref_id, t->repo, re->ref);
242 if (error)
243 return error;
244 if (strncmp(name, "tags/", 5) == 0) {
245 error = got_object_open_as_tag(&tag, t->repo, ref_id);
246 if (error) {
247 if (error->code != GOT_ERR_OBJ_TYPE) {
248 free(ref_id);
249 continue;
251 /*
252 * Ref points at something other
253 * than a tag.
254 */
255 error = NULL;
256 tag = NULL;
259 cmp = got_object_id_cmp(tag ?
260 got_object_tag_get_object_id(tag) : ref_id, id);
261 free(ref_id);
262 if (tag)
263 got_object_tag_close(tag);
264 if (cmp != 0)
265 continue;
266 s = repo_commit->refs_str;
267 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
268 s ? ", " : "", name) == -1) {
269 error = got_error_from_errno("asprintf");
270 free(s);
271 repo_commit->refs_str = NULL;
272 return error;
274 free(s);
277 error = got_object_id_str(&repo_commit->commit_id, id);
278 if (error)
279 return error;
281 error = got_object_id_str(&repo_commit->tree_id,
282 got_object_commit_get_tree_id(commit));
283 if (error)
284 return error;
286 if (qs->action == DIFF) {
287 parent_id = STAILQ_FIRST(
288 got_object_commit_get_parent_ids(commit));
289 if (parent_id != NULL) {
290 id2 = got_object_id_dup(&parent_id->id);
291 error = got_object_id_str(&repo_commit->parent_id, id2);
292 if (error)
293 return error;
294 free(id2);
295 } else {
296 repo_commit->parent_id = strdup("/dev/null");
297 if (repo_commit->parent_id == NULL) {
298 error = got_error_from_errno("strdup");
299 return error;
304 repo_commit->committer_time =
305 got_object_commit_get_committer_time(commit);
307 repo_commit->author =
308 strdup(got_object_commit_get_author(commit));
309 if (repo_commit->author == NULL) {
310 error = got_error_from_errno("strdup");
311 return error;
313 repo_commit->committer =
314 strdup(got_object_commit_get_committer(commit));
315 if (repo_commit->committer == NULL) {
316 error = got_error_from_errno("strdup");
317 return error;
319 error = got_object_commit_get_logmsg(&commit_msg0, commit);
320 if (error)
321 return error;
323 commit_msg = commit_msg0;
324 while (*commit_msg == '\n')
325 commit_msg++;
327 repo_commit->commit_msg = strdup(commit_msg);
328 if (repo_commit->commit_msg == NULL)
329 error = got_error_from_errno("strdup");
330 free(commit_msg0);
331 return error;
334 const struct got_error *
335 got_get_repo_commits(struct request *c, int limit)
337 const struct got_error *error = NULL;
338 struct got_object_id *id = NULL;
339 struct got_commit_graph *graph = NULL;
340 struct got_commit_object *commit = NULL;
341 struct got_reflist_head refs;
342 struct got_reference *ref;
343 struct repo_commit *repo_commit = NULL;
344 struct server *srv = c->srv;
345 struct transport *t = c->t;
346 struct got_repository *repo = t->repo;
347 struct querystring *qs = t->qs;
348 struct repo_dir *repo_dir = t->repo_dir;
349 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
350 int chk_next = 0, chk_multi = 0, commit_found = 0;
351 int obj_type, limit_chk = 0;
353 TAILQ_INIT(&refs);
355 if (qs->file != NULL && strlen(qs->file) > 0)
356 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
357 qs->file) == -1)
358 return got_error_from_errno("asprintf");
360 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
361 repo_dir->name) == -1)
362 return got_error_from_errno("asprintf");
364 error = got_init_repo_commit(&repo_commit);
365 if (error)
366 return error;
368 /*
369 * XXX: jumping directly to a commit id via
370 * got_repo_match_object_id_prefix significantly improves performance,
371 * but does not allow us to create a PREVIOUS button, since commits can
372 * only be itereated forward. So, we have to match as we iterate from
373 * the headref.
374 */
375 if (qs->action == BRIEFS || qs->action == COMMITS ||
376 (qs->action == TREE && qs->commit == NULL)) {
377 error = got_ref_open(&ref, repo, qs->headref, 0);
378 if (error)
379 goto done;
381 error = got_ref_resolve(&id, repo, ref);
382 got_ref_close(ref);
383 if (error)
384 goto done;
385 } else if (qs->commit != NULL) {
386 error = got_ref_open(&ref, repo, qs->commit, 0);
387 if (error == NULL) {
388 error = got_ref_resolve(&id, repo, ref);
389 if (error)
390 goto done;
391 error = got_object_get_type(&obj_type, repo, id);
392 got_ref_close(ref);
393 if (error)
394 goto done;
395 if (obj_type == GOT_OBJ_TYPE_TAG) {
396 struct got_tag_object *tag;
397 error = got_object_open_as_tag(&tag, repo, id);
398 if (error)
399 goto done;
400 if (got_object_tag_get_object_type(tag) !=
401 GOT_OBJ_TYPE_COMMIT) {
402 got_object_tag_close(tag);
403 error = got_error(GOT_ERR_OBJ_TYPE);
404 goto done;
406 free(id);
407 id = got_object_id_dup(
408 got_object_tag_get_object_id(tag));
409 if (id == NULL)
410 error = got_error_from_errno(
411 "got_object_id_dup");
412 got_object_tag_close(tag);
413 if (error)
414 goto done;
415 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
416 error = got_error(GOT_ERR_OBJ_TYPE);
417 goto done;
420 error = got_repo_match_object_id_prefix(&id, qs->commit,
421 GOT_OBJ_TYPE_COMMIT, repo);
422 if (error)
423 goto done;
426 error = got_repo_map_path(&in_repo_path, repo, repo_path);
427 if (error)
428 goto done;
430 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
431 if (error)
432 goto done;
434 if (qs->file != NULL && strlen(qs->file) > 0) {
435 error = got_commit_graph_open(&graph, file_path, 0);
436 if (error)
437 goto done;
438 } else {
439 error = got_commit_graph_open(&graph, in_repo_path, 0);
440 if (error)
441 goto done;
444 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
445 if (error)
446 goto done;
448 for (;;) {
449 if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
450 commit_found == 0 && repo_commit->commit_id != NULL) {
451 t->prev_id = strdup(repo_commit->commit_id);
452 if (t->prev_id == NULL) {
453 error = got_error_from_errno("strdup");
454 goto done;
458 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
459 NULL);
460 if (error) {
461 if (error->code == GOT_ERR_ITER_COMPLETED)
462 error = NULL;
463 goto done;
465 if (id == NULL)
466 goto done;
468 error = got_object_open_as_commit(&commit, repo, id);
469 if (error)
470 goto done;
472 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
473 NULL);
474 if (error)
475 goto done;
477 error = got_get_repo_commit(c, repo_commit, commit,
478 &refs, id);
479 if (error)
480 goto done;
482 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
483 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
484 commit_found = 1;
485 else if (qs->file != NULL && strlen(qs->file) > 0 &&
486 qs->page == 0)
487 commit_found = 1;
488 else {
489 limit_chk++;
490 free(id);
491 id = NULL;
492 continue;
496 struct repo_commit *new_repo_commit = NULL;
497 error = got_init_repo_commit(&new_repo_commit);
498 if (error)
499 goto done;
501 TAILQ_INSERT_TAIL(&t->repo_commits, new_repo_commit, entry);
503 error = got_get_repo_commit(c, new_repo_commit, commit,
504 &refs, id);
505 if (error)
506 goto done;
508 free(id);
509 id = NULL;
511 if (limit == 1 && chk_multi == 0 &&
512 srv->max_commits_display != 1)
513 commit_found = 1;
514 else {
515 chk_multi = 1;
517 /*
518 * check for one more commit before breaking,
519 * so we know whether to navigate through briefs
520 * commits and summary
521 */
522 if (chk_next && (qs->action == BRIEFS ||
523 qs->action == COMMITS || qs->action == SUMMARY)) {
524 t->next_id = strdup(new_repo_commit->commit_id);
525 if (t->next_id == NULL) {
526 error = got_error_from_errno("strdup");
527 goto done;
529 if (commit) {
530 got_object_commit_close(commit);
531 commit = NULL;
533 if (t->next_id == NULL) {
534 error = got_error_from_errno("strdup");
535 goto done;
537 TAILQ_REMOVE(&t->repo_commits, new_repo_commit,
538 entry);
539 gotweb_free_repo_commit(new_repo_commit);
540 goto done;
543 got_ref_list_free(&refs);
544 if (error || (limit && --limit == 0)) {
545 if (commit_found || (qs->file != NULL &&
546 strlen(qs->file) > 0))
547 if (chk_multi == 0)
548 break;
549 chk_next = 1;
551 if (commit) {
552 got_object_commit_close(commit);
553 commit = NULL;
556 done:
557 gotweb_free_repo_commit(repo_commit);
558 if (commit)
559 got_object_commit_close(commit);
560 if (graph)
561 got_commit_graph_close(graph);
562 got_ref_list_free(&refs);
563 free(file_path);
564 free(repo_path);
565 free(id);
566 return error;
569 const struct got_error *
570 got_get_repo_tags(struct request *c, int limit)
572 const struct got_error *error = NULL;
573 struct got_object_id *id = NULL;
574 struct got_commit_object *commit = NULL;
575 struct got_reflist_head refs;
576 struct got_reference *ref;
577 struct got_reflist_entry *re;
578 struct server *srv = c->srv;
579 struct transport *t = c->t;
580 struct got_repository *repo = t->repo;
581 struct querystring *qs = t->qs;
582 struct repo_dir *repo_dir = t->repo_dir;
583 struct got_tag_object *tag = NULL;
584 struct repo_tag *rt = NULL, *trt = NULL;
585 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
586 char *commit_msg = NULL, *commit_msg0 = NULL;
587 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
589 TAILQ_INIT(&refs);
591 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
592 repo_dir->name) == -1)
593 return got_error_from_errno("asprintf");
595 if (error)
596 return error;
598 if (qs->commit == NULL && qs->action == TAGS) {
599 error = got_ref_open(&ref, repo, qs->headref, 0);
600 if (error)
601 goto err;
602 error = got_ref_resolve(&id, repo, ref);
603 got_ref_close(ref);
604 if (error)
605 goto err;
606 } else if (qs->commit == NULL && qs->action == TAG) {
607 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
608 goto err;
609 } else {
610 error = got_repo_match_object_id_prefix(&id, qs->commit,
611 GOT_OBJ_TYPE_COMMIT, repo);
612 if (error)
613 goto err;
616 if (qs->action != SUMMARY && qs->action != TAGS) {
617 error = got_object_open_as_commit(&commit, repo, id);
618 if (error)
619 goto err;
620 error = got_object_commit_get_logmsg(&commit_msg0, commit);
621 if (error)
622 goto err;
623 if (commit) {
624 got_object_commit_close(commit);
625 commit = NULL;
629 error = got_repo_map_path(&in_repo_path, repo, repo_path);
630 if (error)
631 goto err;
633 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
634 repo);
635 if (error)
636 goto err;
638 if (limit == 1)
639 chk_multi = 0;
641 /*
642 * XXX: again, see previous message about caching
643 */
645 TAILQ_FOREACH(re, &refs, entry) {
646 struct repo_tag *new_repo_tag = NULL;
647 error = got_init_repo_tag(&new_repo_tag);
648 if (error)
649 goto err;
651 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
653 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
654 if (new_repo_tag->tag_name == NULL) {
655 error = got_error_from_errno("strdup");
656 goto err;
659 error = got_ref_resolve(&id, repo, re->ref);
660 if (error)
661 goto done;
663 error = got_object_open_as_tag(&tag, repo, id);
664 if (error) {
665 if (error->code != GOT_ERR_OBJ_TYPE) {
666 free(id);
667 id = NULL;
668 goto done;
670 /* "lightweight" tag */
671 error = got_object_open_as_commit(&commit, repo, id);
672 if (error) {
673 free(id);
674 id = NULL;
675 goto done;
677 new_repo_tag->tagger =
678 strdup(got_object_commit_get_committer(commit));
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_commit_get_committer_time(commit);
685 error = got_object_id_str(&id_str, id);
686 if (error)
687 goto err;
688 free(id);
689 id = NULL;
690 } else {
691 free(id);
692 id = NULL;
693 new_repo_tag->tagger =
694 strdup(got_object_tag_get_tagger(tag));
695 if (new_repo_tag->tagger == NULL) {
696 error = got_error_from_errno("strdup");
697 goto err;
699 new_repo_tag->tagger_time =
700 got_object_tag_get_tagger_time(tag);
701 error = got_object_id_str(&id_str,
702 got_object_tag_get_object_id(tag));
703 if (error)
704 goto err;
707 new_repo_tag->commit_id = strdup(id_str);
708 if (new_repo_tag->commit_id == NULL)
709 goto err;
711 if (commit_found == 0 && qs->commit != NULL &&
712 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
713 continue;
714 else
715 commit_found = 1;
717 t->tag_count++;
719 /*
720 * check for one more commit before breaking,
721 * so we know whether to navigate through briefs
722 * commits and summary
723 */
724 if (chk_next) {
725 t->next_id = strdup(new_repo_tag->commit_id);
726 if (t->next_id == NULL) {
727 error = got_error_from_errno("strdup");
728 goto err;
730 if (commit) {
731 got_object_commit_close(commit);
732 commit = NULL;
734 if (t->next_id == NULL) {
735 error = got_error_from_errno("strdup");
736 goto err;
738 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
739 gotweb_free_repo_tag(new_repo_tag);
740 goto done;
743 if (commit) {
744 error = got_object_commit_get_logmsg(&new_repo_tag->
745 tag_commit, commit);
746 if (error)
747 goto done;
748 got_object_commit_close(commit);
749 commit = NULL;
750 } else {
751 new_repo_tag->tag_commit =
752 strdup(got_object_tag_get_message(tag));
753 if (new_repo_tag->tag_commit == NULL) {
754 error = got_error_from_errno("strdup");
755 goto done;
759 while (*new_repo_tag->tag_commit == '\n')
760 new_repo_tag->tag_commit++;
762 if (qs->action != SUMMARY && qs->action != TAGS) {
763 commit_msg = commit_msg0;
764 while (*commit_msg == '\n')
765 commit_msg++;
767 new_repo_tag->commit_msg = strdup(commit_msg);
768 if (new_repo_tag->commit_msg == NULL) {
769 error = got_error_from_errno("strdup");
770 free(commit_msg0);
771 goto err;
773 free(commit_msg0);
776 if (limit && --limit == 0) {
777 if (chk_multi == 0)
778 break;
779 chk_next = 1;
781 free(id);
782 id = NULL;
785 done:
786 /*
787 * we have tailq populated, so find previous commit id
788 * for navigation through briefs and commits
789 */
790 if (t->tag_count == 0) {
791 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
792 TAILQ_REMOVE(&t->repo_tags, rt, entry);
793 gotweb_free_repo_tag(rt);
796 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
797 commit_found = 0;
798 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
799 entry) {
800 if (commit_found == 0 && rt->commit_id != NULL &&
801 strcmp(qs->commit, rt->commit_id) != 0) {
802 continue;
803 } else
804 commit_found = 1;
805 if (c_cnt == srv->max_commits_display ||
806 rt == TAILQ_FIRST(&t->repo_tags)) {
807 t->prev_id = strdup(rt->commit_id);
808 if (t->prev_id == NULL)
809 error = got_error_from_errno("strdup");
810 break;
812 c_cnt++;
815 err:
816 if (commit)
817 got_object_commit_close(commit);
818 got_ref_list_free(&refs);
819 free(repo_path);
820 free(id);
821 return error;
824 const struct got_error *
825 got_output_repo_tree(struct request *c)
827 const struct got_error *error = NULL;
828 struct transport *t = c->t;
829 struct got_commit_object *commit = NULL;
830 struct got_repository *repo = t->repo;
831 struct querystring *qs = t->qs;
832 struct repo_commit *rc = NULL;
833 struct got_object_id *tree_id = NULL, *commit_id = NULL;
834 struct got_reflist_head refs;
835 struct got_tree_object *tree = NULL;
836 struct repo_dir *repo_dir = t->repo_dir;
837 char *id_str = NULL;
838 char *path = NULL, *in_repo_path = NULL, *build_folder = NULL;
839 char *modestr = NULL, *name = NULL, *class = NULL;
840 int nentries, i, class_flip = 0;
842 TAILQ_INIT(&refs);
844 rc = TAILQ_FIRST(&t->repo_commits);
846 if (qs->folder != NULL) {
847 path = strdup(qs->folder);
848 if (path == NULL) {
849 error = got_error_from_errno("strdup");
850 goto done;
852 } else {
853 error = got_repo_map_path(&in_repo_path, repo, repo_dir->path);
854 if (error)
855 goto done;
856 free(path);
857 path = in_repo_path;
860 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
861 GOT_OBJ_TYPE_COMMIT, &refs, repo);
862 if (error)
863 goto done;
865 error = got_object_open_as_commit(&commit, repo, commit_id);
866 if (error)
867 goto done;
869 error = got_object_id_by_path(&tree_id, repo, commit, path);
870 if (error)
871 goto done;
873 error = got_object_open_as_tree(&tree, repo, tree_id);
874 if (error)
875 goto done;
877 nentries = got_object_tree_get_nentries(tree);
879 for (i = 0; i < nentries; i++) {
880 struct got_tree_entry *te;
881 mode_t mode;
883 te = got_object_tree_get_entry(tree, i);
885 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
886 if (error)
887 goto done;
889 modestr = strdup("");
890 if (modestr == NULL) {
891 error = got_error_from_errno("strdup");
892 goto done;
894 mode = got_tree_entry_get_mode(te);
895 if (got_object_tree_entry_is_submodule(te)) {
896 free(modestr);
897 modestr = strdup("$");
898 if (modestr == NULL) {
899 error = got_error_from_errno("strdup");
900 goto done;
902 } else if (S_ISLNK(mode)) {
903 free(modestr);
904 modestr = strdup("@");
905 if (modestr == NULL) {
906 error = got_error_from_errno("strdup");
907 goto done;
909 } else if (S_ISDIR(mode)) {
910 free(modestr);
911 modestr = strdup("/");
912 if (modestr == NULL) {
913 error = got_error_from_errno("strdup");
914 goto done;
916 } else if (mode & S_IXUSR) {
917 free(modestr);
918 modestr = strdup("*");
919 if (modestr == NULL) {
920 error = got_error_from_errno("strdup");
921 goto done;
925 if (class_flip == 0) {
926 class = strdup("back_lightgray");
927 if (class == NULL) {
928 error = got_error_from_errno("strdup");
929 goto done;
931 class_flip = 1;
932 } else {
933 class = strdup("back_white");
934 if (class == NULL) {
935 error = got_error_from_errno("strdup");
936 goto done;
938 class_flip = 0;
941 name = strdup(got_tree_entry_get_name(te));
942 if (name == NULL) {
943 error = got_error_from_errno("strdup");
944 goto done;
946 if (S_ISDIR(mode)) {
947 if (asprintf(&build_folder, "%s/%s",
948 qs->folder ? qs->folder : "",
949 got_tree_entry_get_name(te)) == -1) {
950 error = got_error_from_errno("asprintf");
951 goto done;
954 if (fcgi_gen_response(c,
955 "<div id='tree_wrapper'>\n") == -1)
956 goto done;
958 if (fcgi_gen_response(c, "<div id='tree_line' "
959 "class='") == -1)
960 goto done;
961 if (fcgi_gen_response(c, class) == -1)
962 goto done;
963 if (fcgi_gen_response(c, "'>") == -1)
964 goto done;
966 if (fcgi_gen_response(c, "<a class='diff_directory' "
967 "href='?index_page=") == -1)
968 goto done;
969 if (fcgi_gen_response(c, qs->index_page_str) == -1)
970 goto done;
971 if (fcgi_gen_response(c, "&path=") == -1)
972 goto done;
973 if (fcgi_gen_response(c, qs->path) == -1)
974 goto done;
975 if (fcgi_gen_response(c, "&action=tree") == -1)
976 goto done;
977 if (fcgi_gen_response(c, "&commit=") == -1)
978 goto done;
979 if (fcgi_gen_response(c, rc->commit_id) == -1)
980 goto done;
981 if (fcgi_gen_response(c, "&folder=") == -1)
982 goto done;
983 if (fcgi_gen_response(c, build_folder) == -1)
984 goto done;
985 if (fcgi_gen_response(c, "'>") == -1)
986 goto done;
987 if (fcgi_gen_response(c, name) == -1)
988 goto done;
989 if (fcgi_gen_response(c, modestr) == -1)
990 goto done;
991 if (fcgi_gen_response(c, "</a>") == -1)
992 goto done;
994 if (fcgi_gen_response(c, "</div>\n") == -1)
995 goto done;
997 if (fcgi_gen_response(c, "<div id='tree_line_blank' "
998 "class='") == -1)
999 goto done;
1000 if (fcgi_gen_response(c, class) == -1)
1001 goto done;
1002 if (fcgi_gen_response(c, "'>") == -1)
1003 goto done;
1004 if (fcgi_gen_response(c, "&nbsp;") == -1)
1005 goto done;
1006 if (fcgi_gen_response(c, "</div>\n") == -1)
1007 goto done;
1009 if (fcgi_gen_response(c, "</div>\n") == -1)
1010 goto done;
1012 } else {
1013 free(name);
1014 name = strdup(got_tree_entry_get_name(te));
1015 if (name == NULL) {
1016 error = got_error_from_errno("strdup");
1017 goto done;
1020 if (fcgi_gen_response(c,
1021 "<div id='tree_wrapper'>\n") == -1)
1022 goto done;
1023 if (fcgi_gen_response(c, "<div id='tree_line' "
1024 "class='") == -1)
1025 goto done;
1026 if (fcgi_gen_response(c, class) == -1)
1027 goto done;
1028 if (fcgi_gen_response(c, "'>") == -1)
1029 goto done;
1031 if (fcgi_gen_response(c,
1032 "<a href='?index_page=") == -1)
1033 goto done;
1035 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1036 goto done;
1038 if (fcgi_gen_response(c, "&path=") == -1)
1039 goto done;
1040 if (fcgi_gen_response(c, qs->path) == -1)
1041 goto done;
1043 if (fcgi_gen_response(c, "&action=blob") == -1)
1044 goto done;
1046 if (fcgi_gen_response(c, "&commit=") == -1)
1047 goto done;
1048 if (fcgi_gen_response(c, rc->commit_id) == -1)
1049 goto done;
1051 if (fcgi_gen_response(c, "&folder=") == -1)
1052 goto done;
1053 if (fcgi_gen_response(c, qs->folder) == -1)
1054 goto done;
1056 if (fcgi_gen_response(c, "&file=") == -1)
1057 goto done;
1058 if (fcgi_gen_response(c, name) == -1)
1059 goto done;
1061 if (fcgi_gen_response(c, "'>") == -1)
1062 goto done;
1063 if (fcgi_gen_response(c, name) == -1)
1064 goto done;
1065 if (fcgi_gen_response(c, modestr) == -1)
1066 goto done;
1068 if (fcgi_gen_response(c, "</a>") == -1)
1069 goto done;
1071 if (fcgi_gen_response(c, "</div>\n") == -1)
1072 goto done;
1074 if (fcgi_gen_response(c, "<div id='tree_line_blank' "
1075 "class='") == -1)
1076 goto done;
1077 if (fcgi_gen_response(c, class) == -1)
1078 goto done;
1079 if (fcgi_gen_response(c, "'>") == -1)
1080 goto done;
1082 if (fcgi_gen_response(c,
1083 "<a href='?index_page=") == -1)
1084 goto done;
1086 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1087 goto done;
1089 if (fcgi_gen_response(c, "&path=") == -1)
1090 goto done;
1091 if (fcgi_gen_response(c, qs->path) == -1)
1092 goto done;
1094 if (fcgi_gen_response(c, "&action=commits") == -1)
1095 goto done;
1097 if (fcgi_gen_response(c, "&commit=") == -1)
1098 goto done;
1099 if (fcgi_gen_response(c, rc->commit_id) == -1)
1100 goto done;
1102 if (fcgi_gen_response(c, "&folder=") == -1)
1103 goto done;
1104 if (fcgi_gen_response(c, qs->folder) == -1)
1105 goto done;
1107 if (fcgi_gen_response(c, "&file=") == -1)
1108 goto done;
1109 if (fcgi_gen_response(c, name) == -1)
1110 goto done;
1112 if (fcgi_gen_response(c, "'>") == -1)
1113 goto done;
1115 if (fcgi_gen_response(c, "commits") == -1)
1116 goto done;
1117 if (fcgi_gen_response(c, "</a>\n") == -1)
1118 goto done;
1120 if (fcgi_gen_response(c, " | \n") == -1)
1121 goto done;
1123 if (fcgi_gen_response(c,
1124 "<a href='?index_page=") == -1)
1125 goto done;
1127 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1128 goto done;
1130 if (fcgi_gen_response(c, "&path=") == -1)
1131 goto done;
1132 if (fcgi_gen_response(c, qs->path) == -1)
1133 goto done;
1135 if (fcgi_gen_response(c, "&action=blame") == -1)
1136 goto done;
1138 if (fcgi_gen_response(c, "&commit=") == -1)
1139 goto done;
1140 if (fcgi_gen_response(c, rc->commit_id) == -1)
1141 goto done;
1143 if (fcgi_gen_response(c, "&folder=") == -1)
1144 goto done;
1145 if (fcgi_gen_response(c, qs->folder) == -1)
1146 goto done;
1148 if (fcgi_gen_response(c, "&file=") == -1)
1149 goto done;
1150 if (fcgi_gen_response(c, name) == -1)
1151 goto done;
1153 if (fcgi_gen_response(c, "'>") == -1)
1154 goto done;
1156 if (fcgi_gen_response(c, "blame") == -1)
1157 goto done;
1158 if (fcgi_gen_response(c, "</a>\n") == -1)
1159 goto done;
1161 if (fcgi_gen_response(c, "</div>\n") == -1)
1162 goto done;
1163 if (fcgi_gen_response(c, "</div>\n") == -1)
1164 goto done;
1166 free(id_str);
1167 id_str = NULL;
1168 free(build_folder);
1169 build_folder = NULL;
1170 free(name);
1171 name = NULL;
1172 free(modestr);
1173 modestr = NULL;
1174 free(class);
1175 class = NULL;
1177 done:
1178 free(id_str);
1179 free(build_folder);
1180 free(modestr);
1181 free(path);
1182 free(name);
1183 free(class);
1184 got_ref_list_free(&refs);
1185 if (commit)
1186 got_object_commit_close(commit);
1187 free(commit_id);
1188 free(tree_id);
1189 return error;
1192 const struct got_error *
1193 got_output_file_blob(struct request *c)
1195 const struct got_error *error = NULL;
1196 struct transport *t = c->t;
1197 struct got_repository *repo = t->repo;
1198 struct querystring *qs = c->t->qs;
1199 struct got_commit_object *commit = NULL;
1200 struct got_object_id *commit_id = NULL;
1201 struct got_reflist_head refs;
1202 struct got_blob_object *blob = NULL;
1203 char *path = NULL, *in_repo_path = NULL;
1204 int obj_type, set_mime = 0, type = 0, fd = -1;
1205 char *buf_output = NULL;
1206 size_t len, hdrlen;
1207 const uint8_t *buf;
1209 TAILQ_INIT(&refs);
1211 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1212 qs->folder ? "/" : "", qs->file) == -1) {
1213 error = got_error_from_errno("asprintf");
1214 goto done;
1217 error = got_repo_map_path(&in_repo_path, repo, path);
1218 if (error)
1219 goto done;
1221 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1222 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1223 if (error)
1224 goto done;
1226 error = got_object_open_as_commit(&commit, repo, commit_id);
1227 if (error)
1228 goto done;
1230 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1231 if (error)
1232 goto done;
1234 if (commit_id == NULL) {
1235 error = got_error(GOT_ERR_NO_OBJ);
1236 goto done;
1239 error = got_object_get_type(&obj_type, repo, commit_id);
1240 if (error)
1241 goto done;
1243 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1244 error = got_error(GOT_ERR_OBJ_TYPE);
1245 goto done;
1248 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1249 if (error)
1250 goto done;
1252 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1253 if (error)
1254 goto done;
1255 hdrlen = got_object_blob_get_hdrlen(blob);
1256 do {
1257 error = got_object_blob_read_block(&len, blob);
1258 if (error)
1259 goto done;
1260 buf = got_object_blob_get_read_buf(blob);
1263 * Skip blob object header first time around,
1264 * which also contains a zero byte.
1266 buf += hdrlen;
1267 if (set_mime == 0) {
1268 if (isbinary(buf, len - hdrlen)) {
1269 error = gotweb_render_content_type_file(c,
1270 "application/octet-stream",
1271 qs->file);
1272 if (error) {
1273 log_warnx("%s: %s", __func__,
1274 error->msg);
1275 goto done;
1277 type = 0;
1278 } else {
1279 error = gotweb_render_content_type(c,
1280 "text/text");
1281 if (error) {
1282 log_warnx("%s: %s", __func__,
1283 error->msg);
1284 goto done;
1286 type = 1;
1289 set_mime = 1;
1290 if (type) {
1291 buf_output = calloc(len - hdrlen + 1,
1292 sizeof(*buf_output));
1293 if (buf_output == NULL) {
1294 error = got_error_from_errno("calloc");
1295 goto done;
1297 memcpy(buf_output, buf, len - hdrlen);
1298 fcgi_gen_response(c, buf_output);
1299 free(buf_output);
1300 buf_output = NULL;
1301 } else
1302 fcgi_gen_binary_response(c, buf, len - hdrlen);
1304 hdrlen = 0;
1305 } while (len != 0);
1306 done:
1307 if (commit)
1308 got_object_commit_close(commit);
1309 if (fd != -1 && close(fd) == -1 && error == NULL)
1310 error = got_error_from_errno("close");
1311 if (blob)
1312 got_object_blob_close(blob);
1313 free(buf_output);
1314 free(in_repo_path);
1315 free(commit_id);
1316 free(path);
1317 return error;
1320 struct blame_line {
1321 int annotated;
1322 char *id_str;
1323 char *committer;
1324 char datebuf[11]; /* YYYY-MM-DD + NUL */
1327 struct blame_cb_args {
1328 struct blame_line *lines;
1329 int nlines;
1330 int nlines_prec;
1331 int lineno_cur;
1332 off_t *line_offsets;
1333 FILE *f;
1334 struct got_repository *repo;
1335 struct request *c;
1338 static const struct got_error *
1339 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1340 struct got_commit_object *commit, struct got_object_id *id)
1342 const struct got_error *err = NULL;
1343 struct blame_cb_args *a = arg;
1344 struct blame_line *bline;
1345 struct request *c = a->c;
1346 struct transport *t = c->t;
1347 struct querystring *qs = t->qs;
1348 struct repo_dir *repo_dir = t->repo_dir;
1349 char *line = NULL, *eline = NULL;
1350 size_t linesize = 0;
1351 off_t offset;
1352 struct tm tm;
1353 time_t committer_time;
1355 if (nlines != a->nlines ||
1356 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1357 return got_error(GOT_ERR_RANGE);
1359 if (lineno == -1)
1360 return NULL; /* no change in this commit */
1362 /* Annotate this line. */
1363 bline = &a->lines[lineno - 1];
1364 if (bline->annotated)
1365 return NULL;
1366 err = got_object_id_str(&bline->id_str, id);
1367 if (err)
1368 return err;
1370 bline->committer = strdup(got_object_commit_get_committer(commit));
1371 if (bline->committer == NULL) {
1372 err = got_error_from_errno("strdup");
1373 goto done;
1376 committer_time = got_object_commit_get_committer_time(commit);
1377 if (gmtime_r(&committer_time, &tm) == NULL)
1378 return got_error_from_errno("gmtime_r");
1379 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1380 &tm) == 0) {
1381 err = got_error(GOT_ERR_NO_SPACE);
1382 goto done;
1384 bline->annotated = 1;
1386 /* Print lines annotated so far. */
1387 bline = &a->lines[a->lineno_cur - 1];
1388 if (!bline->annotated)
1389 goto done;
1391 offset = a->line_offsets[a->lineno_cur - 1];
1392 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1393 err = got_error_from_errno("fseeko");
1394 goto done;
1397 while (bline->annotated) {
1398 int out_buff_size = 100;
1399 char *smallerthan, *at, *nl, *committer;
1400 char out_buff[out_buff_size];
1401 size_t len;
1403 if (getline(&line, &linesize, a->f) == -1) {
1404 if (ferror(a->f))
1405 err = got_error_from_errno("getline");
1406 break;
1409 committer = bline->committer;
1410 smallerthan = strchr(committer, '<');
1411 if (smallerthan && smallerthan[1] != '\0')
1412 committer = smallerthan + 1;
1413 at = strchr(committer, '@');
1414 if (at)
1415 *at = '\0';
1416 len = strlen(committer);
1417 if (len >= 9)
1418 committer[8] = '\0';
1420 nl = strchr(line, '\n');
1421 if (nl)
1422 *nl = '\0';
1424 if (fcgi_gen_response(c, "<div id='blame_wrapper'>") == -1)
1425 goto done;
1426 if (fcgi_gen_response(c, "<div id='blame_number'>") == -1)
1427 goto done;
1428 if (snprintf(out_buff, strlen(out_buff), "%.*d", a->nlines_prec,
1429 a->lineno_cur) < 0)
1430 goto done;
1431 if (fcgi_gen_response(c, out_buff) == -1)
1432 goto done;
1433 if (fcgi_gen_response(c, "</div>") == -1)
1434 goto done;
1436 if (fcgi_gen_response(c, "<div id='blame_hash'>") == -1)
1437 goto done;
1439 if (fcgi_gen_response(c, "<a href='?index_page=") == -1)
1440 goto done;
1441 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1442 goto done;
1443 if (fcgi_gen_response(c, "&path=") == -1)
1444 goto done;
1445 if (fcgi_gen_response(c, repo_dir->name) == -1)
1446 goto done;
1447 if (fcgi_gen_response(c, "&action=diff&commit=") == -1)
1448 goto done;
1449 if (fcgi_gen_response(c, bline->id_str) == -1)
1450 goto done;
1451 if (fcgi_gen_response(c, "'>") == -1)
1452 goto done;
1453 if (snprintf(out_buff, 10, "%.8s", bline->id_str) < 0)
1454 goto done;
1455 if (fcgi_gen_response(c, out_buff) == -1)
1456 goto done;
1457 if (fcgi_gen_response(c, "</a></div>") == -1)
1458 goto done;
1460 if (fcgi_gen_response(c, "<div id='blame_date'>") == -1)
1461 goto done;
1462 if (fcgi_gen_response(c, bline->datebuf) == -1)
1463 goto done;
1464 if (fcgi_gen_response(c, "</div>") == -1)
1465 goto done;
1467 if (fcgi_gen_response(c, "<div id='blame_author'>") == -1)
1468 goto done;
1469 if (fcgi_gen_response(c, committer) == -1)
1470 goto done;
1471 if (fcgi_gen_response(c, "</div>") == -1)
1472 goto done;
1474 if (fcgi_gen_response(c, "<div id='blame_code'>") == -1)
1475 goto done;
1476 err = gotweb_escape_html(&eline, line);
1477 if (err)
1478 goto done;
1479 if (fcgi_gen_response(c, eline) == -1)
1480 goto done;
1481 if (fcgi_gen_response(c, "</div>") == -1)
1482 goto done;
1484 if (fcgi_gen_response(c, "</div>") == -1)
1485 goto done;
1486 a->lineno_cur++;
1487 bline = &a->lines[a->lineno_cur - 1];
1489 done:
1490 free(line);
1491 free(eline);
1492 return err;
1495 const struct got_error *
1496 got_output_file_blame(struct request *c)
1498 const struct got_error *error = NULL;
1499 struct transport *t = c->t;
1500 struct got_repository *repo = t->repo;
1501 struct querystring *qs = c->t->qs;
1502 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1503 struct got_commit_object *commit = NULL;
1504 struct got_reflist_head refs;
1505 struct got_blob_object *blob = NULL;
1506 char *path = NULL, *in_repo_path = NULL;
1507 struct blame_cb_args bca;
1508 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1509 int fd6 = -1;
1510 off_t filesize;
1511 FILE *f1 = NULL, *f2 = NULL;
1513 TAILQ_INIT(&refs);
1514 bca.f = NULL;
1515 bca.lines = NULL;
1517 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1518 qs->folder ? "/" : "", qs->file) == -1) {
1519 error = got_error_from_errno("asprintf");
1520 goto done;
1523 error = got_repo_map_path(&in_repo_path, repo, path);
1524 if (error)
1525 goto done;
1527 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1528 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1529 if (error)
1530 goto done;
1532 error = got_object_open_as_commit(&commit, repo, commit_id);
1533 if (error)
1534 goto done;
1536 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1537 if (error)
1538 goto done;
1540 if (commit_id == NULL) {
1541 error = got_error(GOT_ERR_NO_OBJ);
1542 goto done;
1545 error = got_object_get_type(&obj_type, repo, obj_id);
1546 if (error)
1547 goto done;
1549 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1550 error = got_error(GOT_ERR_OBJ_TYPE);
1551 goto done;
1554 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1555 if (error)
1556 goto done;
1558 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1559 if (error)
1560 goto done;
1562 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1563 if (error)
1564 goto done;
1566 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1567 &bca.line_offsets, bca.f, blob);
1568 if (error || bca.nlines == 0)
1569 goto done;
1571 /* Don't include \n at EOF in the blame line count. */
1572 if (bca.line_offsets[bca.nlines - 1] == filesize)
1573 bca.nlines--;
1575 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1576 if (bca.lines == NULL) {
1577 error = got_error_from_errno("calloc");
1578 goto done;
1580 bca.lineno_cur = 1;
1581 bca.nlines_prec = 0;
1582 i = bca.nlines;
1583 while (i > 0) {
1584 i /= 10;
1585 bca.nlines_prec++;
1587 bca.repo = repo;
1588 bca.c = c;
1590 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1591 if (error)
1592 goto done;
1594 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1595 if (error)
1596 goto done;
1598 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1599 if (error)
1600 goto done;
1602 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1603 if (error)
1604 goto done;
1606 error = got_blame(in_repo_path, commit_id, repo,
1607 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1608 fd3, fd4, f1, f2);
1610 if (blob) {
1611 free(bca.line_offsets);
1612 for (i = 0; i < bca.nlines; i++) {
1613 struct blame_line *bline = &bca.lines[i];
1614 free(bline->id_str);
1615 free(bline->committer);
1618 done:
1619 free(bca.lines);
1620 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1621 error = got_error_from_errno("close");
1622 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1623 error = got_error_from_errno("close");
1624 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1625 error = got_error_from_errno("close");
1626 if (bca.f) {
1627 const struct got_error *bca_err =
1628 got_gotweb_flushfile(bca.f, fd1);
1629 if (error == NULL)
1630 error = bca_err;
1632 if (f1) {
1633 const struct got_error *f1_err =
1634 got_gotweb_flushfile(f1, fd5);
1635 if (error == NULL)
1636 error = f1_err;
1638 if (f2) {
1639 const struct got_error *f2_err =
1640 got_gotweb_flushfile(f2, fd6);
1641 if (error == NULL)
1642 error = f2_err;
1644 if (commit)
1645 got_object_commit_close(commit);
1646 if (blob)
1647 got_object_blob_close(blob);
1648 free(in_repo_path);
1649 free(commit_id);
1650 free(path);
1651 return error;
1654 const struct got_error *
1655 got_output_repo_diff(struct request *c)
1657 const struct got_error *error = NULL;
1658 struct transport *t = c->t;
1659 struct got_repository *repo = t->repo;
1660 struct repo_commit *rc = NULL;
1661 struct got_object_id *id1 = NULL, *id2 = NULL;
1662 struct got_reflist_head refs;
1663 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1664 char *label1 = NULL, *label2 = NULL, *line = NULL;
1665 char *newline, *eline = NULL, *color = NULL;
1666 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1667 size_t linesize = 0;
1668 ssize_t linelen;
1669 int wrlen = 0;
1671 TAILQ_INIT(&refs);
1673 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1674 if (error)
1675 return error;
1677 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1678 if (error)
1679 return error;
1681 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1682 if (error)
1683 return error;
1685 rc = TAILQ_FIRST(&t->repo_commits);
1687 if (rc->parent_id != NULL &&
1688 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1689 error = got_repo_match_object_id(&id1, &label1,
1690 rc->parent_id, GOT_OBJ_TYPE_ANY,
1691 &refs, repo);
1692 if (error)
1693 goto done;
1696 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1697 GOT_OBJ_TYPE_ANY, &refs, repo);
1698 if (error)
1699 goto done;
1701 error = got_object_get_type(&obj_type, repo, id2);
1702 if (error)
1703 goto done;
1705 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1706 if (error)
1707 goto done;
1709 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1710 if (error)
1711 goto done;
1713 switch (obj_type) {
1714 case GOT_OBJ_TYPE_BLOB:
1715 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1716 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1717 repo, f3);
1718 break;
1719 case GOT_OBJ_TYPE_TREE:
1720 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1721 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1722 repo, f3);
1723 break;
1724 case GOT_OBJ_TYPE_COMMIT:
1725 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1726 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1727 repo, f3);
1728 break;
1729 default:
1730 error = got_error(GOT_ERR_OBJ_TYPE);
1732 if (error)
1733 goto done;
1735 if (fseek(f1, 0, SEEK_SET) == -1) {
1736 error = got_ferror(f1, GOT_ERR_IO);
1737 goto done;
1740 if (fseek(f2, 0, SEEK_SET) == -1) {
1741 error = got_ferror(f2, GOT_ERR_IO);
1742 goto done;
1745 if (fseek(f3, 0, SEEK_SET) == -1) {
1746 error = got_ferror(f3, GOT_ERR_IO);
1747 goto done;
1750 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1751 if (strncmp(line, "-", 1) == 0) {
1752 color = strdup("diff_minus");
1753 if (color == NULL) {
1754 error = got_error_from_errno("strdup");
1755 goto done;
1757 } else if (strncmp(line, "+", 1) == 0) {
1758 color = strdup("diff_plus");
1759 if (color == NULL) {
1760 error = got_error_from_errno("strdup");
1761 goto done;
1763 } else if (strncmp(line, "@@", 2) == 0) {
1764 color = strdup("diff_chunk_header");
1765 if (color == NULL) {
1766 error = got_error_from_errno("strdup");
1767 goto done;
1769 } else if (strncmp(line, "@@", 2) == 0) {
1770 color = strdup("diff_chunk_header");
1771 if (color == NULL) {
1772 error = got_error_from_errno("strdup");
1773 goto done;
1775 } else if (strncmp(line, "commit +", 8) == 0) {
1776 color = strdup("diff_meta");
1777 if (color == NULL) {
1778 error = got_error_from_errno("strdup");
1779 goto done;
1781 } else if (strncmp(line, "commit -", 8) == 0) {
1782 color = strdup("diff_meta");
1783 if (color == NULL) {
1784 error = got_error_from_errno("strdup");
1785 goto done;
1787 } else if (strncmp(line, "blob +", 6) == 0) {
1788 color = strdup("diff_meta");
1789 if (color == NULL) {
1790 error = got_error_from_errno("strdup");
1791 goto done;
1793 } else if (strncmp(line, "blob -", 6) == 0) {
1794 color = strdup("diff_meta");
1795 if (color == NULL) {
1796 error = got_error_from_errno("strdup");
1797 goto done;
1799 } else if (strncmp(line, "file +", 6) == 0) {
1800 color = strdup("diff_meta");
1801 if (color == NULL) {
1802 error = got_error_from_errno("strdup");
1803 goto done;
1805 } else if (strncmp(line, "file -", 6) == 0) {
1806 color = strdup("diff_meta");
1807 if (color == NULL) {
1808 error = got_error_from_errno("strdup");
1809 goto done;
1811 } else if (strncmp(line, "from:", 5) == 0) {
1812 color = strdup("diff_author");
1813 if (color == NULL) {
1814 error = got_error_from_errno("strdup");
1815 goto done;
1817 } else if (strncmp(line, "via:", 4) == 0) {
1818 color = strdup("diff_author");
1819 if (color == NULL) {
1820 error = got_error_from_errno("strdup");
1821 goto done;
1823 } else if (strncmp(line, "date:", 5) == 0) {
1824 color = strdup("diff_date");
1825 if (color == NULL) {
1826 error = got_error_from_errno("strdup");
1827 goto done;
1830 if (fcgi_gen_response(c, "<div id='diff_line' class='") == -1)
1831 goto done;
1832 if (fcgi_gen_response(c, color ? color : "") == -1)
1833 goto done;
1834 if (fcgi_gen_response(c, "'>") == -1)
1835 goto done;
1836 newline = strchr(line, '\n');
1837 if (newline)
1838 *newline = '\0';
1840 error = gotweb_escape_html(&eline, line);
1841 if (error)
1842 goto done;
1843 if (fcgi_gen_response(c, eline) == -1)
1844 goto done;
1845 free(eline);
1846 eline = NULL;
1848 if (fcgi_gen_response(c, "</div>\n") == -1)
1849 goto done;
1850 if (linelen > 0)
1851 wrlen = wrlen + linelen;
1852 free(color);
1853 color = NULL;
1855 if (linelen == -1 && ferror(f3))
1856 error = got_error_from_errno("getline");
1857 done:
1858 free(color);
1859 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1860 error = got_error_from_errno("close");
1861 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1862 error = got_error_from_errno("close");
1863 if (f1) {
1864 const struct got_error *f1_err =
1865 got_gotweb_flushfile(f1, fd1);
1866 if (error == NULL)
1867 error = f1_err;
1869 if (f2) {
1870 const struct got_error *f2_err =
1871 got_gotweb_flushfile(f2, fd2);
1872 if (error == NULL)
1873 error = f2_err;
1875 if (f3) {
1876 const struct got_error *f3_err =
1877 got_gotweb_flushfile(f3, fd3);
1878 if (error == NULL)
1879 error = f3_err;
1881 got_ref_list_free(&refs);
1882 free(line);
1883 free(eline);
1884 free(label1);
1885 free(label2);
1886 free(id1);
1887 free(id2);
1888 return error;
1891 static const struct got_error *
1892 got_init_repo_commit(struct repo_commit **rc)
1894 const struct got_error *error = NULL;
1896 *rc = calloc(1, sizeof(**rc));
1897 if (*rc == NULL)
1898 return got_error_from_errno2("%s: calloc", __func__);
1900 (*rc)->path = NULL;
1901 (*rc)->refs_str = NULL;
1902 (*rc)->commit_id = NULL;
1903 (*rc)->committer = NULL;
1904 (*rc)->author = NULL;
1905 (*rc)->parent_id = NULL;
1906 (*rc)->tree_id = NULL;
1907 (*rc)->commit_msg = NULL;
1909 return error;
1912 static const struct got_error *
1913 got_init_repo_tag(struct repo_tag **rt)
1915 const struct got_error *error = NULL;
1917 *rt = calloc(1, sizeof(**rt));
1918 if (*rt == NULL)
1919 return got_error_from_errno2("%s: calloc", __func__);
1921 (*rt)->commit_id = NULL;
1922 (*rt)->tag_name = NULL;
1923 (*rt)->tag_commit = NULL;
1924 (*rt)->commit_msg = NULL;
1925 (*rt)->tagger = NULL;
1927 return error;