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"
45 static const struct got_error
*got_init_repo_commit(struct repo_commit
**);
46 static const struct got_error
*got_init_repo_tag(struct repo_tag
**);
47 static const struct got_error
*got_get_repo_commit(struct request
*,
48 struct repo_commit
*, struct got_commit_object
*, struct got_reflist_head
*,
49 struct got_object_id
*);
50 static const struct got_error
*got_gotweb_dupfd(int *, int *);
51 static const struct got_error
*got_gotweb_openfile(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 const struct got_error
*
56 got_gotweb_closefile(FILE *f
)
58 const struct got_error
*err
= NULL
;
60 if (fseek(f
, 0, SEEK_SET
) == -1)
61 err
= got_error_from_errno("fseek");
63 if (ftruncate(fileno(f
), 0) == -1 && err
== NULL
)
64 err
= got_error_from_errno("ftruncate");
66 if (fclose(f
) == EOF
&& err
== NULL
)
67 err
= got_error_from_errno("fclose");
72 static const struct got_error
*
73 got_gotweb_openfile(FILE **f
, int *priv_fd
)
79 return got_error_from_errno("dup");
81 *f
= fdopen(fd
, "w+");
84 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
90 static const struct got_error
*
91 got_gotweb_dupfd(int *priv_fd
, int *fd
)
96 return got_error_from_errno("dup");
101 const struct got_error
*
102 got_get_repo_owner(char **owner
, struct request
*c
)
104 struct transport
*t
= c
->t
;
105 struct got_repository
*repo
= t
->repo
;
106 const char *gitconfig_owner
;
109 gitconfig_owner
= got_repo_get_gitconfig_owner(repo
);
110 if (gitconfig_owner
) {
111 *owner
= strdup(gitconfig_owner
);
113 return got_error_from_errno("strdup");
119 const struct got_error
*
120 got_get_repo_age(time_t *repo_age
, struct request
*c
, const char *refname
)
122 const struct got_error
*error
= NULL
;
123 struct transport
*t
= c
->t
;
124 struct got_repository
*repo
= t
->repo
;
125 struct got_commit_object
*commit
= NULL
;
126 struct got_reflist_head refs
;
127 struct got_reflist_entry
*re
;
128 time_t committer_time
= 0, cmp_time
= 0;
134 error
= got_ref_list(&refs
, repo
, "refs/heads",
135 got_ref_cmp_by_name
, NULL
);
140 * Find the youngest branch tip in the repository, or the age of
141 * the a specific branch tip if a name was provided by the caller.
143 TAILQ_FOREACH(re
, &refs
, entry
) {
144 struct got_object_id
*id
= NULL
;
146 if (refname
&& strcmp(got_ref_get_name(re
->ref
), refname
) != 0)
149 error
= got_ref_resolve(&id
, repo
, re
->ref
);
153 error
= got_object_open_as_commit(&commit
, repo
, id
);
159 got_object_commit_get_committer_time(commit
);
160 got_object_commit_close(commit
);
161 if (cmp_time
< committer_time
)
162 cmp_time
= committer_time
;
169 *repo_age
= cmp_time
;
171 got_ref_list_free(&refs
);
175 static const struct got_error
*
176 got_get_repo_commit(struct request
*c
, struct repo_commit
*repo_commit
,
177 struct got_commit_object
*commit
, struct got_reflist_head
*refs
,
178 struct got_object_id
*id
)
180 const struct got_error
*error
= NULL
;
181 struct got_reflist_entry
*re
;
182 struct got_object_id
*id2
= NULL
;
183 struct got_object_qid
*parent_id
;
184 struct transport
*t
= c
->t
;
185 struct querystring
*qs
= c
->t
->qs
;
186 char *commit_msg
= NULL
, *commit_msg0
;
188 TAILQ_FOREACH(re
, refs
, entry
) {
191 struct got_tag_object
*tag
= NULL
;
192 struct got_object_id
*ref_id
;
195 if (got_ref_is_symbolic(re
->ref
))
198 name
= got_ref_get_name(re
->ref
);
199 if (strncmp(name
, "refs/", 5) == 0)
201 if (strncmp(name
, "got/", 4) == 0)
203 if (strncmp(name
, "heads/", 6) == 0)
205 if (strncmp(name
, "remotes/", 8) == 0) {
207 if (strstr(name
, "/" GOT_REF_HEAD
) != NULL
)
210 error
= got_ref_resolve(&ref_id
, t
->repo
, re
->ref
);
213 if (strncmp(name
, "tags/", 5) == 0) {
214 error
= got_object_open_as_tag(&tag
, t
->repo
, ref_id
);
216 if (error
->code
!= GOT_ERR_OBJ_TYPE
) {
221 * Ref points at something other
228 cmp
= got_object_id_cmp(tag
?
229 got_object_tag_get_object_id(tag
) : ref_id
, id
);
232 got_object_tag_close(tag
);
235 s
= repo_commit
->refs_str
;
236 if (asprintf(&repo_commit
->refs_str
, "%s%s%s", s
? s
: "",
237 s
? ", " : "", name
) == -1) {
238 error
= got_error_from_errno("asprintf");
240 repo_commit
->refs_str
= NULL
;
246 error
= got_object_id_str(&repo_commit
->commit_id
, id
);
250 error
= got_object_id_str(&repo_commit
->tree_id
,
251 got_object_commit_get_tree_id(commit
));
255 if (qs
->action
== DIFF
|| qs
->action
== PATCH
) {
256 parent_id
= STAILQ_FIRST(
257 got_object_commit_get_parent_ids(commit
));
258 if (parent_id
!= NULL
) {
259 id2
= got_object_id_dup(&parent_id
->id
);
260 error
= got_object_id_str(&repo_commit
->parent_id
, id2
);
265 repo_commit
->parent_id
= strdup("/dev/null");
266 if (repo_commit
->parent_id
== NULL
) {
267 error
= got_error_from_errno("strdup");
273 repo_commit
->committer_time
=
274 got_object_commit_get_committer_time(commit
);
276 repo_commit
->author
=
277 strdup(got_object_commit_get_author(commit
));
278 if (repo_commit
->author
== NULL
) {
279 error
= got_error_from_errno("strdup");
282 repo_commit
->committer
=
283 strdup(got_object_commit_get_committer(commit
));
284 if (repo_commit
->committer
== NULL
) {
285 error
= got_error_from_errno("strdup");
288 error
= got_object_commit_get_logmsg(&commit_msg0
, commit
);
292 commit_msg
= commit_msg0
;
293 while (*commit_msg
== '\n')
296 repo_commit
->commit_msg
= strdup(commit_msg
);
297 if (repo_commit
->commit_msg
== NULL
)
298 error
= got_error_from_errno("strdup");
303 const struct got_error
*
304 got_get_repo_commits(struct request
*c
, size_t limit
)
306 const struct got_error
*error
= NULL
;
307 struct got_object_id
*id
= NULL
;
308 struct got_commit_graph
*graph
= NULL
;
309 struct got_commit_object
*commit
= NULL
;
310 struct got_reflist_head refs
;
311 struct got_reference
*ref
= NULL
;
312 struct repo_commit
*repo_commit
= NULL
;
313 struct server
*srv
= c
->srv
;
314 struct transport
*t
= c
->t
;
315 struct got_repository
*repo
= t
->repo
;
316 struct querystring
*qs
= t
->qs
;
317 struct repo_dir
*repo_dir
= t
->repo_dir
;
318 char *in_repo_path
= NULL
, *repo_path
= NULL
, *file_path
= NULL
;
322 return got_error(GOT_ERR_RANGE
);
326 * Traverse one commit more than requested to provide
335 if (qs
->file
!= NULL
&& *qs
->file
!= '\0')
336 if (asprintf(&file_path
, "%s/%s", qs
->folder
? qs
->folder
: "",
338 return got_error_from_errno("asprintf");
340 if (asprintf(&repo_path
, "%s/%s", srv
->repos_path
,
341 repo_dir
->name
) == -1) {
342 error
= got_error_from_errno("asprintf");
347 error
= got_repo_match_object_id_prefix(&id
, qs
->commit
,
348 GOT_OBJ_TYPE_COMMIT
, repo
);
352 error
= got_ref_open(&ref
, repo
, qs
->headref
, 0);
356 error
= got_ref_resolve(&id
, repo
, ref
);
361 error
= got_repo_map_path(&in_repo_path
, repo
, repo_path
);
365 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
369 if (qs
->file
!= NULL
&& *qs
->file
!= '\0') {
370 error
= got_commit_graph_open(&graph
, file_path
, 0);
374 error
= got_commit_graph_open(&graph
, in_repo_path
, 0);
379 error
= got_commit_graph_bfsort(graph
, id
, repo
, NULL
, NULL
);
384 struct got_object_id next_id
;
386 error
= got_commit_graph_iter_next(&next_id
, graph
, repo
, NULL
,
389 if (error
->code
== GOT_ERR_ITER_COMPLETED
)
394 error
= got_object_open_as_commit(&commit
, repo
, &next_id
);
398 error
= got_init_repo_commit(&repo_commit
);
402 error
= got_get_repo_commit(c
, repo_commit
, commit
,
405 gotweb_free_repo_commit(repo_commit
);
409 if (--limit
== 0 && chk_next
) {
410 t
->more_id
= strdup(repo_commit
->commit_id
);
411 if (t
->more_id
== NULL
)
412 error
= got_error_from_errno("strdup");
416 TAILQ_INSERT_TAIL(&t
->repo_commits
, repo_commit
, entry
);
422 got_object_commit_close(commit
);
430 got_object_commit_close(commit
);
432 got_commit_graph_close(graph
);
433 got_ref_list_free(&refs
);
441 const struct got_error
*
442 got_get_repo_tags(struct request
*c
, size_t limit
)
444 const struct got_error
*error
= NULL
;
445 struct got_object_id
*id
= NULL
;
446 struct got_commit_object
*commit
= NULL
;
447 struct got_reflist_head refs
;
448 struct got_reference
*ref
;
449 struct got_reflist_entry
*re
;
450 struct server
*srv
= c
->srv
;
451 struct transport
*t
= c
->t
;
452 struct got_repository
*repo
= t
->repo
;
453 struct querystring
*qs
= t
->qs
;
454 struct repo_dir
*repo_dir
= t
->repo_dir
;
455 struct got_tag_object
*tag
= NULL
;
456 char *in_repo_path
= NULL
, *repo_path
= NULL
, *id_str
= NULL
;
457 char *tag_commit
= NULL
, *tag_commit0
= NULL
;
458 char *commit_msg
= NULL
, *commit_msg0
= NULL
;
459 int chk_next
= 0, chk_multi
= 1, commit_found
= 0;
464 return got_error(GOT_ERR_RANGE
);
466 if (asprintf(&repo_path
, "%s/%s", srv
->repos_path
,
467 repo_dir
->name
) == -1)
468 return got_error_from_errno("asprintf");
470 if (qs
->commit
== NULL
&& (qs
->action
== TAGS
|| qs
->action
== RSS
)) {
471 error
= got_ref_open(&ref
, repo
, qs
->headref
, 0);
474 error
= got_ref_resolve(&id
, repo
, ref
);
478 } else if (qs
->commit
== NULL
&& qs
->action
== TAG
) {
479 error
= got_error_msg(GOT_ERR_EOF
, "commit id missing");
482 error
= got_repo_match_object_id_prefix(&id
, qs
->commit
,
483 GOT_OBJ_TYPE_COMMIT
, repo
);
488 if (qs
->action
!= SUMMARY
&& qs
->action
!= TAGS
) {
489 error
= got_object_open_as_commit(&commit
, repo
, id
);
492 error
= got_object_commit_get_logmsg(&commit_msg0
, commit
);
496 got_object_commit_close(commit
);
501 error
= got_repo_map_path(&in_repo_path
, repo
, repo_path
);
505 error
= got_ref_list(&refs
, repo
, "refs/tags", got_ref_cmp_tags
,
513 TAILQ_FOREACH(re
, &refs
, entry
) {
514 struct repo_tag
*new_repo_tag
= NULL
;
515 error
= got_init_repo_tag(&new_repo_tag
);
519 TAILQ_INSERT_TAIL(&t
->repo_tags
, new_repo_tag
, entry
);
521 new_repo_tag
->tag_name
= strdup(got_ref_get_name(re
->ref
));
522 if (new_repo_tag
->tag_name
== NULL
) {
523 error
= got_error_from_errno("strdup");
533 error
= got_ref_resolve(&id
, repo
, re
->ref
);
538 got_object_tag_close(tag
);
539 error
= got_object_open_as_tag(&tag
, repo
, id
);
541 if (error
->code
!= GOT_ERR_OBJ_TYPE
)
543 /* "lightweight" tag */
544 error
= got_object_open_as_commit(&commit
, repo
, id
);
547 new_repo_tag
->tagger
=
548 strdup(got_object_commit_get_committer(commit
));
549 if (new_repo_tag
->tagger
== NULL
) {
550 error
= got_error_from_errno("strdup");
553 new_repo_tag
->tagger_time
=
554 got_object_commit_get_committer_time(commit
);
555 error
= got_object_id_str(&id_str
, id
);
559 new_repo_tag
->tagger
=
560 strdup(got_object_tag_get_tagger(tag
));
561 if (new_repo_tag
->tagger
== NULL
) {
562 error
= got_error_from_errno("strdup");
565 new_repo_tag
->tagger_time
=
566 got_object_tag_get_tagger_time(tag
);
567 error
= got_object_id_str(&id_str
,
568 got_object_tag_get_object_id(tag
));
573 new_repo_tag
->commit_id
= strdup(id_str
);
574 if (new_repo_tag
->commit_id
== NULL
)
577 if (commit_found
== 0 && qs
->commit
!= NULL
&&
578 strncmp(id_str
, qs
->commit
, strlen(id_str
)) != 0) {
579 TAILQ_REMOVE(&t
->repo_tags
, new_repo_tag
, entry
);
580 gotweb_free_repo_tag(new_repo_tag
);
586 * check for one more commit before breaking,
587 * so we know whether to navigate through briefs
588 * commits and summary
591 t
->tags_more_id
= strdup(new_repo_tag
->commit_id
);
592 if (t
->tags_more_id
== NULL
) {
593 error
= got_error_from_errno("strdup");
597 got_object_commit_close(commit
);
600 TAILQ_REMOVE(&t
->repo_tags
, new_repo_tag
, entry
);
601 gotweb_free_repo_tag(new_repo_tag
);
606 error
= got_object_commit_get_logmsg(&tag_commit0
,
610 got_object_commit_close(commit
);
613 tag_commit0
= strdup(got_object_tag_get_message(tag
));
614 if (tag_commit0
== NULL
) {
615 error
= got_error_from_errno("strdup");
620 tag_commit
= tag_commit0
;
621 while (*tag_commit
== '\n')
623 new_repo_tag
->tag_commit
= strdup(tag_commit
);
624 if (new_repo_tag
->tag_commit
== NULL
) {
625 error
= got_error_from_errno("strdup");
631 if (qs
->action
!= SUMMARY
&& qs
->action
!= TAGS
) {
632 commit_msg
= commit_msg0
;
633 while (*commit_msg
== '\n')
636 new_repo_tag
->commit_msg
= strdup(commit_msg
);
637 if (new_repo_tag
->commit_msg
== NULL
) {
638 error
= got_error_from_errno("strdup");
643 if (limit
&& --limit
== 0) {
652 got_object_commit_close(commit
);
654 got_object_tag_close(tag
);
655 got_ref_list_free(&refs
);
665 got_output_repo_tree(struct request
*c
, char **readme
,
666 int (*cb
)(struct template *, struct got_tree_entry
*))
668 const struct got_error
*error
= NULL
;
669 struct transport
*t
= c
->t
;
670 struct got_commit_object
*commit
= NULL
;
671 struct got_repository
*repo
= t
->repo
;
672 struct querystring
*qs
= t
->qs
;
673 struct repo_commit
*rc
= NULL
;
674 struct got_object_id
*tree_id
= NULL
, *commit_id
= NULL
;
675 struct got_reflist_head refs
;
676 struct got_tree_object
*tree
= NULL
;
677 struct got_tree_entry
*te
;
678 struct repo_dir
*repo_dir
= t
->repo_dir
;
681 char *escaped_name
= NULL
, *path
= NULL
;
687 rc
= TAILQ_FIRST(&t
->repo_commits
);
689 if (qs
->folder
!= NULL
) {
690 path
= strdup(qs
->folder
);
692 error
= got_error_from_errno("strdup");
696 error
= got_repo_map_path(&path
, repo
, repo_dir
->path
);
701 error
= got_repo_match_object_id(&commit_id
, NULL
, rc
->commit_id
,
702 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
706 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
710 error
= got_object_id_by_path(&tree_id
, repo
, commit
, path
);
714 error
= got_object_open_as_tree(&tree
, repo
, tree_id
);
718 nentries
= got_object_tree_get_nentries(tree
);
720 for (i
= 0; i
< nentries
; i
++) {
721 te
= got_object_tree_get_entry(tree
, i
);
723 name
= got_tree_entry_get_name(te
);
724 mode
= got_tree_entry_get_mode(te
);
725 if (!S_ISDIR(mode
) && (!strcasecmp(name
, "README") ||
726 !strcasecmp(name
, "README.md") ||
727 !strcasecmp(name
, "README.txt"))) {
729 *readme
= strdup(name
);
732 if (cb(c
->tp
, te
) == -1) {
733 error
= got_error(GOT_ERR_CANCELLED
);
740 got_ref_list_free(&refs
);
742 got_object_commit_close(commit
);
744 got_object_tree_close(tree
);
750 if (error
->code
!= GOT_ERR_CANCELLED
)
751 log_warnx("%s: %s", __func__
, error
->msg
);
757 const struct got_error
*
758 got_open_blob_for_output(struct got_blob_object
**blob
, int *fd
,
759 int *binary
, struct request
*c
, const char *directory
, const char *file
,
760 const char *commitstr
)
762 const struct got_error
*error
= NULL
;
763 struct got_repository
*repo
= c
->t
->repo
;
764 struct got_commit_object
*commit
= NULL
;
765 struct got_object_id
*commit_id
= NULL
;
766 struct got_reflist_head refs
;
767 char *path
= NULL
, *in_repo_path
= NULL
;
776 error
= got_ref_list(&refs
, repo
, "refs/heads",
777 got_ref_cmp_by_name
, NULL
);
781 if (asprintf(&path
, "%s%s%s", directory
? directory
: "",
782 directory
? "/" : "", file
) == -1) {
783 error
= got_error_from_errno("asprintf");
787 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
791 if (commitstr
== NULL
)
792 commitstr
= GOT_REF_HEAD
;
794 error
= got_repo_match_object_id(&commit_id
, NULL
, commitstr
,
795 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
799 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
803 error
= got_object_id_by_path(&commit_id
, repo
, commit
, in_repo_path
);
807 if (commit_id
== NULL
) {
808 error
= got_error(GOT_ERR_NO_OBJ
);
812 error
= got_object_get_type(&obj_type
, repo
, commit_id
);
816 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
817 error
= got_error(GOT_ERR_OBJ_TYPE
);
821 error
= got_gotweb_dupfd(&c
->priv_fd
[BLOB_FD_1
], fd
);
825 error
= got_object_open_as_blob(blob
, repo
, commit_id
, BUF
, *fd
);
829 error
= got_object_blob_is_binary(binary
, *blob
);
835 got_object_commit_close(commit
);
841 got_object_blob_close(*blob
);
846 got_ref_list_free(&refs
);
854 got_output_blob_by_lines(struct template *tp
, struct got_blob_object
*blob
,
855 int (*cb
)(struct template *, const char *, size_t))
857 const struct got_error
*err
;
864 err
= got_object_blob_getline(&line
, &linelen
, &linesize
,
866 if (err
|| linelen
== -1)
869 if (cb(tp
, line
, lineno
) == -1) {
870 err
= got_error(GOT_ERR_CANCELLED
);
878 if (err
->code
!= GOT_ERR_CANCELLED
)
879 log_warnx("%s: got_object_blob_getline failed: %s",
886 struct blame_cb_args
{
887 struct blame_line
*lines
;
893 struct got_repository
*repo
;
895 got_render_blame_line_cb cb
;
898 static const struct got_error
*
899 got_gotweb_blame_cb(void *arg
, int nlines
, int lineno
,
900 struct got_commit_object
*commit
, struct got_object_id
*id
)
902 const struct got_error
*err
= NULL
;
903 struct blame_cb_args
*a
= arg
;
904 struct blame_line
*bline
;
905 struct request
*c
= a
->c
;
910 time_t committer_time
;
912 if (nlines
!= a
->nlines
||
913 (lineno
!= -1 && lineno
< 1) || lineno
> a
->nlines
)
914 return got_error(GOT_ERR_RANGE
);
917 return NULL
; /* no change in this commit */
919 /* Annotate this line. */
920 bline
= &a
->lines
[lineno
- 1];
921 if (bline
->annotated
)
923 err
= got_object_id_str(&bline
->id_str
, id
);
927 bline
->committer
= strdup(got_object_commit_get_committer(commit
));
928 if (bline
->committer
== NULL
) {
929 err
= got_error_from_errno("strdup");
933 committer_time
= got_object_commit_get_committer_time(commit
);
934 if (gmtime_r(&committer_time
, &tm
) == NULL
)
935 return got_error_from_errno("gmtime_r");
936 if (strftime(bline
->datebuf
, sizeof(bline
->datebuf
), "%F", &tm
) == 0) {
937 err
= got_error(GOT_ERR_NO_SPACE
);
940 bline
->annotated
= 1;
942 /* Print lines annotated so far. */
943 bline
= &a
->lines
[a
->lineno_cur
- 1];
944 if (!bline
->annotated
)
947 offset
= a
->line_offsets
[a
->lineno_cur
- 1];
948 if (fseeko(a
->f
, offset
, SEEK_SET
) == -1) {
949 err
= got_error_from_errno("fseeko");
953 while (a
->lineno_cur
<= a
->nlines
&& bline
->annotated
) {
954 if (getline(&line
, &linesize
, a
->f
) == -1) {
956 err
= got_error_from_errno("getline");
960 if (a
->cb(c
->tp
, line
, bline
, a
->nlines_prec
,
961 a
->lineno_cur
) == -1) {
962 err
= got_error(GOT_ERR_CANCELLED
);
967 bline
= &a
->lines
[a
->lineno_cur
- 1];
974 const struct got_error
*
975 got_output_file_blame(struct request
*c
, got_render_blame_line_cb cb
)
977 const struct got_error
*error
= NULL
;
978 struct transport
*t
= c
->t
;
979 struct got_repository
*repo
= t
->repo
;
980 struct querystring
*qs
= c
->t
->qs
;
981 struct got_object_id
*obj_id
= NULL
, *commit_id
= NULL
;
982 struct got_commit_object
*commit
= NULL
;
983 struct got_reflist_head refs
;
984 struct got_blob_object
*blob
= NULL
;
985 char *path
= NULL
, *in_repo_path
= NULL
;
986 struct blame_cb_args bca
;
987 int i
, obj_type
, blobfd
= -1, fd1
= -1, fd2
= -1;
989 FILE *f1
= NULL
, *f2
= NULL
;
996 if (asprintf(&path
, "%s/%s", qs
->folder
, qs
->file
) == -1) {
997 error
= got_error_from_errno("asprintf");
1001 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
1005 error
= got_repo_match_object_id(&commit_id
, NULL
, qs
->commit
,
1006 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
1010 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
1014 error
= got_object_id_by_path(&obj_id
, repo
, commit
, in_repo_path
);
1018 error
= got_object_get_type(&obj_type
, repo
, obj_id
);
1022 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
1023 error
= got_error(GOT_ERR_OBJ_TYPE
);
1027 error
= got_gotweb_openfile(&bca
.f
, &c
->priv_fd
[BLAME_FD_1
]);
1031 error
= got_gotweb_dupfd(&c
->priv_fd
[BLAME_FD_2
], &blobfd
);
1035 error
= got_object_open_as_blob(&blob
, repo
, obj_id
, BUF
, blobfd
);
1039 error
= got_object_blob_dump_to_file(&filesize
, &bca
.nlines
,
1040 &bca
.line_offsets
, bca
.f
, blob
);
1041 if (error
|| bca
.nlines
== 0)
1044 /* Don't include \n at EOF in the blame line count. */
1045 if (bca
.line_offsets
[bca
.nlines
- 1] == filesize
)
1048 bca
.lines
= calloc(bca
.nlines
, sizeof(*bca
.lines
));
1049 if (bca
.lines
== NULL
) {
1050 error
= got_error_from_errno("calloc");
1054 bca
.nlines_prec
= 0;
1063 error
= got_gotweb_dupfd(&c
->priv_fd
[BLAME_FD_3
], &fd1
);
1067 error
= got_gotweb_dupfd(&c
->priv_fd
[BLAME_FD_4
], &fd2
);
1071 error
= got_gotweb_openfile(&f1
, &c
->priv_fd
[BLAME_FD_5
]);
1075 error
= got_gotweb_openfile(&f2
, &c
->priv_fd
[BLAME_FD_6
]);
1079 error
= got_blame(in_repo_path
, commit_id
, repo
,
1080 GOT_DIFF_ALGORITHM_MYERS
, got_gotweb_blame_cb
, &bca
, NULL
, NULL
,
1085 free(bca
.line_offsets
);
1086 for (i
= 0; i
< bca
.nlines
; i
++) {
1087 struct blame_line
*bline
= &bca
.lines
[i
];
1088 free(bline
->id_str
);
1089 free(bline
->committer
);
1093 if (blobfd
!= -1 && close(blobfd
) == -1 && error
== NULL
)
1094 error
= got_error_from_errno("close");
1095 if (fd1
!= -1 && close(fd1
) == -1 && error
== NULL
)
1096 error
= got_error_from_errno("close");
1097 if (fd2
!= -1 && close(fd2
) == -1 && error
== NULL
)
1098 error
= got_error_from_errno("close");
1100 const struct got_error
*bca_err
= got_gotweb_closefile(bca
.f
);
1105 const struct got_error
*f1_err
= got_gotweb_closefile(f1
);
1110 const struct got_error
*f2_err
= got_gotweb_closefile(f2
);
1115 got_object_commit_close(commit
);
1117 got_object_blob_close(blob
);
1122 got_ref_list_free(&refs
);
1126 const struct got_error
*
1127 got_open_diff_for_output(FILE **fp
, struct request
*c
)
1129 const struct got_error
*error
= NULL
;
1130 struct transport
*t
= c
->t
;
1131 struct got_repository
*repo
= t
->repo
;
1132 struct repo_commit
*rc
= NULL
;
1133 struct got_object_id
*id1
= NULL
, *id2
= NULL
;
1134 struct got_reflist_head refs
;
1135 FILE *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
1136 int obj_type
, fd1
= -1, fd2
= -1;
1142 error
= got_gotweb_openfile(&f1
, &c
->priv_fd
[DIFF_FD_1
]);
1146 error
= got_gotweb_openfile(&f2
, &c
->priv_fd
[DIFF_FD_2
]);
1150 error
= got_gotweb_openfile(&f3
, &c
->priv_fd
[DIFF_FD_3
]);
1154 rc
= TAILQ_FIRST(&t
->repo_commits
);
1156 if (rc
->parent_id
!= NULL
&&
1157 strncmp(rc
->parent_id
, "/dev/null", 9) != 0) {
1158 error
= got_repo_match_object_id(&id1
, NULL
,
1159 rc
->parent_id
, GOT_OBJ_TYPE_ANY
,
1165 error
= got_repo_match_object_id(&id2
, NULL
, rc
->commit_id
,
1166 GOT_OBJ_TYPE_ANY
, &refs
, repo
);
1170 error
= got_object_get_type(&obj_type
, repo
, id2
);
1174 error
= got_gotweb_dupfd(&c
->priv_fd
[DIFF_FD_4
], &fd1
);
1178 error
= got_gotweb_dupfd(&c
->priv_fd
[DIFF_FD_5
], &fd2
);
1183 case GOT_OBJ_TYPE_BLOB
:
1184 error
= got_diff_objects_as_blobs(NULL
, NULL
, f1
, f2
, fd1
, fd2
,
1185 id1
, id2
, NULL
, NULL
, GOT_DIFF_ALGORITHM_MYERS
, 3, 0, 0,
1188 case GOT_OBJ_TYPE_TREE
:
1189 error
= got_diff_objects_as_trees(NULL
, NULL
, f1
, f2
, fd1
, fd2
,
1190 id1
, id2
, NULL
, "", "", GOT_DIFF_ALGORITHM_MYERS
, 3, 0, 0,
1193 case GOT_OBJ_TYPE_COMMIT
:
1194 error
= got_diff_objects_as_commits(NULL
, NULL
, f1
, f2
, fd1
,
1195 fd2
, id1
, id2
, NULL
, GOT_DIFF_ALGORITHM_MYERS
, 3, 0, 0,
1199 error
= got_error(GOT_ERR_OBJ_TYPE
);
1204 if (fseek(f3
, 0, SEEK_SET
) == -1) {
1205 error
= got_ferror(f3
, GOT_ERR_IO
);
1212 if (fd1
!= -1 && close(fd1
) == -1 && error
== NULL
)
1213 error
= got_error_from_errno("close");
1214 if (fd2
!= -1 && close(fd2
) == -1 && error
== NULL
)
1215 error
= got_error_from_errno("close");
1217 const struct got_error
*f1_err
= got_gotweb_closefile(f1
);
1222 const struct got_error
*f2_err
= got_gotweb_closefile(f2
);
1227 got_gotweb_closefile(f3
);
1230 got_ref_list_free(&refs
);
1236 static const struct got_error
*
1237 got_init_repo_commit(struct repo_commit
**rc
)
1239 *rc
= calloc(1, sizeof(**rc
));
1241 return got_error_from_errno2(__func__
, "calloc");
1244 (*rc
)->refs_str
= NULL
;
1245 (*rc
)->commit_id
= NULL
;
1246 (*rc
)->committer
= NULL
;
1247 (*rc
)->author
= NULL
;
1248 (*rc
)->parent_id
= NULL
;
1249 (*rc
)->tree_id
= NULL
;
1250 (*rc
)->commit_msg
= NULL
;
1255 static const struct got_error
*
1256 got_init_repo_tag(struct repo_tag
**rt
)
1258 *rt
= calloc(1, sizeof(**rt
));
1260 return got_error_from_errno2(__func__
, "calloc");
1262 (*rt
)->commit_id
= NULL
;
1263 (*rt
)->tag_name
= NULL
;
1264 (*rt
)->tag_commit
= NULL
;
1265 (*rt
)->commit_msg
= NULL
;
1266 (*rt
)->tagger
= NULL
;