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 static const struct got_error
*
120 get_ref_commit_timestamp(time_t *timestamp
,
121 struct got_reference
*ref
, struct got_repository
*repo
)
123 const struct got_error
*error
;
124 struct got_object_id
*id
= NULL
;
126 struct got_commit_object
*commit
= NULL
;
128 /* We might have a cached timestamp available. */
129 *timestamp
= got_ref_get_cached_committer_time(ref
);
133 error
= got_ref_resolve(&id
, repo
, ref
);
137 error
= got_object_get_type(&obj_type
, repo
, id
);
138 if (error
|| obj_type
!= GOT_OBJ_TYPE_COMMIT
)
141 error
= got_object_open_as_commit(&commit
, repo
, id
);
145 *timestamp
= got_object_commit_get_committer_time(commit
);
149 got_object_commit_close(commit
);
155 * Find the youngest branch tip in the repository, or the age of
156 * a specific branch tip if a name was provided by the caller.
158 const struct got_error
*
159 got_get_repo_age(time_t *repo_age
, struct request
*c
, const char *refname
)
161 const struct got_error
*error
= NULL
;
162 struct transport
*t
= c
->t
;
163 struct got_repository
*repo
= t
->repo
;
168 struct got_reference
*ref
;
170 error
= got_ref_open(&ref
, repo
, refname
, 0);
174 error
= get_ref_commit_timestamp(repo_age
, ref
, repo
);
177 struct got_reflist_head refs
;
178 struct got_reflist_entry
*re
;
182 error
= got_ref_list(&refs
, repo
, "refs/heads",
183 got_ref_cmp_by_commit_timestamp_descending
, repo
);
187 re
= TAILQ_FIRST(&refs
);
189 error
= get_ref_commit_timestamp(repo_age
,
192 got_ref_list_free(&refs
);
198 static const struct got_error
*
199 got_get_repo_commit(struct request
*c
, struct repo_commit
*repo_commit
,
200 struct got_commit_object
*commit
, struct got_reflist_head
*refs
,
201 struct got_object_id
*id
)
203 const struct got_error
*error
= NULL
;
204 struct got_reflist_entry
*re
;
205 struct got_object_id
*id2
= NULL
;
206 struct got_object_qid
*parent_id
;
207 struct transport
*t
= c
->t
;
208 struct querystring
*qs
= c
->t
->qs
;
209 char *commit_msg
= NULL
, *commit_msg0
;
211 TAILQ_FOREACH(re
, refs
, entry
) {
214 struct got_tag_object
*tag
= NULL
;
215 struct got_object_id
*ref_id
;
218 if (got_ref_is_symbolic(re
->ref
))
221 name
= got_ref_get_name(re
->ref
);
222 if (strncmp(name
, "refs/", 5) == 0)
224 if (strncmp(name
, "got/", 4) == 0)
226 if (strncmp(name
, "heads/", 6) == 0)
228 if (strncmp(name
, "remotes/", 8) == 0) {
230 if (strstr(name
, "/" GOT_REF_HEAD
) != NULL
)
233 error
= got_ref_resolve(&ref_id
, t
->repo
, re
->ref
);
236 if (strncmp(name
, "tags/", 5) == 0) {
237 error
= got_object_open_as_tag(&tag
, t
->repo
, ref_id
);
239 if (error
->code
!= GOT_ERR_OBJ_TYPE
) {
244 * Ref points at something other
251 cmp
= got_object_id_cmp(tag
?
252 got_object_tag_get_object_id(tag
) : ref_id
, id
);
255 got_object_tag_close(tag
);
258 s
= repo_commit
->refs_str
;
259 if (asprintf(&repo_commit
->refs_str
, "%s%s%s", s
? s
: "",
260 s
? ", " : "", name
) == -1) {
261 error
= got_error_from_errno("asprintf");
263 repo_commit
->refs_str
= NULL
;
269 error
= got_object_id_str(&repo_commit
->commit_id
, id
);
273 error
= got_object_id_str(&repo_commit
->tree_id
,
274 got_object_commit_get_tree_id(commit
));
278 if (qs
->action
== DIFF
|| qs
->action
== PATCH
) {
279 parent_id
= STAILQ_FIRST(
280 got_object_commit_get_parent_ids(commit
));
281 if (parent_id
!= NULL
) {
282 id2
= got_object_id_dup(&parent_id
->id
);
283 error
= got_object_id_str(&repo_commit
->parent_id
, id2
);
288 repo_commit
->parent_id
= strdup("/dev/null");
289 if (repo_commit
->parent_id
== NULL
) {
290 error
= got_error_from_errno("strdup");
296 repo_commit
->committer_time
=
297 got_object_commit_get_committer_time(commit
);
299 repo_commit
->author
=
300 strdup(got_object_commit_get_author(commit
));
301 if (repo_commit
->author
== NULL
) {
302 error
= got_error_from_errno("strdup");
305 repo_commit
->committer
=
306 strdup(got_object_commit_get_committer(commit
));
307 if (repo_commit
->committer
== NULL
) {
308 error
= got_error_from_errno("strdup");
311 error
= got_object_commit_get_logmsg(&commit_msg0
, commit
);
315 commit_msg
= commit_msg0
;
316 while (*commit_msg
== '\n')
319 repo_commit
->commit_msg
= strdup(commit_msg
);
320 if (repo_commit
->commit_msg
== NULL
)
321 error
= got_error_from_errno("strdup");
326 const struct got_error
*
327 got_get_repo_commits(struct request
*c
, size_t limit
)
329 const struct got_error
*error
= NULL
;
330 struct got_object_id
*id
= NULL
;
331 struct got_commit_graph
*graph
= NULL
;
332 struct got_commit_object
*commit
= NULL
;
333 struct got_reflist_head refs
;
334 struct got_reference
*ref
= NULL
;
335 struct repo_commit
*repo_commit
= NULL
;
336 struct server
*srv
= c
->srv
;
337 struct transport
*t
= c
->t
;
338 struct got_repository
*repo
= t
->repo
;
339 struct querystring
*qs
= t
->qs
;
340 struct repo_dir
*repo_dir
= t
->repo_dir
;
341 char *in_repo_path
= NULL
, *repo_path
= NULL
, *file_path
= NULL
;
345 return got_error(GOT_ERR_RANGE
);
349 * Traverse one commit more than requested to provide
358 if (qs
->file
!= NULL
&& *qs
->file
!= '\0')
359 if (asprintf(&file_path
, "%s/%s", qs
->folder
? qs
->folder
: "",
361 return got_error_from_errno("asprintf");
363 if (asprintf(&repo_path
, "%s/%s", srv
->repos_path
,
364 repo_dir
->name
) == -1) {
365 error
= got_error_from_errno("asprintf");
370 error
= got_repo_match_object_id_prefix(&id
, qs
->commit
,
371 GOT_OBJ_TYPE_COMMIT
, repo
);
375 error
= got_ref_open(&ref
, repo
, qs
->headref
, 0);
379 error
= got_ref_resolve(&id
, repo
, ref
);
384 error
= got_repo_map_path(&in_repo_path
, repo
, repo_path
);
388 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
392 if (qs
->file
!= NULL
&& *qs
->file
!= '\0') {
393 error
= got_commit_graph_open(&graph
, file_path
, 0);
397 error
= got_commit_graph_open(&graph
, in_repo_path
, 0);
402 error
= got_commit_graph_bfsort(graph
, id
, repo
, NULL
, NULL
);
407 struct got_object_id next_id
;
409 error
= got_commit_graph_iter_next(&next_id
, graph
, repo
, NULL
,
412 if (error
->code
== GOT_ERR_ITER_COMPLETED
)
417 error
= got_object_open_as_commit(&commit
, repo
, &next_id
);
421 error
= got_init_repo_commit(&repo_commit
);
425 error
= got_get_repo_commit(c
, repo_commit
, commit
,
428 gotweb_free_repo_commit(repo_commit
);
432 if (--limit
== 0 && chk_next
) {
433 t
->more_id
= strdup(repo_commit
->commit_id
);
434 if (t
->more_id
== NULL
)
435 error
= got_error_from_errno("strdup");
436 gotweb_free_repo_commit(repo_commit
);
440 TAILQ_INSERT_TAIL(&t
->repo_commits
, repo_commit
, entry
);
446 got_object_commit_close(commit
);
454 got_object_commit_close(commit
);
456 got_commit_graph_close(graph
);
457 got_ref_list_free(&refs
);
465 const struct got_error
*
466 got_get_repo_tags(struct request
*c
, size_t limit
)
468 const struct got_error
*error
= NULL
;
469 struct got_object_id
*id
= NULL
;
470 struct got_commit_object
*commit
= NULL
;
471 struct got_reflist_head refs
;
472 struct got_reference
*ref
;
473 struct got_reflist_entry
*re
;
474 struct server
*srv
= c
->srv
;
475 struct transport
*t
= c
->t
;
476 struct got_repository
*repo
= t
->repo
;
477 struct querystring
*qs
= t
->qs
;
478 struct repo_dir
*repo_dir
= t
->repo_dir
;
479 struct got_tag_object
*tag
= NULL
;
480 char *in_repo_path
= NULL
, *repo_path
= NULL
, *id_str
= NULL
;
481 char *tag_commit
= NULL
, *tag_commit0
= NULL
;
482 char *commit_msg
= NULL
, *commit_msg0
= NULL
;
483 int chk_next
= 0, chk_multi
= 1, commit_found
= 0;
488 return got_error(GOT_ERR_RANGE
);
490 if (asprintf(&repo_path
, "%s/%s", srv
->repos_path
,
491 repo_dir
->name
) == -1)
492 return got_error_from_errno("asprintf");
494 if (qs
->commit
== NULL
&& (qs
->action
== TAGS
|| qs
->action
== RSS
)) {
495 error
= got_ref_open(&ref
, repo
, qs
->headref
, 0);
498 error
= got_ref_resolve(&id
, repo
, ref
);
502 } else if (qs
->commit
== NULL
&& qs
->action
== TAG
) {
503 error
= got_error_msg(GOT_ERR_EOF
, "commit id missing");
506 error
= got_repo_match_object_id_prefix(&id
, qs
->commit
,
507 GOT_OBJ_TYPE_COMMIT
, repo
);
512 if (qs
->action
!= SUMMARY
&& qs
->action
!= TAGS
) {
513 error
= got_object_open_as_commit(&commit
, repo
, id
);
516 error
= got_object_commit_get_logmsg(&commit_msg0
, commit
);
520 got_object_commit_close(commit
);
525 error
= got_repo_map_path(&in_repo_path
, repo
, repo_path
);
529 error
= got_ref_list(&refs
, repo
, "refs/tags", got_ref_cmp_tags
,
537 TAILQ_FOREACH(re
, &refs
, entry
) {
538 struct repo_tag
*new_repo_tag
= NULL
;
539 error
= got_init_repo_tag(&new_repo_tag
);
543 TAILQ_INSERT_TAIL(&t
->repo_tags
, new_repo_tag
, entry
);
545 new_repo_tag
->tag_name
= strdup(got_ref_get_name(re
->ref
));
546 if (new_repo_tag
->tag_name
== NULL
) {
547 error
= got_error_from_errno("strdup");
557 error
= got_ref_resolve(&id
, repo
, re
->ref
);
562 got_object_tag_close(tag
);
563 error
= got_object_open_as_tag(&tag
, repo
, id
);
565 if (error
->code
!= GOT_ERR_OBJ_TYPE
)
567 /* "lightweight" tag */
568 error
= got_object_open_as_commit(&commit
, repo
, id
);
571 new_repo_tag
->tagger
=
572 strdup(got_object_commit_get_committer(commit
));
573 if (new_repo_tag
->tagger
== NULL
) {
574 error
= got_error_from_errno("strdup");
577 new_repo_tag
->tagger_time
=
578 got_object_commit_get_committer_time(commit
);
579 error
= got_object_id_str(&id_str
, id
);
583 new_repo_tag
->tagger
=
584 strdup(got_object_tag_get_tagger(tag
));
585 if (new_repo_tag
->tagger
== NULL
) {
586 error
= got_error_from_errno("strdup");
589 new_repo_tag
->tagger_time
=
590 got_object_tag_get_tagger_time(tag
);
591 error
= got_object_id_str(&id_str
,
592 got_object_tag_get_object_id(tag
));
597 new_repo_tag
->commit_id
= strdup(id_str
);
598 if (new_repo_tag
->commit_id
== NULL
)
601 if (commit_found
== 0 && qs
->commit
!= NULL
&&
602 strncmp(id_str
, qs
->commit
, strlen(id_str
)) != 0) {
604 got_object_commit_close(commit
);
607 TAILQ_REMOVE(&t
->repo_tags
, new_repo_tag
, entry
);
608 gotweb_free_repo_tag(new_repo_tag
);
614 * check for one more commit before breaking,
615 * so we know whether to navigate through briefs
616 * commits and summary
619 t
->tags_more_id
= strdup(new_repo_tag
->commit_id
);
620 if (t
->tags_more_id
== NULL
) {
621 error
= got_error_from_errno("strdup");
625 got_object_commit_close(commit
);
628 TAILQ_REMOVE(&t
->repo_tags
, new_repo_tag
, entry
);
629 gotweb_free_repo_tag(new_repo_tag
);
634 error
= got_object_commit_get_logmsg(&tag_commit0
,
638 got_object_commit_close(commit
);
641 tag_commit0
= strdup(got_object_tag_get_message(tag
));
642 if (tag_commit0
== NULL
) {
643 error
= got_error_from_errno("strdup");
648 tag_commit
= tag_commit0
;
649 while (*tag_commit
== '\n')
651 new_repo_tag
->tag_commit
= strdup(tag_commit
);
652 if (new_repo_tag
->tag_commit
== NULL
) {
653 error
= got_error_from_errno("strdup");
659 if (qs
->action
!= SUMMARY
&& qs
->action
!= TAGS
) {
660 commit_msg
= commit_msg0
;
661 while (*commit_msg
== '\n')
664 new_repo_tag
->commit_msg
= strdup(commit_msg
);
665 if (new_repo_tag
->commit_msg
== NULL
) {
666 error
= got_error_from_errno("strdup");
671 if (limit
&& --limit
== 0) {
680 got_object_commit_close(commit
);
682 got_object_tag_close(tag
);
683 got_ref_list_free(&refs
);
693 got_output_repo_tree(struct request
*c
, char **readme
,
694 int (*cb
)(struct template *, struct got_tree_entry
*))
696 const struct got_error
*error
= NULL
;
697 struct transport
*t
= c
->t
;
698 struct got_commit_object
*commit
= NULL
;
699 struct got_repository
*repo
= t
->repo
;
700 struct querystring
*qs
= t
->qs
;
701 struct repo_commit
*rc
= NULL
;
702 struct got_object_id
*tree_id
= NULL
, *commit_id
= NULL
;
703 struct got_reflist_head refs
;
704 struct got_tree_object
*tree
= NULL
;
705 struct got_tree_entry
*te
;
706 struct repo_dir
*repo_dir
= t
->repo_dir
;
709 char *escaped_name
= NULL
, *path
= NULL
;
715 rc
= TAILQ_FIRST(&t
->repo_commits
);
717 if (qs
->folder
!= NULL
) {
718 path
= strdup(qs
->folder
);
720 error
= got_error_from_errno("strdup");
724 error
= got_repo_map_path(&path
, repo
, repo_dir
->path
);
729 error
= got_repo_match_object_id(&commit_id
, NULL
, rc
->commit_id
,
730 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
734 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
738 error
= got_object_id_by_path(&tree_id
, repo
, commit
, path
);
742 error
= got_object_open_as_tree(&tree
, repo
, tree_id
);
746 nentries
= got_object_tree_get_nentries(tree
);
748 for (i
= 0; i
< nentries
; i
++) {
749 te
= got_object_tree_get_entry(tree
, i
);
751 name
= got_tree_entry_get_name(te
);
752 mode
= got_tree_entry_get_mode(te
);
753 if (!S_ISDIR(mode
) && (!strcasecmp(name
, "README") ||
754 !strcasecmp(name
, "README.md") ||
755 !strcasecmp(name
, "README.txt"))) {
757 *readme
= strdup(name
);
760 if (cb(c
->tp
, te
) == -1) {
761 error
= got_error(GOT_ERR_CANCELLED
);
768 got_ref_list_free(&refs
);
770 got_object_commit_close(commit
);
772 got_object_tree_close(tree
);
778 if (error
->code
!= GOT_ERR_CANCELLED
)
779 log_warnx("%s: %s", __func__
, error
->msg
);
785 const struct got_error
*
786 got_open_blob_for_output(struct got_blob_object
**blob
, int *fd
,
787 int *binary
, struct request
*c
, const char *directory
, const char *file
,
788 const char *commitstr
)
790 const struct got_error
*error
= NULL
;
791 struct got_repository
*repo
= c
->t
->repo
;
792 struct got_commit_object
*commit
= NULL
;
793 struct got_object_id
*commit_id
= NULL
;
794 struct got_object_id
*blob_id
= NULL
;
795 struct got_reflist_head refs
;
796 char *path
= NULL
, *in_repo_path
= NULL
;
805 error
= got_ref_list(&refs
, repo
, "refs/heads",
806 got_ref_cmp_by_name
, NULL
);
810 if (asprintf(&path
, "%s%s%s", directory
? directory
: "",
811 directory
? "/" : "", file
) == -1) {
812 error
= got_error_from_errno("asprintf");
816 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
820 if (commitstr
== NULL
)
821 commitstr
= GOT_REF_HEAD
;
823 error
= got_repo_match_object_id(&commit_id
, NULL
, commitstr
,
824 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
828 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
832 error
= got_object_id_by_path(&blob_id
, repo
, commit
, in_repo_path
);
836 if (blob_id
== NULL
) {
837 error
= got_error(GOT_ERR_NO_OBJ
);
841 error
= got_object_get_type(&obj_type
, repo
, blob_id
);
845 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
846 error
= got_error(GOT_ERR_OBJ_TYPE
);
850 error
= got_gotweb_dupfd(&c
->priv_fd
[BLOB_FD_1
], fd
);
854 error
= got_object_open_as_blob(blob
, repo
, blob_id
, BUF
, *fd
);
858 error
= got_object_blob_is_binary(binary
, *blob
);
864 got_object_commit_close(commit
);
870 got_object_blob_close(*blob
);
875 got_ref_list_free(&refs
);
884 got_output_blob_by_lines(struct template *tp
, struct got_blob_object
*blob
,
885 int (*cb
)(struct template *, const char *, size_t))
887 const struct got_error
*err
;
894 err
= got_object_blob_getline(&line
, &linelen
, &linesize
,
896 if (err
|| linelen
== -1)
899 if (cb(tp
, line
, lineno
) == -1) {
900 err
= got_error(GOT_ERR_CANCELLED
);
908 if (err
->code
!= GOT_ERR_CANCELLED
)
909 log_warnx("%s: got_object_blob_getline failed: %s",
916 struct blame_cb_args
{
917 struct blame_line
*lines
;
923 struct got_repository
*repo
;
925 got_render_blame_line_cb cb
;
928 static const struct got_error
*
929 got_gotweb_blame_cb(void *arg
, int nlines
, int lineno
,
930 struct got_commit_object
*commit
, struct got_object_id
*id
)
932 const struct got_error
*err
= NULL
;
933 struct blame_cb_args
*a
= arg
;
934 struct blame_line
*bline
;
935 struct request
*c
= a
->c
;
940 time_t committer_time
;
942 if (nlines
!= a
->nlines
||
943 (lineno
!= -1 && lineno
< 1) || lineno
> a
->nlines
)
944 return got_error(GOT_ERR_RANGE
);
947 return NULL
; /* no change in this commit */
949 /* Annotate this line. */
950 bline
= &a
->lines
[lineno
- 1];
951 if (bline
->annotated
)
953 err
= got_object_id_str(&bline
->id_str
, id
);
957 bline
->committer
= strdup(got_object_commit_get_committer(commit
));
958 if (bline
->committer
== NULL
) {
959 err
= got_error_from_errno("strdup");
963 committer_time
= got_object_commit_get_committer_time(commit
);
964 if (gmtime_r(&committer_time
, &tm
) == NULL
)
965 return got_error_from_errno("gmtime_r");
966 if (strftime(bline
->datebuf
, sizeof(bline
->datebuf
), "%F", &tm
) == 0) {
967 err
= got_error(GOT_ERR_NO_SPACE
);
970 bline
->annotated
= 1;
972 /* Print lines annotated so far. */
973 bline
= &a
->lines
[a
->lineno_cur
- 1];
974 if (!bline
->annotated
)
977 offset
= a
->line_offsets
[a
->lineno_cur
- 1];
978 if (fseeko(a
->f
, offset
, SEEK_SET
) == -1) {
979 err
= got_error_from_errno("fseeko");
983 while (a
->lineno_cur
<= a
->nlines
&& bline
->annotated
) {
984 if (getline(&line
, &linesize
, a
->f
) == -1) {
986 err
= got_error_from_errno("getline");
990 if (a
->cb(c
->tp
, line
, bline
, a
->nlines_prec
,
991 a
->lineno_cur
) == -1) {
992 err
= got_error(GOT_ERR_CANCELLED
);
997 bline
= &a
->lines
[a
->lineno_cur
- 1];
1004 const struct got_error
*
1005 got_output_file_blame(struct request
*c
, got_render_blame_line_cb cb
)
1007 const struct got_error
*error
= NULL
;
1008 struct transport
*t
= c
->t
;
1009 struct got_repository
*repo
= t
->repo
;
1010 struct querystring
*qs
= c
->t
->qs
;
1011 struct got_object_id
*obj_id
= NULL
, *commit_id
= NULL
;
1012 struct got_commit_object
*commit
= NULL
;
1013 struct got_reflist_head refs
;
1014 struct got_blob_object
*blob
= NULL
;
1015 char *path
= NULL
, *in_repo_path
= NULL
;
1016 struct blame_cb_args bca
;
1017 int i
, obj_type
, blobfd
= -1, fd1
= -1, fd2
= -1;
1019 FILE *f1
= NULL
, *f2
= NULL
;
1026 if (asprintf(&path
, "%s/%s", qs
->folder
, qs
->file
) == -1) {
1027 error
= got_error_from_errno("asprintf");
1031 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
1035 error
= got_repo_match_object_id(&commit_id
, NULL
, qs
->commit
,
1036 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
1040 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
1044 error
= got_object_id_by_path(&obj_id
, repo
, commit
, in_repo_path
);
1048 error
= got_object_get_type(&obj_type
, repo
, obj_id
);
1052 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
1053 error
= got_error(GOT_ERR_OBJ_TYPE
);
1057 error
= got_gotweb_openfile(&bca
.f
, &c
->priv_fd
[BLAME_FD_1
]);
1061 error
= got_gotweb_dupfd(&c
->priv_fd
[BLAME_FD_2
], &blobfd
);
1065 error
= got_object_open_as_blob(&blob
, repo
, obj_id
, BUF
, blobfd
);
1069 error
= got_object_blob_dump_to_file(&filesize
, &bca
.nlines
,
1070 &bca
.line_offsets
, bca
.f
, blob
);
1071 if (error
|| bca
.nlines
== 0)
1074 /* Don't include \n at EOF in the blame line count. */
1075 if (bca
.line_offsets
[bca
.nlines
- 1] == filesize
)
1078 bca
.lines
= calloc(bca
.nlines
, sizeof(*bca
.lines
));
1079 if (bca
.lines
== NULL
) {
1080 error
= got_error_from_errno("calloc");
1084 bca
.nlines_prec
= 0;
1093 error
= got_gotweb_dupfd(&c
->priv_fd
[BLAME_FD_3
], &fd1
);
1097 error
= got_gotweb_dupfd(&c
->priv_fd
[BLAME_FD_4
], &fd2
);
1101 error
= got_gotweb_openfile(&f1
, &c
->priv_fd
[BLAME_FD_5
]);
1105 error
= got_gotweb_openfile(&f2
, &c
->priv_fd
[BLAME_FD_6
]);
1109 error
= got_blame(in_repo_path
, commit_id
, repo
,
1110 GOT_DIFF_ALGORITHM_MYERS
, got_gotweb_blame_cb
, &bca
, NULL
, NULL
,
1115 free(bca
.line_offsets
);
1116 for (i
= 0; i
< bca
.nlines
; i
++) {
1117 struct blame_line
*bline
= &bca
.lines
[i
];
1118 free(bline
->id_str
);
1119 free(bline
->committer
);
1123 if (blobfd
!= -1 && close(blobfd
) == -1 && error
== NULL
)
1124 error
= got_error_from_errno("close");
1125 if (fd1
!= -1 && close(fd1
) == -1 && error
== NULL
)
1126 error
= got_error_from_errno("close");
1127 if (fd2
!= -1 && close(fd2
) == -1 && error
== NULL
)
1128 error
= got_error_from_errno("close");
1130 const struct got_error
*bca_err
= got_gotweb_closefile(bca
.f
);
1135 const struct got_error
*f1_err
= got_gotweb_closefile(f1
);
1140 const struct got_error
*f2_err
= got_gotweb_closefile(f2
);
1145 got_object_commit_close(commit
);
1147 got_object_blob_close(blob
);
1152 got_ref_list_free(&refs
);
1156 const struct got_error
*
1157 got_open_diff_for_output(FILE **fp
, struct request
*c
)
1159 const struct got_error
*error
= NULL
;
1160 struct transport
*t
= c
->t
;
1161 struct got_repository
*repo
= t
->repo
;
1162 struct repo_commit
*rc
= NULL
;
1163 struct got_object_id
*id1
= NULL
, *id2
= NULL
;
1164 struct got_reflist_head refs
;
1165 FILE *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
1166 int obj_type
, fd1
= -1, fd2
= -1;
1172 error
= got_gotweb_openfile(&f1
, &c
->priv_fd
[DIFF_FD_1
]);
1176 error
= got_gotweb_openfile(&f2
, &c
->priv_fd
[DIFF_FD_2
]);
1180 error
= got_gotweb_openfile(&f3
, &c
->priv_fd
[DIFF_FD_3
]);
1184 rc
= TAILQ_FIRST(&t
->repo_commits
);
1186 if (rc
->parent_id
!= NULL
&&
1187 strncmp(rc
->parent_id
, "/dev/null", 9) != 0) {
1188 error
= got_repo_match_object_id(&id1
, NULL
,
1189 rc
->parent_id
, GOT_OBJ_TYPE_ANY
,
1195 error
= got_repo_match_object_id(&id2
, NULL
, rc
->commit_id
,
1196 GOT_OBJ_TYPE_ANY
, &refs
, repo
);
1200 error
= got_object_get_type(&obj_type
, repo
, id2
);
1204 error
= got_gotweb_dupfd(&c
->priv_fd
[DIFF_FD_4
], &fd1
);
1208 error
= got_gotweb_dupfd(&c
->priv_fd
[DIFF_FD_5
], &fd2
);
1213 case GOT_OBJ_TYPE_BLOB
:
1214 error
= got_diff_objects_as_blobs(NULL
, NULL
, f1
, f2
, fd1
, fd2
,
1215 id1
, id2
, NULL
, NULL
, GOT_DIFF_ALGORITHM_MYERS
, 3, 0, 0,
1218 case GOT_OBJ_TYPE_TREE
:
1219 error
= got_diff_objects_as_trees(NULL
, NULL
, f1
, f2
, fd1
, fd2
,
1220 id1
, id2
, NULL
, "", "", GOT_DIFF_ALGORITHM_MYERS
, 3, 0, 0,
1223 case GOT_OBJ_TYPE_COMMIT
:
1224 error
= got_diff_objects_as_commits(NULL
, NULL
, f1
, f2
, fd1
,
1225 fd2
, id1
, id2
, NULL
, GOT_DIFF_ALGORITHM_MYERS
, 3, 0, 0,
1229 error
= got_error(GOT_ERR_OBJ_TYPE
);
1234 if (fseek(f3
, 0, SEEK_SET
) == -1) {
1235 error
= got_ferror(f3
, GOT_ERR_IO
);
1242 if (fd1
!= -1 && close(fd1
) == -1 && error
== NULL
)
1243 error
= got_error_from_errno("close");
1244 if (fd2
!= -1 && close(fd2
) == -1 && error
== NULL
)
1245 error
= got_error_from_errno("close");
1247 const struct got_error
*f1_err
= got_gotweb_closefile(f1
);
1252 const struct got_error
*f2_err
= got_gotweb_closefile(f2
);
1257 got_gotweb_closefile(f3
);
1260 got_ref_list_free(&refs
);
1266 static const struct got_error
*
1267 got_init_repo_commit(struct repo_commit
**rc
)
1269 *rc
= calloc(1, sizeof(**rc
));
1271 return got_error_from_errno2(__func__
, "calloc");
1274 (*rc
)->refs_str
= NULL
;
1275 (*rc
)->commit_id
= NULL
;
1276 (*rc
)->committer
= NULL
;
1277 (*rc
)->author
= NULL
;
1278 (*rc
)->parent_id
= NULL
;
1279 (*rc
)->tree_id
= NULL
;
1280 (*rc
)->commit_msg
= NULL
;
1285 static const struct got_error
*
1286 got_init_repo_tag(struct repo_tag
**rt
)
1288 *rt
= calloc(1, sizeof(**rt
));
1290 return got_error_from_errno2(__func__
, "calloc");
1292 (*rt
)->commit_id
= NULL
;
1293 (*rt
)->tag_name
= NULL
;
1294 (*rt
)->tag_commit
= NULL
;
1295 (*rt
)->commit_msg
= NULL
;
1296 (*rt
)->tagger
= NULL
;