2 * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include "got_compat.h"
20 #include <sys/queue.h>
21 #include <sys/socket.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_reference.h"
34 #include "got_repository.h"
36 #include "got_cancel.h"
38 #include "got_commit_graph.h"
39 #include "got_blame.h"
40 #include "got_privsep.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 *);
51 static const struct got_error
*got_gotweb_blame_cb(void *, int, int,
52 struct got_commit_object
*,struct got_object_id
*);
54 const struct got_error
*
55 got_gotweb_closefile(FILE *f
)
57 const struct got_error
*err
= NULL
;
59 if (fseek(f
, 0, SEEK_SET
) == -1)
60 err
= got_error_from_errno("fseek");
62 if (err
== NULL
&& ftruncate(fileno(f
), 0) == -1)
63 err
= got_error_from_errno("ftruncate");
65 if (fclose(f
) == EOF
&& err
== NULL
)
66 err
= got_error_from_errno("fclose");
71 static const struct got_error
*
72 got_gotweb_openfile(FILE **f
, int *priv_fd
)
78 return got_error_from_errno("dup");
80 *f
= fdopen(fd
, "w+");
83 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
89 static const struct got_error
*
90 got_gotweb_dupfd(int *priv_fd
, int *fd
)
95 return got_error_from_errno("dup");
100 const struct got_error
*
101 got_get_repo_owner(char **owner
, struct request
*c
)
103 struct server
*srv
= c
->srv
;
104 struct transport
*t
= c
->t
;
105 struct got_repository
*repo
= t
->repo
;
106 const char *gitconfig_owner
;
110 if (srv
->show_repo_owner
== 0)
113 gitconfig_owner
= got_repo_get_gitconfig_owner(repo
);
114 if (gitconfig_owner
) {
115 *owner
= strdup(gitconfig_owner
);
117 return got_error_from_errno("strdup");
121 return got_error_from_errno("strdup");
126 const struct got_error
*
127 got_get_repo_age(time_t *repo_age
, struct request
*c
, const char *refname
)
129 const struct got_error
*error
= NULL
;
130 struct transport
*t
= c
->t
;
131 struct got_repository
*repo
= t
->repo
;
132 struct got_commit_object
*commit
= NULL
;
133 struct got_reflist_head refs
;
134 struct got_reflist_entry
*re
;
135 time_t committer_time
= 0, cmp_time
= 0;
141 error
= got_ref_list(&refs
, repo
, "refs/heads",
142 got_ref_cmp_by_name
, NULL
);
147 * Find the youngest branch tip in the repository, or the age of
148 * the a specific branch tip if a name was provided by the caller.
150 TAILQ_FOREACH(re
, &refs
, entry
) {
151 struct got_object_id
*id
= NULL
;
153 if (refname
&& strcmp(got_ref_get_name(re
->ref
), refname
) != 0)
156 error
= got_ref_resolve(&id
, repo
, re
->ref
);
160 error
= got_object_open_as_commit(&commit
, repo
, id
);
166 got_object_commit_get_committer_time(commit
);
167 got_object_commit_close(commit
);
168 if (cmp_time
< committer_time
)
169 cmp_time
= committer_time
;
176 *repo_age
= cmp_time
;
178 got_ref_list_free(&refs
);
182 static const struct got_error
*
183 got_get_repo_commit(struct request
*c
, struct repo_commit
*repo_commit
,
184 struct got_commit_object
*commit
, struct got_reflist_head
*refs
,
185 struct got_object_id
*id
)
187 const struct got_error
*error
= NULL
;
188 struct got_reflist_entry
*re
;
189 struct got_object_id
*id2
= NULL
;
190 struct got_object_qid
*parent_id
;
191 struct transport
*t
= c
->t
;
192 struct querystring
*qs
= c
->t
->qs
;
193 char *commit_msg
= NULL
, *commit_msg0
;
195 TAILQ_FOREACH(re
, refs
, entry
) {
198 struct got_tag_object
*tag
= NULL
;
199 struct got_object_id
*ref_id
;
202 if (got_ref_is_symbolic(re
->ref
))
205 name
= got_ref_get_name(re
->ref
);
206 if (strncmp(name
, "refs/", 5) == 0)
208 if (strncmp(name
, "got/", 4) == 0)
210 if (strncmp(name
, "heads/", 6) == 0)
212 if (strncmp(name
, "remotes/", 8) == 0) {
214 if (strstr(name
, "/" GOT_REF_HEAD
) != NULL
)
217 error
= got_ref_resolve(&ref_id
, t
->repo
, re
->ref
);
220 if (strncmp(name
, "tags/", 5) == 0) {
221 error
= got_object_open_as_tag(&tag
, t
->repo
, ref_id
);
223 if (error
->code
!= GOT_ERR_OBJ_TYPE
) {
228 * Ref points at something other
235 cmp
= got_object_id_cmp(tag
?
236 got_object_tag_get_object_id(tag
) : ref_id
, id
);
239 got_object_tag_close(tag
);
242 s
= repo_commit
->refs_str
;
243 if (asprintf(&repo_commit
->refs_str
, "%s%s%s", s
? s
: "",
244 s
? ", " : "", name
) == -1) {
245 error
= got_error_from_errno("asprintf");
247 repo_commit
->refs_str
= NULL
;
253 error
= got_object_id_str(&repo_commit
->commit_id
, id
);
257 error
= got_object_id_str(&repo_commit
->tree_id
,
258 got_object_commit_get_tree_id(commit
));
262 if (qs
->action
== DIFF
|| qs
->action
== PATCH
) {
263 parent_id
= STAILQ_FIRST(
264 got_object_commit_get_parent_ids(commit
));
265 if (parent_id
!= NULL
) {
266 id2
= got_object_id_dup(&parent_id
->id
);
267 error
= got_object_id_str(&repo_commit
->parent_id
, id2
);
272 repo_commit
->parent_id
= strdup("/dev/null");
273 if (repo_commit
->parent_id
== NULL
) {
274 error
= got_error_from_errno("strdup");
280 repo_commit
->committer_time
=
281 got_object_commit_get_committer_time(commit
);
283 repo_commit
->author
=
284 strdup(got_object_commit_get_author(commit
));
285 if (repo_commit
->author
== NULL
) {
286 error
= got_error_from_errno("strdup");
289 repo_commit
->committer
=
290 strdup(got_object_commit_get_committer(commit
));
291 if (repo_commit
->committer
== NULL
) {
292 error
= got_error_from_errno("strdup");
295 error
= got_object_commit_get_logmsg(&commit_msg0
, commit
);
299 commit_msg
= commit_msg0
;
300 while (*commit_msg
== '\n')
303 repo_commit
->commit_msg
= strdup(commit_msg
);
304 if (repo_commit
->commit_msg
== NULL
)
305 error
= got_error_from_errno("strdup");
310 const struct got_error
*
311 got_get_repo_commits(struct request
*c
, size_t limit
)
313 const struct got_error
*error
= NULL
;
314 struct got_object_id
*id
= NULL
;
315 struct got_commit_graph
*graph
= NULL
;
316 struct got_commit_object
*commit
= NULL
;
317 struct got_reflist_head refs
;
318 struct got_reference
*ref
= NULL
;
319 struct repo_commit
*repo_commit
= NULL
;
320 struct server
*srv
= c
->srv
;
321 struct transport
*t
= c
->t
;
322 struct got_repository
*repo
= t
->repo
;
323 struct querystring
*qs
= t
->qs
;
324 struct repo_dir
*repo_dir
= t
->repo_dir
;
325 char *in_repo_path
= NULL
, *repo_path
= NULL
, *file_path
= NULL
;
329 return got_error(GOT_ERR_RANGE
);
333 * Traverse one commit more than requested to provide
342 if (qs
->file
!= NULL
&& *qs
->file
!= '\0')
343 if (asprintf(&file_path
, "%s/%s", qs
->folder
? qs
->folder
: "",
345 return got_error_from_errno("asprintf");
347 if (asprintf(&repo_path
, "%s/%s", srv
->repos_path
,
348 repo_dir
->name
) == -1) {
349 error
= got_error_from_errno("asprintf");
354 error
= got_repo_match_object_id_prefix(&id
, qs
->commit
,
355 GOT_OBJ_TYPE_COMMIT
, repo
);
359 error
= got_ref_open(&ref
, repo
, qs
->headref
, 0);
363 error
= got_ref_resolve(&id
, repo
, ref
);
368 error
= got_repo_map_path(&in_repo_path
, repo
, repo_path
);
372 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
376 if (qs
->file
!= NULL
&& *qs
->file
!= '\0') {
377 error
= got_commit_graph_open(&graph
, file_path
, 0);
381 error
= got_commit_graph_open(&graph
, in_repo_path
, 0);
386 error
= got_commit_graph_iter_start(graph
, id
, repo
, NULL
, NULL
);
391 struct got_object_id next_id
;
393 error
= got_commit_graph_iter_next(&next_id
, graph
, repo
, NULL
,
396 if (error
->code
== GOT_ERR_ITER_COMPLETED
)
401 error
= got_object_open_as_commit(&commit
, repo
, &next_id
);
405 error
= got_init_repo_commit(&repo_commit
);
409 error
= got_get_repo_commit(c
, repo_commit
, commit
,
412 gotweb_free_repo_commit(repo_commit
);
416 if (--limit
== 0 && chk_next
) {
417 t
->more_id
= strdup(repo_commit
->commit_id
);
418 if (t
->more_id
== NULL
)
419 error
= got_error_from_errno("strdup");
423 TAILQ_INSERT_TAIL(&t
->repo_commits
, repo_commit
, entry
);
429 got_object_commit_close(commit
);
437 got_object_commit_close(commit
);
439 got_commit_graph_close(graph
);
440 got_ref_list_free(&refs
);
448 const struct got_error
*
449 got_get_repo_tags(struct request
*c
, size_t limit
)
451 const struct got_error
*error
= NULL
;
452 struct got_object_id
*id
= NULL
;
453 struct got_commit_object
*commit
= NULL
;
454 struct got_reflist_head refs
;
455 struct got_reference
*ref
;
456 struct got_reflist_entry
*re
;
457 struct server
*srv
= c
->srv
;
458 struct transport
*t
= c
->t
;
459 struct got_repository
*repo
= t
->repo
;
460 struct querystring
*qs
= t
->qs
;
461 struct repo_dir
*repo_dir
= t
->repo_dir
;
462 struct got_tag_object
*tag
= NULL
;
463 struct repo_tag
*rt
= NULL
, *trt
= NULL
;
464 char *in_repo_path
= NULL
, *repo_path
= NULL
, *id_str
= NULL
;
465 char *tag_commit
= NULL
, *tag_commit0
= NULL
;
466 char *commit_msg
= NULL
, *commit_msg0
= NULL
;
467 int chk_next
= 0, chk_multi
= 1, commit_found
= 0, c_cnt
= 0;
472 return got_error(GOT_ERR_RANGE
);
474 if (asprintf(&repo_path
, "%s/%s", srv
->repos_path
,
475 repo_dir
->name
) == -1)
476 return got_error_from_errno("asprintf");
478 if (qs
->commit
== NULL
&& (qs
->action
== TAGS
|| qs
->action
== RSS
)) {
479 error
= got_ref_open(&ref
, repo
, qs
->headref
, 0);
482 error
= got_ref_resolve(&id
, repo
, ref
);
486 } else if (qs
->commit
== NULL
&& qs
->action
== TAG
) {
487 error
= got_error_msg(GOT_ERR_EOF
, "commit id missing");
490 error
= got_repo_match_object_id_prefix(&id
, qs
->commit
,
491 GOT_OBJ_TYPE_COMMIT
, repo
);
496 if (qs
->action
!= SUMMARY
&& qs
->action
!= TAGS
) {
497 error
= got_object_open_as_commit(&commit
, repo
, id
);
500 error
= got_object_commit_get_logmsg(&commit_msg0
, commit
);
504 got_object_commit_close(commit
);
509 error
= got_repo_map_path(&in_repo_path
, repo
, repo_path
);
513 error
= got_ref_list(&refs
, repo
, "refs/tags", got_ref_cmp_tags
,
522 * XXX: again, see previous message about caching
525 TAILQ_FOREACH(re
, &refs
, entry
) {
526 struct repo_tag
*new_repo_tag
= NULL
;
527 error
= got_init_repo_tag(&new_repo_tag
);
531 TAILQ_INSERT_TAIL(&t
->repo_tags
, new_repo_tag
, entry
);
533 new_repo_tag
->tag_name
= strdup(got_ref_get_name(re
->ref
));
534 if (new_repo_tag
->tag_name
== NULL
) {
535 error
= got_error_from_errno("strdup");
545 error
= got_ref_resolve(&id
, repo
, re
->ref
);
550 got_object_tag_close(tag
);
551 error
= got_object_open_as_tag(&tag
, repo
, id
);
553 if (error
->code
!= GOT_ERR_OBJ_TYPE
)
555 /* "lightweight" tag */
556 error
= got_object_open_as_commit(&commit
, repo
, id
);
559 new_repo_tag
->tagger
=
560 strdup(got_object_commit_get_committer(commit
));
561 if (new_repo_tag
->tagger
== NULL
) {
562 error
= got_error_from_errno("strdup");
565 new_repo_tag
->tagger_time
=
566 got_object_commit_get_committer_time(commit
);
567 error
= got_object_id_str(&id_str
, id
);
571 new_repo_tag
->tagger
=
572 strdup(got_object_tag_get_tagger(tag
));
573 if (new_repo_tag
->tagger
== NULL
) {
574 error
= got_error_from_errno("strdup");
577 new_repo_tag
->tagger_time
=
578 got_object_tag_get_tagger_time(tag
);
579 error
= got_object_id_str(&id_str
,
580 got_object_tag_get_object_id(tag
));
585 new_repo_tag
->commit_id
= strdup(id_str
);
586 if (new_repo_tag
->commit_id
== NULL
)
589 if (commit_found
== 0 && qs
->commit
!= NULL
&&
590 strncmp(id_str
, qs
->commit
, strlen(id_str
)) != 0)
598 * check for one more commit before breaking,
599 * so we know whether to navigate through briefs
600 * commits and summary
603 t
->next_id
= strdup(new_repo_tag
->commit_id
);
604 if (t
->next_id
== NULL
) {
605 error
= got_error_from_errno("strdup");
609 got_object_commit_close(commit
);
612 TAILQ_REMOVE(&t
->repo_tags
, new_repo_tag
, entry
);
613 gotweb_free_repo_tag(new_repo_tag
);
618 error
= got_object_commit_get_logmsg(&tag_commit0
,
622 got_object_commit_close(commit
);
625 tag_commit0
= strdup(got_object_tag_get_message(tag
));
626 if (tag_commit0
== NULL
) {
627 error
= got_error_from_errno("strdup");
632 tag_commit
= tag_commit0
;
633 while (*tag_commit
== '\n')
635 new_repo_tag
->tag_commit
= strdup(tag_commit
);
636 if (new_repo_tag
->tag_commit
== NULL
) {
637 error
= got_error_from_errno("strdup");
643 if (qs
->action
!= SUMMARY
&& qs
->action
!= TAGS
) {
644 commit_msg
= commit_msg0
;
645 while (*commit_msg
== '\n')
648 new_repo_tag
->commit_msg
= strdup(commit_msg
);
649 if (new_repo_tag
->commit_msg
== NULL
) {
650 error
= got_error_from_errno("strdup");
655 if (limit
&& --limit
== 0) {
664 * we have tailq populated, so find previous commit id
665 * for navigation through briefs and commits
667 if (t
->tag_count
== 0) {
668 TAILQ_FOREACH_SAFE(rt
, &t
->repo_tags
, entry
, trt
) {
669 TAILQ_REMOVE(&t
->repo_tags
, rt
, entry
);
670 gotweb_free_repo_tag(rt
);
673 if (t
->tag_count
> 0 && t
->prev_id
== NULL
&& qs
->commit
!= NULL
) {
675 TAILQ_FOREACH_REVERSE(rt
, &t
->repo_tags
, repo_tags_head
,
677 if (commit_found
== 0 && rt
->commit_id
!= NULL
&&
678 strcmp(qs
->commit
, rt
->commit_id
) != 0) {
682 if (c_cnt
== srv
->max_commits_display
||
683 rt
== TAILQ_FIRST(&t
->repo_tags
)) {
684 t
->prev_id
= strdup(rt
->commit_id
);
685 if (t
->prev_id
== NULL
)
686 error
= got_error_from_errno("strdup");
694 got_object_commit_close(commit
);
696 got_object_tag_close(tag
);
697 got_ref_list_free(&refs
);
707 got_output_repo_tree(struct request
*c
,
708 int (*cb
)(struct template *, struct got_tree_entry
*))
710 const struct got_error
*error
= NULL
;
711 struct transport
*t
= c
->t
;
712 struct got_commit_object
*commit
= NULL
;
713 struct got_repository
*repo
= t
->repo
;
714 struct querystring
*qs
= t
->qs
;
715 struct repo_commit
*rc
= NULL
;
716 struct got_object_id
*tree_id
= NULL
, *commit_id
= NULL
;
717 struct got_reflist_head refs
;
718 struct got_tree_object
*tree
= NULL
;
719 struct got_tree_entry
*te
;
720 struct repo_dir
*repo_dir
= t
->repo_dir
;
721 char *escaped_name
= NULL
, *path
= NULL
;
726 rc
= TAILQ_FIRST(&t
->repo_commits
);
728 if (qs
->folder
!= NULL
) {
729 path
= strdup(qs
->folder
);
731 error
= got_error_from_errno("strdup");
735 error
= got_repo_map_path(&path
, repo
, repo_dir
->path
);
740 error
= got_repo_match_object_id(&commit_id
, NULL
, rc
->commit_id
,
741 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
745 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
749 error
= got_object_id_by_path(&tree_id
, repo
, commit
, path
);
753 error
= got_object_open_as_tree(&tree
, repo
, tree_id
);
757 nentries
= got_object_tree_get_nentries(tree
);
759 for (i
= 0; i
< nentries
; i
++) {
760 te
= got_object_tree_get_entry(tree
, i
);
761 if (cb(c
->tp
, te
) == -1) {
762 error
= got_error(GOT_ERR_CANCELLED
);
769 got_ref_list_free(&refs
);
771 got_object_commit_close(commit
);
773 got_object_tree_close(tree
);
777 if (error
->code
!= GOT_ERR_CANCELLED
)
778 log_warnx("%s: %s", __func__
, error
->msg
);
784 const struct got_error
*
785 got_open_blob_for_output(struct got_blob_object
**blob
, int *fd
,
786 int *binary
, struct request
*c
)
788 const struct got_error
*error
= NULL
;
789 struct transport
*t
= c
->t
;
790 struct got_repository
*repo
= t
->repo
;
791 struct querystring
*qs
= c
->t
->qs
;
792 struct got_commit_object
*commit
= NULL
;
793 struct got_object_id
*commit_id
= NULL
;
794 struct got_reflist_head refs
;
795 char *path
= NULL
, *in_repo_path
= NULL
;
804 if (asprintf(&path
, "%s%s%s", qs
->folder
? qs
->folder
: "",
805 qs
->folder
? "/" : "", qs
->file
) == -1) {
806 error
= got_error_from_errno("asprintf");
810 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
814 error
= got_repo_match_object_id(&commit_id
, NULL
, qs
->commit
,
815 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
819 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
823 error
= got_object_id_by_path(&commit_id
, repo
, commit
, in_repo_path
);
827 if (commit_id
== NULL
) {
828 error
= got_error(GOT_ERR_NO_OBJ
);
832 error
= got_object_get_type(&obj_type
, repo
, commit_id
);
836 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
837 error
= got_error(GOT_ERR_OBJ_TYPE
);
841 error
= got_gotweb_dupfd(&c
->priv_fd
[BLOB_FD_1
], fd
);
845 error
= got_object_open_as_blob(blob
, repo
, commit_id
, BUF
, *fd
);
849 error
= got_object_blob_is_binary(binary
, *blob
);
855 got_object_commit_close(commit
);
861 got_object_blob_close(*blob
);
873 got_output_blob_by_lines(struct template *tp
, struct got_blob_object
*blob
,
874 int (*cb
)(struct template *, const char *, size_t))
876 const struct got_error
*err
;
883 err
= got_object_blob_getline(&line
, &linelen
, &linesize
,
885 if (err
|| linelen
== -1)
888 if (cb(tp
, line
, lineno
) == -1) {
889 err
= got_error(GOT_ERR_CANCELLED
);
897 if (err
->code
!= GOT_ERR_CANCELLED
)
898 log_warnx("%s: got_object_blob_getline failed: %s",
905 struct blame_cb_args
{
906 struct blame_line
*lines
;
912 struct got_repository
*repo
;
914 got_render_blame_line_cb cb
;
917 static const struct got_error
*
918 got_gotweb_blame_cb(void *arg
, int nlines
, int lineno
,
919 struct got_commit_object
*commit
, struct got_object_id
*id
)
921 const struct got_error
*err
= NULL
;
922 struct blame_cb_args
*a
= arg
;
923 struct blame_line
*bline
;
924 struct request
*c
= a
->c
;
929 time_t committer_time
;
931 if (nlines
!= a
->nlines
||
932 (lineno
!= -1 && lineno
< 1) || lineno
> a
->nlines
)
933 return got_error(GOT_ERR_RANGE
);
936 return NULL
; /* no change in this commit */
938 /* Annotate this line. */
939 bline
= &a
->lines
[lineno
- 1];
940 if (bline
->annotated
)
942 err
= got_object_id_str(&bline
->id_str
, id
);
946 bline
->committer
= strdup(got_object_commit_get_committer(commit
));
947 if (bline
->committer
== NULL
) {
948 err
= got_error_from_errno("strdup");
952 committer_time
= got_object_commit_get_committer_time(commit
);
953 if (gmtime_r(&committer_time
, &tm
) == NULL
)
954 return got_error_from_errno("gmtime_r");
955 if (strftime(bline
->datebuf
, sizeof(bline
->datebuf
), "%G-%m-%d",
957 err
= got_error(GOT_ERR_NO_SPACE
);
960 bline
->annotated
= 1;
962 /* Print lines annotated so far. */
963 bline
= &a
->lines
[a
->lineno_cur
- 1];
964 if (!bline
->annotated
)
967 offset
= a
->line_offsets
[a
->lineno_cur
- 1];
968 if (fseeko(a
->f
, offset
, SEEK_SET
) == -1) {
969 err
= got_error_from_errno("fseeko");
973 while (a
->lineno_cur
<= a
->nlines
&& bline
->annotated
) {
974 if (getline(&line
, &linesize
, a
->f
) == -1) {
976 err
= got_error_from_errno("getline");
980 if (a
->cb(c
->tp
, line
, bline
, a
->nlines_prec
,
981 a
->lineno_cur
) == -1) {
982 err
= got_error(GOT_ERR_CANCELLED
);
987 bline
= &a
->lines
[a
->lineno_cur
- 1];
994 const struct got_error
*
995 got_output_file_blame(struct request
*c
, got_render_blame_line_cb cb
)
997 const struct got_error
*error
= NULL
;
998 struct transport
*t
= c
->t
;
999 struct got_repository
*repo
= t
->repo
;
1000 struct querystring
*qs
= c
->t
->qs
;
1001 struct got_object_id
*obj_id
= NULL
, *commit_id
= NULL
;
1002 struct got_commit_object
*commit
= NULL
;
1003 struct got_reflist_head refs
;
1004 struct got_blob_object
*blob
= NULL
;
1005 char *path
= NULL
, *in_repo_path
= NULL
;
1006 struct blame_cb_args bca
;
1007 int i
, obj_type
, blobfd
= -1, fd1
= -1, fd2
= -1;
1009 FILE *f1
= NULL
, *f2
= NULL
;
1016 if (asprintf(&path
, "%s%s%s", qs
->folder
? qs
->folder
: "",
1017 qs
->folder
? "/" : "", qs
->file
) == -1) {
1018 error
= got_error_from_errno("asprintf");
1022 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
1026 error
= got_repo_match_object_id(&commit_id
, NULL
, qs
->commit
,
1027 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
1031 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
1035 error
= got_object_id_by_path(&obj_id
, repo
, commit
, in_repo_path
);
1039 error
= got_object_get_type(&obj_type
, repo
, obj_id
);
1043 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
1044 error
= got_error(GOT_ERR_OBJ_TYPE
);
1048 error
= got_gotweb_openfile(&bca
.f
, &c
->priv_fd
[BLAME_FD_1
]);
1052 error
= got_gotweb_dupfd(&c
->priv_fd
[BLAME_FD_2
], &blobfd
);
1056 error
= got_object_open_as_blob(&blob
, repo
, obj_id
, BUF
, blobfd
);
1060 error
= got_object_blob_dump_to_file(&filesize
, &bca
.nlines
,
1061 &bca
.line_offsets
, bca
.f
, blob
);
1062 if (error
|| bca
.nlines
== 0)
1065 /* Don't include \n at EOF in the blame line count. */
1066 if (bca
.line_offsets
[bca
.nlines
- 1] == filesize
)
1069 bca
.lines
= calloc(bca
.nlines
, sizeof(*bca
.lines
));
1070 if (bca
.lines
== NULL
) {
1071 error
= got_error_from_errno("calloc");
1075 bca
.nlines_prec
= 0;
1084 error
= got_gotweb_dupfd(&c
->priv_fd
[BLAME_FD_3
], &fd1
);
1088 error
= got_gotweb_dupfd(&c
->priv_fd
[BLAME_FD_4
], &fd2
);
1092 error
= got_gotweb_openfile(&f1
, &c
->priv_fd
[BLAME_FD_5
]);
1096 error
= got_gotweb_openfile(&f2
, &c
->priv_fd
[BLAME_FD_6
]);
1100 error
= got_blame(in_repo_path
, commit_id
, repo
,
1101 GOT_DIFF_ALGORITHM_MYERS
, got_gotweb_blame_cb
, &bca
, NULL
, NULL
,
1106 free(bca
.line_offsets
);
1107 for (i
= 0; i
< bca
.nlines
; i
++) {
1108 struct blame_line
*bline
= &bca
.lines
[i
];
1109 free(bline
->id_str
);
1110 free(bline
->committer
);
1114 if (blobfd
!= -1 && close(blobfd
) == -1 && error
== NULL
)
1115 error
= got_error_from_errno("close");
1116 if (fd1
!= -1 && close(fd1
) == -1 && error
== NULL
)
1117 error
= got_error_from_errno("close");
1118 if (fd2
!= -1 && close(fd2
) == -1 && error
== NULL
)
1119 error
= got_error_from_errno("close");
1121 const struct got_error
*bca_err
= got_gotweb_closefile(bca
.f
);
1126 const struct got_error
*f1_err
= got_gotweb_closefile(f1
);
1131 const struct got_error
*f2_err
= got_gotweb_closefile(f2
);
1136 got_object_commit_close(commit
);
1138 got_object_blob_close(blob
);
1143 got_ref_list_free(&refs
);
1147 const struct got_error
*
1148 got_open_diff_for_output(FILE **fp
, struct request
*c
)
1150 const struct got_error
*error
= NULL
;
1151 struct transport
*t
= c
->t
;
1152 struct got_repository
*repo
= t
->repo
;
1153 struct repo_commit
*rc
= NULL
;
1154 struct got_object_id
*id1
= NULL
, *id2
= NULL
;
1155 struct got_reflist_head refs
;
1156 FILE *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
1157 int obj_type
, fd1
= -1, fd2
= -1;
1163 error
= got_gotweb_openfile(&f1
, &c
->priv_fd
[DIFF_FD_1
]);
1167 error
= got_gotweb_openfile(&f2
, &c
->priv_fd
[DIFF_FD_2
]);
1171 error
= got_gotweb_openfile(&f3
, &c
->priv_fd
[DIFF_FD_3
]);
1175 rc
= TAILQ_FIRST(&t
->repo_commits
);
1177 if (rc
->parent_id
!= NULL
&&
1178 strncmp(rc
->parent_id
, "/dev/null", 9) != 0) {
1179 error
= got_repo_match_object_id(&id1
, NULL
,
1180 rc
->parent_id
, GOT_OBJ_TYPE_ANY
,
1186 error
= got_repo_match_object_id(&id2
, NULL
, rc
->commit_id
,
1187 GOT_OBJ_TYPE_ANY
, &refs
, repo
);
1191 error
= got_object_get_type(&obj_type
, repo
, id2
);
1195 error
= got_gotweb_dupfd(&c
->priv_fd
[DIFF_FD_4
], &fd1
);
1199 error
= got_gotweb_dupfd(&c
->priv_fd
[DIFF_FD_5
], &fd2
);
1204 case GOT_OBJ_TYPE_BLOB
:
1205 error
= got_diff_objects_as_blobs(NULL
, NULL
, f1
, f2
, fd1
, fd2
,
1206 id1
, id2
, NULL
, NULL
, GOT_DIFF_ALGORITHM_MYERS
, 3, 0, 0,
1209 case GOT_OBJ_TYPE_TREE
:
1210 error
= got_diff_objects_as_trees(NULL
, NULL
, f1
, f2
, fd1
, fd2
,
1211 id1
, id2
, NULL
, "", "", GOT_DIFF_ALGORITHM_MYERS
, 3, 0, 0,
1214 case GOT_OBJ_TYPE_COMMIT
:
1215 error
= got_diff_objects_as_commits(NULL
, NULL
, f1
, f2
, fd1
,
1216 fd2
, id1
, id2
, NULL
, GOT_DIFF_ALGORITHM_MYERS
, 3, 0, 0,
1220 error
= got_error(GOT_ERR_OBJ_TYPE
);
1225 if (fseek(f3
, 0, SEEK_SET
) == -1) {
1226 error
= got_ferror(f3
, GOT_ERR_IO
);
1233 if (fd1
!= -1 && close(fd1
) == -1 && error
== NULL
)
1234 error
= got_error_from_errno("close");
1235 if (fd2
!= -1 && close(fd2
) == -1 && error
== NULL
)
1236 error
= got_error_from_errno("close");
1238 const struct got_error
*f1_err
= got_gotweb_closefile(f1
);
1243 const struct got_error
*f2_err
= got_gotweb_closefile(f2
);
1248 got_gotweb_closefile(f3
);
1251 got_ref_list_free(&refs
);
1257 static const struct got_error
*
1258 got_init_repo_commit(struct repo_commit
**rc
)
1260 *rc
= calloc(1, sizeof(**rc
));
1262 return got_error_from_errno2(__func__
, "calloc");
1265 (*rc
)->refs_str
= NULL
;
1266 (*rc
)->commit_id
= NULL
;
1267 (*rc
)->committer
= NULL
;
1268 (*rc
)->author
= NULL
;
1269 (*rc
)->parent_id
= NULL
;
1270 (*rc
)->tree_id
= NULL
;
1271 (*rc
)->commit_msg
= NULL
;
1276 static const struct got_error
*
1277 got_init_repo_tag(struct repo_tag
**rt
)
1279 *rt
= calloc(1, sizeof(**rt
));
1281 return got_error_from_errno2(__func__
, "calloc");
1283 (*rt
)->commit_id
= NULL
;
1284 (*rt
)->tag_name
= NULL
;
1285 (*rt
)->tag_commit
= NULL
;
1286 (*rt
)->commit_msg
= NULL
;
1287 (*rt
)->tagger
= NULL
;