Blame


1 8a35f56c 2022-07-16 thomas /*
2 8a35f56c 2022-07-16 thomas * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 8a35f56c 2022-07-16 thomas *
5 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
6 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
7 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
8 8a35f56c 2022-07-16 thomas *
9 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 8a35f56c 2022-07-16 thomas */
17 4fccd2fe 2023-03-08 thomas
18 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
19 8a35f56c 2022-07-16 thomas
20 3e12c168 2022-07-16 thomas #include <sys/queue.h>
21 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
22 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
23 8a35f56c 2022-07-16 thomas
24 8a35f56c 2022-07-16 thomas #include <event.h>
25 588a8092 2023-02-23 thomas #include <imsg.h>
26 8a35f56c 2022-07-16 thomas #include <stdlib.h>
27 8a35f56c 2022-07-16 thomas #include <stdio.h>
28 8a35f56c 2022-07-16 thomas #include <string.h>
29 8a35f56c 2022-07-16 thomas #include <unistd.h>
30 8a35f56c 2022-07-16 thomas
31 8a35f56c 2022-07-16 thomas #include "got_error.h"
32 8a35f56c 2022-07-16 thomas #include "got_object.h"
33 8a35f56c 2022-07-16 thomas #include "got_reference.h"
34 8a35f56c 2022-07-16 thomas #include "got_repository.h"
35 8a35f56c 2022-07-16 thomas #include "got_path.h"
36 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
37 8a35f56c 2022-07-16 thomas #include "got_diff.h"
38 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
39 8a35f56c 2022-07-16 thomas #include "got_blame.h"
40 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
41 8a35f56c 2022-07-16 thomas
42 8a35f56c 2022-07-16 thomas #include "proc.h"
43 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
44 8a35f56c 2022-07-16 thomas
45 8a35f56c 2022-07-16 thomas static const struct got_error *got_init_repo_commit(struct repo_commit **);
46 8a35f56c 2022-07-16 thomas static const struct got_error *got_init_repo_tag(struct repo_tag **);
47 8a35f56c 2022-07-16 thomas static const struct got_error *got_get_repo_commit(struct request *,
48 8a35f56c 2022-07-16 thomas struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
49 8a35f56c 2022-07-16 thomas struct got_object_id *);
50 8a35f56c 2022-07-16 thomas static const struct got_error *got_gotweb_dupfd(int *, int *);
51 04b2c111 2023-05-16 thomas static const struct got_error *got_gotweb_openfile(FILE **, int *);
52 8a35f56c 2022-07-16 thomas static const struct got_error *got_gotweb_blame_cb(void *, int, int,
53 8a35f56c 2022-07-16 thomas struct got_commit_object *,struct got_object_id *);
54 8a35f56c 2022-07-16 thomas
55 dccd05b4 2023-01-10 thomas const struct got_error *
56 d00235d8 2023-05-17 thomas got_gotweb_closefile(FILE *f)
57 8a35f56c 2022-07-16 thomas {
58 985ec13b 2023-05-17 thomas const struct got_error *err = NULL;
59 985ec13b 2023-05-17 thomas
60 8a35f56c 2022-07-16 thomas if (fseek(f, 0, SEEK_SET) == -1)
61 985ec13b 2023-05-17 thomas err = got_error_from_errno("fseek");
62 8a35f56c 2022-07-16 thomas
63 985ec13b 2023-05-17 thomas if (err == NULL && ftruncate(fileno(f), 0) == -1)
64 985ec13b 2023-05-17 thomas err = got_error_from_errno("ftruncate");
65 8a35f56c 2022-07-16 thomas
66 985ec13b 2023-05-17 thomas if (fclose(f) == EOF && err == NULL)
67 985ec13b 2023-05-17 thomas err = got_error_from_errno("fclose");
68 8a35f56c 2022-07-16 thomas
69 985ec13b 2023-05-17 thomas return err;
70 8a35f56c 2022-07-16 thomas }
71 8a35f56c 2022-07-16 thomas
72 8a35f56c 2022-07-16 thomas static const struct got_error *
73 04b2c111 2023-05-16 thomas got_gotweb_openfile(FILE **f, int *priv_fd)
74 8a35f56c 2022-07-16 thomas {
75 04b2c111 2023-05-16 thomas int fd;
76 04b2c111 2023-05-16 thomas
77 04b2c111 2023-05-16 thomas fd = dup(*priv_fd);
78 04b2c111 2023-05-16 thomas if (fd == -1)
79 6e596ed0 2022-08-31 thomas return got_error_from_errno("dup");
80 8a35f56c 2022-07-16 thomas
81 04b2c111 2023-05-16 thomas *f = fdopen(fd, "w+");
82 8a35f56c 2022-07-16 thomas if (*f == NULL) {
83 04b2c111 2023-05-16 thomas close(fd);
84 e2af4cd7 2022-08-31 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
85 8a35f56c 2022-07-16 thomas }
86 8a35f56c 2022-07-16 thomas
87 e2af4cd7 2022-08-31 thomas return NULL;
88 8a35f56c 2022-07-16 thomas }
89 8a35f56c 2022-07-16 thomas
90 8a35f56c 2022-07-16 thomas static const struct got_error *
91 8a35f56c 2022-07-16 thomas got_gotweb_dupfd(int *priv_fd, int *fd)
92 8a35f56c 2022-07-16 thomas {
93 8a35f56c 2022-07-16 thomas *fd = dup(*priv_fd);
94 8a35f56c 2022-07-16 thomas
95 a66a4a50 2023-02-03 thomas if (*fd == -1)
96 a66a4a50 2023-02-03 thomas return got_error_from_errno("dup");
97 8a35f56c 2022-07-16 thomas
98 e2af4cd7 2022-08-31 thomas return NULL;
99 8a35f56c 2022-07-16 thomas }
100 8a35f56c 2022-07-16 thomas
101 8a35f56c 2022-07-16 thomas const struct got_error *
102 6c7f10f7 2022-11-23 thomas got_get_repo_owner(char **owner, struct request *c)
103 8a35f56c 2022-07-16 thomas {
104 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
105 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
106 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
107 8a35f56c 2022-07-16 thomas const char *gitconfig_owner;
108 8a35f56c 2022-07-16 thomas
109 8a35f56c 2022-07-16 thomas *owner = NULL;
110 8a35f56c 2022-07-16 thomas
111 8a35f56c 2022-07-16 thomas if (srv->show_repo_owner == 0)
112 8a35f56c 2022-07-16 thomas return NULL;
113 8a35f56c 2022-07-16 thomas
114 8a35f56c 2022-07-16 thomas gitconfig_owner = got_repo_get_gitconfig_owner(repo);
115 8a35f56c 2022-07-16 thomas if (gitconfig_owner) {
116 8a35f56c 2022-07-16 thomas *owner = strdup(gitconfig_owner);
117 2f26d334 2022-08-30 thomas if (*owner == NULL)
118 2f26d334 2022-08-30 thomas return got_error_from_errno("strdup");
119 2f26d334 2022-08-30 thomas } else {
120 2f26d334 2022-08-30 thomas *owner = strdup("");
121 8a35f56c 2022-07-16 thomas if (*owner == NULL)
122 8a35f56c 2022-07-16 thomas return got_error_from_errno("strdup");
123 8a35f56c 2022-07-16 thomas }
124 e2af4cd7 2022-08-31 thomas return NULL;
125 8a35f56c 2022-07-16 thomas }
126 8a35f56c 2022-07-16 thomas
127 8a35f56c 2022-07-16 thomas const struct got_error *
128 53bf32b8 2023-01-23 thomas got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
129 8a35f56c 2022-07-16 thomas {
130 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
131 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
132 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
133 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
134 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
135 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
136 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
137 8a35f56c 2022-07-16 thomas time_t committer_time = 0, cmp_time = 0;
138 8a35f56c 2022-07-16 thomas
139 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
140 8a35f56c 2022-07-16 thomas
141 8a35f56c 2022-07-16 thomas if (srv->show_repo_age == 0)
142 8a35f56c 2022-07-16 thomas return NULL;
143 8a35f56c 2022-07-16 thomas
144 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/heads",
145 8a35f56c 2022-07-16 thomas got_ref_cmp_by_name, NULL);
146 8a35f56c 2022-07-16 thomas if (error)
147 8a35f56c 2022-07-16 thomas goto done;
148 8a35f56c 2022-07-16 thomas
149 8a35f56c 2022-07-16 thomas /*
150 8a35f56c 2022-07-16 thomas * Find the youngest branch tip in the repository, or the age of
151 8a35f56c 2022-07-16 thomas * the a specific branch tip if a name was provided by the caller.
152 8a35f56c 2022-07-16 thomas */
153 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
154 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
155 8a35f56c 2022-07-16 thomas
156 8a35f56c 2022-07-16 thomas if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
157 8a35f56c 2022-07-16 thomas continue;
158 8a35f56c 2022-07-16 thomas
159 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, re->ref);
160 8a35f56c 2022-07-16 thomas if (error)
161 8a35f56c 2022-07-16 thomas goto done;
162 8a35f56c 2022-07-16 thomas
163 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
164 8a35f56c 2022-07-16 thomas free(id);
165 8a35f56c 2022-07-16 thomas if (error)
166 8a35f56c 2022-07-16 thomas goto done;
167 8a35f56c 2022-07-16 thomas
168 8a35f56c 2022-07-16 thomas committer_time =
169 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
170 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
171 8a35f56c 2022-07-16 thomas if (cmp_time < committer_time)
172 8a35f56c 2022-07-16 thomas cmp_time = committer_time;
173 8a35f56c 2022-07-16 thomas
174 8a35f56c 2022-07-16 thomas if (refname)
175 8a35f56c 2022-07-16 thomas break;
176 8a35f56c 2022-07-16 thomas }
177 8a35f56c 2022-07-16 thomas
178 53bf32b8 2023-01-23 thomas if (cmp_time != 0)
179 53bf32b8 2023-01-23 thomas *repo_age = cmp_time;
180 8a35f56c 2022-07-16 thomas done:
181 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
182 8a35f56c 2022-07-16 thomas return error;
183 8a35f56c 2022-07-16 thomas }
184 8a35f56c 2022-07-16 thomas
185 8a35f56c 2022-07-16 thomas static const struct got_error *
186 8a35f56c 2022-07-16 thomas got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
187 8a35f56c 2022-07-16 thomas struct got_commit_object *commit, struct got_reflist_head *refs,
188 8a35f56c 2022-07-16 thomas struct got_object_id *id)
189 8a35f56c 2022-07-16 thomas {
190 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
191 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
192 8a35f56c 2022-07-16 thomas struct got_object_id *id2 = NULL;
193 8a35f56c 2022-07-16 thomas struct got_object_qid *parent_id;
194 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
195 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
196 8a35f56c 2022-07-16 thomas char *commit_msg = NULL, *commit_msg0;
197 8a35f56c 2022-07-16 thomas
198 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, refs, entry) {
199 8a35f56c 2022-07-16 thomas char *s;
200 8a35f56c 2022-07-16 thomas const char *name;
201 8a35f56c 2022-07-16 thomas struct got_tag_object *tag = NULL;
202 8a35f56c 2022-07-16 thomas struct got_object_id *ref_id;
203 8a35f56c 2022-07-16 thomas int cmp;
204 8a35f56c 2022-07-16 thomas
205 8a35f56c 2022-07-16 thomas if (got_ref_is_symbolic(re->ref))
206 8a35f56c 2022-07-16 thomas continue;
207 8a35f56c 2022-07-16 thomas
208 8a35f56c 2022-07-16 thomas name = got_ref_get_name(re->ref);
209 8a35f56c 2022-07-16 thomas if (strncmp(name, "refs/", 5) == 0)
210 8a35f56c 2022-07-16 thomas name += 5;
211 8a35f56c 2022-07-16 thomas if (strncmp(name, "got/", 4) == 0)
212 8a35f56c 2022-07-16 thomas continue;
213 8a35f56c 2022-07-16 thomas if (strncmp(name, "heads/", 6) == 0)
214 8a35f56c 2022-07-16 thomas name += 6;
215 8a35f56c 2022-07-16 thomas if (strncmp(name, "remotes/", 8) == 0) {
216 8a35f56c 2022-07-16 thomas name += 8;
217 6b42af1e 2022-09-02 thomas if (strstr(name, "/" GOT_REF_HEAD) != NULL)
218 8a35f56c 2022-07-16 thomas continue;
219 8a35f56c 2022-07-16 thomas }
220 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&ref_id, t->repo, re->ref);
221 8a35f56c 2022-07-16 thomas if (error)
222 8a35f56c 2022-07-16 thomas return error;
223 8a35f56c 2022-07-16 thomas if (strncmp(name, "tags/", 5) == 0) {
224 8a35f56c 2022-07-16 thomas error = got_object_open_as_tag(&tag, t->repo, ref_id);
225 8a35f56c 2022-07-16 thomas if (error) {
226 8a35f56c 2022-07-16 thomas if (error->code != GOT_ERR_OBJ_TYPE) {
227 8a35f56c 2022-07-16 thomas free(ref_id);
228 8a35f56c 2022-07-16 thomas continue;
229 8a35f56c 2022-07-16 thomas }
230 8a35f56c 2022-07-16 thomas /*
231 8a35f56c 2022-07-16 thomas * Ref points at something other
232 8a35f56c 2022-07-16 thomas * than a tag.
233 8a35f56c 2022-07-16 thomas */
234 8a35f56c 2022-07-16 thomas error = NULL;
235 8a35f56c 2022-07-16 thomas tag = NULL;
236 8a35f56c 2022-07-16 thomas }
237 8a35f56c 2022-07-16 thomas }
238 8a35f56c 2022-07-16 thomas cmp = got_object_id_cmp(tag ?
239 8a35f56c 2022-07-16 thomas got_object_tag_get_object_id(tag) : ref_id, id);
240 8a35f56c 2022-07-16 thomas free(ref_id);
241 8a35f56c 2022-07-16 thomas if (tag)
242 8a35f56c 2022-07-16 thomas got_object_tag_close(tag);
243 8a35f56c 2022-07-16 thomas if (cmp != 0)
244 8a35f56c 2022-07-16 thomas continue;
245 8a35f56c 2022-07-16 thomas s = repo_commit->refs_str;
246 8a35f56c 2022-07-16 thomas if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
247 8a35f56c 2022-07-16 thomas s ? ", " : "", name) == -1) {
248 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
249 8a35f56c 2022-07-16 thomas free(s);
250 8a35f56c 2022-07-16 thomas repo_commit->refs_str = NULL;
251 8a35f56c 2022-07-16 thomas return error;
252 8a35f56c 2022-07-16 thomas }
253 8a35f56c 2022-07-16 thomas free(s);
254 8a35f56c 2022-07-16 thomas }
255 8a35f56c 2022-07-16 thomas
256 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->commit_id, id);
257 8a35f56c 2022-07-16 thomas if (error)
258 8a35f56c 2022-07-16 thomas return error;
259 8a35f56c 2022-07-16 thomas
260 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->tree_id,
261 8a35f56c 2022-07-16 thomas got_object_commit_get_tree_id(commit));
262 8a35f56c 2022-07-16 thomas if (error)
263 8a35f56c 2022-07-16 thomas return error;
264 8a35f56c 2022-07-16 thomas
265 8a35f56c 2022-07-16 thomas if (qs->action == DIFF) {
266 8a35f56c 2022-07-16 thomas parent_id = STAILQ_FIRST(
267 8a35f56c 2022-07-16 thomas got_object_commit_get_parent_ids(commit));
268 8a35f56c 2022-07-16 thomas if (parent_id != NULL) {
269 8a35f56c 2022-07-16 thomas id2 = got_object_id_dup(&parent_id->id);
270 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->parent_id, id2);
271 8a35f56c 2022-07-16 thomas if (error)
272 8a35f56c 2022-07-16 thomas return error;
273 8a35f56c 2022-07-16 thomas free(id2);
274 8a35f56c 2022-07-16 thomas } else {
275 8a35f56c 2022-07-16 thomas repo_commit->parent_id = strdup("/dev/null");
276 8a35f56c 2022-07-16 thomas if (repo_commit->parent_id == NULL) {
277 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
278 8a35f56c 2022-07-16 thomas return error;
279 8a35f56c 2022-07-16 thomas }
280 8a35f56c 2022-07-16 thomas }
281 8a35f56c 2022-07-16 thomas }
282 8a35f56c 2022-07-16 thomas
283 8a35f56c 2022-07-16 thomas repo_commit->committer_time =
284 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
285 8a35f56c 2022-07-16 thomas
286 8a35f56c 2022-07-16 thomas repo_commit->author =
287 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_author(commit));
288 8a35f56c 2022-07-16 thomas if (repo_commit->author == NULL) {
289 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
290 8a35f56c 2022-07-16 thomas return error;
291 8a35f56c 2022-07-16 thomas }
292 8a35f56c 2022-07-16 thomas repo_commit->committer =
293 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_committer(commit));
294 8a35f56c 2022-07-16 thomas if (repo_commit->committer == NULL) {
295 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
296 8a35f56c 2022-07-16 thomas return error;
297 8a35f56c 2022-07-16 thomas }
298 8a35f56c 2022-07-16 thomas error = got_object_commit_get_logmsg(&commit_msg0, commit);
299 8a35f56c 2022-07-16 thomas if (error)
300 8a35f56c 2022-07-16 thomas return error;
301 8a35f56c 2022-07-16 thomas
302 8a35f56c 2022-07-16 thomas commit_msg = commit_msg0;
303 8a35f56c 2022-07-16 thomas while (*commit_msg == '\n')
304 8a35f56c 2022-07-16 thomas commit_msg++;
305 8a35f56c 2022-07-16 thomas
306 8a35f56c 2022-07-16 thomas repo_commit->commit_msg = strdup(commit_msg);
307 8a35f56c 2022-07-16 thomas if (repo_commit->commit_msg == NULL)
308 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
309 8a35f56c 2022-07-16 thomas free(commit_msg0);
310 8a35f56c 2022-07-16 thomas return error;
311 8a35f56c 2022-07-16 thomas }
312 8a35f56c 2022-07-16 thomas
313 8a35f56c 2022-07-16 thomas const struct got_error *
314 8a35f56c 2022-07-16 thomas got_get_repo_commits(struct request *c, int limit)
315 8a35f56c 2022-07-16 thomas {
316 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
317 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
318 8a35f56c 2022-07-16 thomas struct got_commit_graph *graph = NULL;
319 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
320 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
321 4d0573ec 2022-09-02 thomas struct got_reference *ref = NULL;
322 8a35f56c 2022-07-16 thomas struct repo_commit *repo_commit = NULL;
323 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
324 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
325 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
326 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
327 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
328 8a35f56c 2022-07-16 thomas char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
329 42814e01 2023-01-23 thomas int chk_next = 0, chk_multi = 0;
330 8a35f56c 2022-07-16 thomas
331 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
332 8a35f56c 2022-07-16 thomas
333 8a35f56c 2022-07-16 thomas if (qs->file != NULL && strlen(qs->file) > 0)
334 8a35f56c 2022-07-16 thomas if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
335 8a35f56c 2022-07-16 thomas qs->file) == -1)
336 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
337 8a35f56c 2022-07-16 thomas
338 8a35f56c 2022-07-16 thomas if (asprintf(&repo_path, "%s/%s", srv->repos_path,
339 31797419 2022-09-02 thomas repo_dir->name) == -1) {
340 31797419 2022-09-02 thomas error = got_error_from_errno("asprintf");
341 31797419 2022-09-02 thomas goto done;
342 31797419 2022-09-02 thomas }
343 8a35f56c 2022-07-16 thomas
344 42814e01 2023-01-23 thomas if (qs->commit) {
345 42814e01 2023-01-23 thomas error = got_repo_match_object_id_prefix(&id, qs->commit,
346 42814e01 2023-01-23 thomas GOT_OBJ_TYPE_COMMIT, repo);
347 42814e01 2023-01-23 thomas if (error)
348 42814e01 2023-01-23 thomas goto done;
349 42814e01 2023-01-23 thomas } else {
350 8a35f56c 2022-07-16 thomas error = got_ref_open(&ref, repo, qs->headref, 0);
351 8a35f56c 2022-07-16 thomas if (error)
352 8a35f56c 2022-07-16 thomas goto done;
353 8a35f56c 2022-07-16 thomas
354 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, ref);
355 8a35f56c 2022-07-16 thomas if (error)
356 8a35f56c 2022-07-16 thomas goto done;
357 8a35f56c 2022-07-16 thomas }
358 8a35f56c 2022-07-16 thomas
359 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, repo_path);
360 8a35f56c 2022-07-16 thomas if (error)
361 8a35f56c 2022-07-16 thomas goto done;
362 8a35f56c 2022-07-16 thomas
363 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
364 8a35f56c 2022-07-16 thomas if (error)
365 8a35f56c 2022-07-16 thomas goto done;
366 8a35f56c 2022-07-16 thomas
367 8a35f56c 2022-07-16 thomas if (qs->file != NULL && strlen(qs->file) > 0) {
368 8a35f56c 2022-07-16 thomas error = got_commit_graph_open(&graph, file_path, 0);
369 8a35f56c 2022-07-16 thomas if (error)
370 8a35f56c 2022-07-16 thomas goto done;
371 8a35f56c 2022-07-16 thomas } else {
372 8a35f56c 2022-07-16 thomas error = got_commit_graph_open(&graph, in_repo_path, 0);
373 8a35f56c 2022-07-16 thomas if (error)
374 8a35f56c 2022-07-16 thomas goto done;
375 8a35f56c 2022-07-16 thomas }
376 8a35f56c 2022-07-16 thomas
377 8a35f56c 2022-07-16 thomas error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
378 8a35f56c 2022-07-16 thomas if (error)
379 8a35f56c 2022-07-16 thomas goto done;
380 8a35f56c 2022-07-16 thomas
381 8a35f56c 2022-07-16 thomas for (;;) {
382 7210b715 2022-09-11 thomas struct got_object_id next_id;
383 95326260 2022-09-05 thomas
384 95326260 2022-09-05 thomas error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
385 8a35f56c 2022-07-16 thomas NULL);
386 8a35f56c 2022-07-16 thomas if (error) {
387 8a35f56c 2022-07-16 thomas if (error->code == GOT_ERR_ITER_COMPLETED)
388 8a35f56c 2022-07-16 thomas error = NULL;
389 8a35f56c 2022-07-16 thomas goto done;
390 95326260 2022-09-05 thomas }
391 8a35f56c 2022-07-16 thomas
392 7210b715 2022-09-11 thomas error = got_object_open_as_commit(&commit, repo, &next_id);
393 8a35f56c 2022-07-16 thomas if (error)
394 8a35f56c 2022-07-16 thomas goto done;
395 8a35f56c 2022-07-16 thomas
396 df9fed09 2022-09-02 thomas error = got_init_repo_commit(&repo_commit);
397 df9fed09 2022-09-02 thomas if (error)
398 df9fed09 2022-09-02 thomas goto done;
399 df9fed09 2022-09-02 thomas
400 8a35f56c 2022-07-16 thomas error = got_get_repo_commit(c, repo_commit, commit,
401 7210b715 2022-09-11 thomas &refs, &next_id);
402 349f6f39 2022-09-07 thomas if (error) {
403 349f6f39 2022-09-07 thomas gotweb_free_repo_commit(repo_commit);
404 8a35f56c 2022-07-16 thomas goto done;
405 349f6f39 2022-09-07 thomas }
406 349f6f39 2022-09-07 thomas
407 349f6f39 2022-09-07 thomas TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
408 349f6f39 2022-09-07 thomas
409 42814e01 2023-01-23 thomas if (!chk_multi || limit != 1 ||
410 42814e01 2023-01-23 thomas srv->max_commits_display == 1) {
411 8a35f56c 2022-07-16 thomas chk_multi = 1;
412 8a35f56c 2022-07-16 thomas
413 8a35f56c 2022-07-16 thomas /*
414 8a35f56c 2022-07-16 thomas * check for one more commit before breaking,
415 8a35f56c 2022-07-16 thomas * so we know whether to navigate through briefs
416 8a35f56c 2022-07-16 thomas * commits and summary
417 8a35f56c 2022-07-16 thomas */
418 8a35f56c 2022-07-16 thomas if (chk_next && (qs->action == BRIEFS ||
419 8a35f56c 2022-07-16 thomas qs->action == COMMITS || qs->action == SUMMARY)) {
420 20bab626 2023-02-03 thomas t->more_id = strdup(repo_commit->commit_id);
421 20bab626 2023-02-03 thomas if (t->more_id == NULL) {
422 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
423 8a35f56c 2022-07-16 thomas goto done;
424 8a35f56c 2022-07-16 thomas }
425 ddcb4a8e 2023-02-03 thomas got_object_commit_close(commit);
426 ddcb4a8e 2023-02-03 thomas commit = NULL;
427 df9fed09 2022-09-02 thomas TAILQ_REMOVE(&t->repo_commits, repo_commit,
428 8a35f56c 2022-07-16 thomas entry);
429 df9fed09 2022-09-02 thomas gotweb_free_repo_commit(repo_commit);
430 8a35f56c 2022-07-16 thomas goto done;
431 8a35f56c 2022-07-16 thomas }
432 8a35f56c 2022-07-16 thomas }
433 8a35f56c 2022-07-16 thomas if (error || (limit && --limit == 0)) {
434 42814e01 2023-01-23 thomas if (qs->file != NULL && *qs->file != '\0')
435 8a35f56c 2022-07-16 thomas if (chk_multi == 0)
436 8a35f56c 2022-07-16 thomas break;
437 8a35f56c 2022-07-16 thomas chk_next = 1;
438 8a35f56c 2022-07-16 thomas }
439 8a35f56c 2022-07-16 thomas if (commit) {
440 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
441 8a35f56c 2022-07-16 thomas commit = NULL;
442 8a35f56c 2022-07-16 thomas }
443 8a35f56c 2022-07-16 thomas }
444 8a35f56c 2022-07-16 thomas done:
445 4d0573ec 2022-09-02 thomas if (ref)
446 4d0573ec 2022-09-02 thomas got_ref_close(ref);
447 8a35f56c 2022-07-16 thomas if (commit)
448 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
449 8a35f56c 2022-07-16 thomas if (graph)
450 8a35f56c 2022-07-16 thomas got_commit_graph_close(graph);
451 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
452 bc4d9cc8 2022-09-02 thomas free(in_repo_path);
453 8a35f56c 2022-07-16 thomas free(file_path);
454 8a35f56c 2022-07-16 thomas free(repo_path);
455 8a35f56c 2022-07-16 thomas free(id);
456 8a35f56c 2022-07-16 thomas return error;
457 8a35f56c 2022-07-16 thomas }
458 8a35f56c 2022-07-16 thomas
459 8a35f56c 2022-07-16 thomas const struct got_error *
460 8a35f56c 2022-07-16 thomas got_get_repo_tags(struct request *c, int limit)
461 8a35f56c 2022-07-16 thomas {
462 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
463 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
464 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
465 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
466 8a35f56c 2022-07-16 thomas struct got_reference *ref;
467 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
468 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
469 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
470 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
471 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
472 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
473 8a35f56c 2022-07-16 thomas struct got_tag_object *tag = NULL;
474 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
475 8a35f56c 2022-07-16 thomas char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
476 217813df 2022-09-02 thomas char *tag_commit = NULL, *tag_commit0 = NULL;
477 8a35f56c 2022-07-16 thomas char *commit_msg = NULL, *commit_msg0 = NULL;
478 8a35f56c 2022-07-16 thomas int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
479 8a35f56c 2022-07-16 thomas
480 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
481 8a35f56c 2022-07-16 thomas
482 8a35f56c 2022-07-16 thomas if (asprintf(&repo_path, "%s/%s", srv->repos_path,
483 8a35f56c 2022-07-16 thomas repo_dir->name) == -1)
484 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
485 8a35f56c 2022-07-16 thomas
486 d6795e9f 2022-12-30 thomas if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
487 8a35f56c 2022-07-16 thomas error = got_ref_open(&ref, repo, qs->headref, 0);
488 8a35f56c 2022-07-16 thomas if (error)
489 8a35f56c 2022-07-16 thomas goto err;
490 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, ref);
491 8a35f56c 2022-07-16 thomas got_ref_close(ref);
492 8a35f56c 2022-07-16 thomas if (error)
493 8a35f56c 2022-07-16 thomas goto err;
494 8a35f56c 2022-07-16 thomas } else if (qs->commit == NULL && qs->action == TAG) {
495 8a35f56c 2022-07-16 thomas error = got_error_msg(GOT_ERR_EOF, "commit id missing");
496 8a35f56c 2022-07-16 thomas goto err;
497 8a35f56c 2022-07-16 thomas } else {
498 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id_prefix(&id, qs->commit,
499 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, repo);
500 8a35f56c 2022-07-16 thomas if (error)
501 8a35f56c 2022-07-16 thomas goto err;
502 8a35f56c 2022-07-16 thomas }
503 8a35f56c 2022-07-16 thomas
504 8a35f56c 2022-07-16 thomas if (qs->action != SUMMARY && qs->action != TAGS) {
505 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
506 8a35f56c 2022-07-16 thomas if (error)
507 8a35f56c 2022-07-16 thomas goto err;
508 8a35f56c 2022-07-16 thomas error = got_object_commit_get_logmsg(&commit_msg0, commit);
509 8a35f56c 2022-07-16 thomas if (error)
510 8a35f56c 2022-07-16 thomas goto err;
511 8a35f56c 2022-07-16 thomas if (commit) {
512 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
513 8a35f56c 2022-07-16 thomas commit = NULL;
514 8a35f56c 2022-07-16 thomas }
515 8a35f56c 2022-07-16 thomas }
516 8a35f56c 2022-07-16 thomas
517 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, repo_path);
518 8a35f56c 2022-07-16 thomas if (error)
519 8a35f56c 2022-07-16 thomas goto err;
520 8a35f56c 2022-07-16 thomas
521 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
522 8a35f56c 2022-07-16 thomas repo);
523 8a35f56c 2022-07-16 thomas if (error)
524 8a35f56c 2022-07-16 thomas goto err;
525 8a35f56c 2022-07-16 thomas
526 8a35f56c 2022-07-16 thomas if (limit == 1)
527 8a35f56c 2022-07-16 thomas chk_multi = 0;
528 8a35f56c 2022-07-16 thomas
529 8a35f56c 2022-07-16 thomas /*
530 8a35f56c 2022-07-16 thomas * XXX: again, see previous message about caching
531 8a35f56c 2022-07-16 thomas */
532 8a35f56c 2022-07-16 thomas
533 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
534 8a35f56c 2022-07-16 thomas struct repo_tag *new_repo_tag = NULL;
535 8a35f56c 2022-07-16 thomas error = got_init_repo_tag(&new_repo_tag);
536 8a35f56c 2022-07-16 thomas if (error)
537 8a35f56c 2022-07-16 thomas goto err;
538 8a35f56c 2022-07-16 thomas
539 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
540 8a35f56c 2022-07-16 thomas
541 8a35f56c 2022-07-16 thomas new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
542 8a35f56c 2022-07-16 thomas if (new_repo_tag->tag_name == NULL) {
543 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
544 8a35f56c 2022-07-16 thomas goto err;
545 8a35f56c 2022-07-16 thomas }
546 8a35f56c 2022-07-16 thomas
547 19d60ee3 2022-09-02 thomas free(id);
548 19d60ee3 2022-09-02 thomas id = NULL;
549 19d60ee3 2022-09-02 thomas
550 19d60ee3 2022-09-02 thomas free(id_str);
551 19d60ee3 2022-09-02 thomas id_str = NULL;
552 19d60ee3 2022-09-02 thomas
553 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, re->ref);
554 8a35f56c 2022-07-16 thomas if (error)
555 8a35f56c 2022-07-16 thomas goto done;
556 8a35f56c 2022-07-16 thomas
557 acd4ff87 2022-09-02 thomas if (tag)
558 acd4ff87 2022-09-02 thomas got_object_tag_close(tag);
559 8a35f56c 2022-07-16 thomas error = got_object_open_as_tag(&tag, repo, id);
560 8a35f56c 2022-07-16 thomas if (error) {
561 19d60ee3 2022-09-02 thomas if (error->code != GOT_ERR_OBJ_TYPE)
562 8a35f56c 2022-07-16 thomas goto done;
563 8a35f56c 2022-07-16 thomas /* "lightweight" tag */
564 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
565 19d60ee3 2022-09-02 thomas if (error)
566 8a35f56c 2022-07-16 thomas goto done;
567 8a35f56c 2022-07-16 thomas new_repo_tag->tagger =
568 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_committer(commit));
569 8a35f56c 2022-07-16 thomas if (new_repo_tag->tagger == NULL) {
570 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
571 8a35f56c 2022-07-16 thomas goto err;
572 8a35f56c 2022-07-16 thomas }
573 8a35f56c 2022-07-16 thomas new_repo_tag->tagger_time =
574 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
575 8a35f56c 2022-07-16 thomas error = got_object_id_str(&id_str, id);
576 8a35f56c 2022-07-16 thomas if (error)
577 8a35f56c 2022-07-16 thomas goto err;
578 8a35f56c 2022-07-16 thomas } else {
579 8a35f56c 2022-07-16 thomas new_repo_tag->tagger =
580 8a35f56c 2022-07-16 thomas strdup(got_object_tag_get_tagger(tag));
581 8a35f56c 2022-07-16 thomas if (new_repo_tag->tagger == NULL) {
582 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
583 8a35f56c 2022-07-16 thomas goto err;
584 8a35f56c 2022-07-16 thomas }
585 8a35f56c 2022-07-16 thomas new_repo_tag->tagger_time =
586 8a35f56c 2022-07-16 thomas got_object_tag_get_tagger_time(tag);
587 8a35f56c 2022-07-16 thomas error = got_object_id_str(&id_str,
588 8a35f56c 2022-07-16 thomas got_object_tag_get_object_id(tag));
589 8a35f56c 2022-07-16 thomas if (error)
590 8a35f56c 2022-07-16 thomas goto err;
591 8a35f56c 2022-07-16 thomas }
592 8a35f56c 2022-07-16 thomas
593 8a35f56c 2022-07-16 thomas new_repo_tag->commit_id = strdup(id_str);
594 8a35f56c 2022-07-16 thomas if (new_repo_tag->commit_id == NULL)
595 8a35f56c 2022-07-16 thomas goto err;
596 8a35f56c 2022-07-16 thomas
597 8a35f56c 2022-07-16 thomas if (commit_found == 0 && qs->commit != NULL &&
598 8a35f56c 2022-07-16 thomas strncmp(id_str, qs->commit, strlen(id_str)) != 0)
599 8a35f56c 2022-07-16 thomas continue;
600 8a35f56c 2022-07-16 thomas else
601 8a35f56c 2022-07-16 thomas commit_found = 1;
602 8a35f56c 2022-07-16 thomas
603 8a35f56c 2022-07-16 thomas t->tag_count++;
604 8a35f56c 2022-07-16 thomas
605 8a35f56c 2022-07-16 thomas /*
606 8a35f56c 2022-07-16 thomas * check for one more commit before breaking,
607 8a35f56c 2022-07-16 thomas * so we know whether to navigate through briefs
608 8a35f56c 2022-07-16 thomas * commits and summary
609 8a35f56c 2022-07-16 thomas */
610 8a35f56c 2022-07-16 thomas if (chk_next) {
611 8a35f56c 2022-07-16 thomas t->next_id = strdup(new_repo_tag->commit_id);
612 8a35f56c 2022-07-16 thomas if (t->next_id == NULL) {
613 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
614 8a35f56c 2022-07-16 thomas goto err;
615 8a35f56c 2022-07-16 thomas }
616 8a35f56c 2022-07-16 thomas if (commit) {
617 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
618 8a35f56c 2022-07-16 thomas commit = NULL;
619 8a35f56c 2022-07-16 thomas }
620 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
621 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(new_repo_tag);
622 8a35f56c 2022-07-16 thomas goto done;
623 8a35f56c 2022-07-16 thomas }
624 8a35f56c 2022-07-16 thomas
625 8a35f56c 2022-07-16 thomas if (commit) {
626 217813df 2022-09-02 thomas error = got_object_commit_get_logmsg(&tag_commit0,
627 217813df 2022-09-02 thomas commit);
628 8a35f56c 2022-07-16 thomas if (error)
629 217813df 2022-09-02 thomas goto err;
630 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
631 8a35f56c 2022-07-16 thomas commit = NULL;
632 8a35f56c 2022-07-16 thomas } else {
633 217813df 2022-09-02 thomas tag_commit0 = strdup(got_object_tag_get_message(tag));
634 217813df 2022-09-02 thomas if (tag_commit0 == NULL) {
635 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
636 217813df 2022-09-02 thomas goto err;
637 8a35f56c 2022-07-16 thomas }
638 8a35f56c 2022-07-16 thomas }
639 8a35f56c 2022-07-16 thomas
640 217813df 2022-09-02 thomas tag_commit = tag_commit0;
641 217813df 2022-09-02 thomas while (*tag_commit == '\n')
642 217813df 2022-09-02 thomas tag_commit++;
643 217813df 2022-09-02 thomas new_repo_tag->tag_commit = strdup(tag_commit);
644 217813df 2022-09-02 thomas if (new_repo_tag->tag_commit == NULL) {
645 217813df 2022-09-02 thomas error = got_error_from_errno("strdup");
646 217813df 2022-09-02 thomas free(tag_commit0);
647 217813df 2022-09-02 thomas goto err;
648 217813df 2022-09-02 thomas }
649 217813df 2022-09-02 thomas free(tag_commit0);
650 8a35f56c 2022-07-16 thomas
651 8a35f56c 2022-07-16 thomas if (qs->action != SUMMARY && qs->action != TAGS) {
652 8a35f56c 2022-07-16 thomas commit_msg = commit_msg0;
653 8a35f56c 2022-07-16 thomas while (*commit_msg == '\n')
654 8a35f56c 2022-07-16 thomas commit_msg++;
655 8a35f56c 2022-07-16 thomas
656 8a35f56c 2022-07-16 thomas new_repo_tag->commit_msg = strdup(commit_msg);
657 8a35f56c 2022-07-16 thomas if (new_repo_tag->commit_msg == NULL) {
658 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
659 8a35f56c 2022-07-16 thomas goto err;
660 8a35f56c 2022-07-16 thomas }
661 8a35f56c 2022-07-16 thomas }
662 8a35f56c 2022-07-16 thomas
663 8a35f56c 2022-07-16 thomas if (limit && --limit == 0) {
664 8a35f56c 2022-07-16 thomas if (chk_multi == 0)
665 8a35f56c 2022-07-16 thomas break;
666 8a35f56c 2022-07-16 thomas chk_next = 1;
667 8a35f56c 2022-07-16 thomas }
668 8a35f56c 2022-07-16 thomas }
669 8a35f56c 2022-07-16 thomas
670 8a35f56c 2022-07-16 thomas done:
671 8a35f56c 2022-07-16 thomas /*
672 8a35f56c 2022-07-16 thomas * we have tailq populated, so find previous commit id
673 8a35f56c 2022-07-16 thomas * for navigation through briefs and commits
674 8a35f56c 2022-07-16 thomas */
675 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
676 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
677 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
678 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
679 8a35f56c 2022-07-16 thomas }
680 8a35f56c 2022-07-16 thomas }
681 8a35f56c 2022-07-16 thomas if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
682 8a35f56c 2022-07-16 thomas commit_found = 0;
683 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
684 8a35f56c 2022-07-16 thomas entry) {
685 8a35f56c 2022-07-16 thomas if (commit_found == 0 && rt->commit_id != NULL &&
686 8a35f56c 2022-07-16 thomas strcmp(qs->commit, rt->commit_id) != 0) {
687 8a35f56c 2022-07-16 thomas continue;
688 8a35f56c 2022-07-16 thomas } else
689 8a35f56c 2022-07-16 thomas commit_found = 1;
690 8a35f56c 2022-07-16 thomas if (c_cnt == srv->max_commits_display ||
691 8a35f56c 2022-07-16 thomas rt == TAILQ_FIRST(&t->repo_tags)) {
692 8a35f56c 2022-07-16 thomas t->prev_id = strdup(rt->commit_id);
693 8a35f56c 2022-07-16 thomas if (t->prev_id == NULL)
694 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
695 8a35f56c 2022-07-16 thomas break;
696 8a35f56c 2022-07-16 thomas }
697 8a35f56c 2022-07-16 thomas c_cnt++;
698 8a35f56c 2022-07-16 thomas }
699 8a35f56c 2022-07-16 thomas }
700 8a35f56c 2022-07-16 thomas err:
701 8a35f56c 2022-07-16 thomas if (commit)
702 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
703 19d60ee3 2022-09-02 thomas if (tag)
704 19d60ee3 2022-09-02 thomas got_object_tag_close(tag);
705 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
706 ddcf1d33 2022-09-02 thomas free(commit_msg0);
707 19d60ee3 2022-09-02 thomas free(in_repo_path);
708 8a35f56c 2022-07-16 thomas free(repo_path);
709 8a35f56c 2022-07-16 thomas free(id);
710 19d60ee3 2022-09-02 thomas free(id_str);
711 8a35f56c 2022-07-16 thomas return error;
712 8a35f56c 2022-07-16 thomas }
713 8a35f56c 2022-07-16 thomas
714 3c14c1f2 2023-01-06 thomas int
715 3c14c1f2 2023-01-06 thomas got_output_repo_tree(struct request *c,
716 3c14c1f2 2023-01-06 thomas int (*cb)(struct template *, struct got_tree_entry *))
717 8a35f56c 2022-07-16 thomas {
718 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
719 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
720 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
721 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
722 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
723 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
724 8a35f56c 2022-07-16 thomas struct got_object_id *tree_id = NULL, *commit_id = NULL;
725 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
726 8a35f56c 2022-07-16 thomas struct got_tree_object *tree = NULL;
727 3c14c1f2 2023-01-06 thomas struct got_tree_entry *te;
728 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
729 06325330 2022-09-02 thomas char *escaped_name = NULL, *path = NULL;
730 3c14c1f2 2023-01-06 thomas int nentries, i;
731 8a35f56c 2022-07-16 thomas
732 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
733 8a35f56c 2022-07-16 thomas
734 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
735 8a35f56c 2022-07-16 thomas
736 8a35f56c 2022-07-16 thomas if (qs->folder != NULL) {
737 8a35f56c 2022-07-16 thomas path = strdup(qs->folder);
738 8a35f56c 2022-07-16 thomas if (path == NULL) {
739 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
740 8a35f56c 2022-07-16 thomas goto done;
741 8a35f56c 2022-07-16 thomas }
742 8a35f56c 2022-07-16 thomas } else {
743 557d32ee 2022-09-02 thomas error = got_repo_map_path(&path, repo, repo_dir->path);
744 8a35f56c 2022-07-16 thomas if (error)
745 8a35f56c 2022-07-16 thomas goto done;
746 8a35f56c 2022-07-16 thomas }
747 8a35f56c 2022-07-16 thomas
748 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
749 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
750 8a35f56c 2022-07-16 thomas if (error)
751 8a35f56c 2022-07-16 thomas goto done;
752 8a35f56c 2022-07-16 thomas
753 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
754 8a35f56c 2022-07-16 thomas if (error)
755 8a35f56c 2022-07-16 thomas goto done;
756 8a35f56c 2022-07-16 thomas
757 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&tree_id, repo, commit, path);
758 8a35f56c 2022-07-16 thomas if (error)
759 8a35f56c 2022-07-16 thomas goto done;
760 8a35f56c 2022-07-16 thomas
761 8a35f56c 2022-07-16 thomas error = got_object_open_as_tree(&tree, repo, tree_id);
762 8a35f56c 2022-07-16 thomas if (error)
763 8a35f56c 2022-07-16 thomas goto done;
764 8a35f56c 2022-07-16 thomas
765 8a35f56c 2022-07-16 thomas nentries = got_object_tree_get_nentries(tree);
766 8a35f56c 2022-07-16 thomas
767 8a35f56c 2022-07-16 thomas for (i = 0; i < nentries; i++) {
768 8a35f56c 2022-07-16 thomas te = got_object_tree_get_entry(tree, i);
769 8f464e92 2023-02-17 thomas if (cb(c->tp, te) == -1) {
770 8f464e92 2023-02-17 thomas error = got_error(GOT_ERR_CANCELLED);
771 3c14c1f2 2023-01-06 thomas break;
772 8f464e92 2023-02-17 thomas }
773 8a35f56c 2022-07-16 thomas }
774 8a35f56c 2022-07-16 thomas done:
775 df610f47 2022-08-27 thomas free(escaped_name);
776 8a35f56c 2022-07-16 thomas free(path);
777 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
778 8a35f56c 2022-07-16 thomas if (commit)
779 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
780 12e242cc 2022-09-06 thomas if (tree)
781 12e242cc 2022-09-06 thomas got_object_tree_close(tree);
782 8a35f56c 2022-07-16 thomas free(commit_id);
783 8a35f56c 2022-07-16 thomas free(tree_id);
784 3c14c1f2 2023-01-06 thomas if (error) {
785 3c14c1f2 2023-01-06 thomas log_warnx("%s: %s", __func__, error->msg);
786 3c14c1f2 2023-01-06 thomas return -1;
787 3c14c1f2 2023-01-06 thomas }
788 3c14c1f2 2023-01-06 thomas return 0;
789 8a35f56c 2022-07-16 thomas }
790 8a35f56c 2022-07-16 thomas
791 8a35f56c 2022-07-16 thomas const struct got_error *
792 b82440e1 2023-01-06 thomas got_open_blob_for_output(struct got_blob_object **blob, int *fd,
793 b82440e1 2023-01-06 thomas int *binary, struct request *c)
794 8a35f56c 2022-07-16 thomas {
795 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
796 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
797 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
798 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
799 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
800 8a35f56c 2022-07-16 thomas struct got_object_id *commit_id = NULL;
801 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
802 8a35f56c 2022-07-16 thomas char *path = NULL, *in_repo_path = NULL;
803 b82440e1 2023-01-06 thomas int obj_type;
804 8a35f56c 2022-07-16 thomas
805 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
806 8a35f56c 2022-07-16 thomas
807 b82440e1 2023-01-06 thomas *blob = NULL;
808 b82440e1 2023-01-06 thomas *fd = -1;
809 b82440e1 2023-01-06 thomas *binary = 0;
810 b82440e1 2023-01-06 thomas
811 8a35f56c 2022-07-16 thomas if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
812 8a35f56c 2022-07-16 thomas qs->folder ? "/" : "", qs->file) == -1) {
813 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
814 8a35f56c 2022-07-16 thomas goto done;
815 8a35f56c 2022-07-16 thomas }
816 8a35f56c 2022-07-16 thomas
817 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, path);
818 8a35f56c 2022-07-16 thomas if (error)
819 8a35f56c 2022-07-16 thomas goto done;
820 8a35f56c 2022-07-16 thomas
821 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
822 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
823 8a35f56c 2022-07-16 thomas if (error)
824 8a35f56c 2022-07-16 thomas goto done;
825 8a35f56c 2022-07-16 thomas
826 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
827 8a35f56c 2022-07-16 thomas if (error)
828 8a35f56c 2022-07-16 thomas goto done;
829 8a35f56c 2022-07-16 thomas
830 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
831 8a35f56c 2022-07-16 thomas if (error)
832 8a35f56c 2022-07-16 thomas goto done;
833 8a35f56c 2022-07-16 thomas
834 8a35f56c 2022-07-16 thomas if (commit_id == NULL) {
835 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_NO_OBJ);
836 8a35f56c 2022-07-16 thomas goto done;
837 8a35f56c 2022-07-16 thomas }
838 8a35f56c 2022-07-16 thomas
839 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, commit_id);
840 8a35f56c 2022-07-16 thomas if (error)
841 8a35f56c 2022-07-16 thomas goto done;
842 8a35f56c 2022-07-16 thomas
843 8a35f56c 2022-07-16 thomas if (obj_type != GOT_OBJ_TYPE_BLOB) {
844 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
845 8a35f56c 2022-07-16 thomas goto done;
846 8a35f56c 2022-07-16 thomas }
847 8a35f56c 2022-07-16 thomas
848 b82440e1 2023-01-06 thomas error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
849 8a35f56c 2022-07-16 thomas if (error)
850 8a35f56c 2022-07-16 thomas goto done;
851 8a35f56c 2022-07-16 thomas
852 b82440e1 2023-01-06 thomas error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
853 8a35f56c 2022-07-16 thomas if (error)
854 8a35f56c 2022-07-16 thomas goto done;
855 efe3fd03 2023-01-06 thomas
856 b82440e1 2023-01-06 thomas error = got_object_blob_is_binary(binary, *blob);
857 efe3fd03 2023-01-06 thomas if (error)
858 efe3fd03 2023-01-06 thomas goto done;
859 efe3fd03 2023-01-06 thomas
860 b82440e1 2023-01-06 thomas done:
861 b82440e1 2023-01-06 thomas if (commit)
862 b82440e1 2023-01-06 thomas got_object_commit_close(commit);
863 b82440e1 2023-01-06 thomas
864 b82440e1 2023-01-06 thomas if (error) {
865 b82440e1 2023-01-06 thomas if (*fd != -1)
866 b82440e1 2023-01-06 thomas close(*fd);
867 b82440e1 2023-01-06 thomas if (*blob)
868 b82440e1 2023-01-06 thomas got_object_blob_close(*blob);
869 b82440e1 2023-01-06 thomas *fd = -1;
870 b82440e1 2023-01-06 thomas *blob = NULL;
871 b82440e1 2023-01-06 thomas }
872 b82440e1 2023-01-06 thomas
873 b82440e1 2023-01-06 thomas free(in_repo_path);
874 b82440e1 2023-01-06 thomas free(commit_id);
875 b82440e1 2023-01-06 thomas free(path);
876 8a35f56c 2022-07-16 thomas return error;
877 b82440e1 2023-01-06 thomas }
878 b82440e1 2023-01-06 thomas
879 b82440e1 2023-01-06 thomas int
880 b82440e1 2023-01-06 thomas got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
881 b82440e1 2023-01-06 thomas int (*cb)(struct template *, const char *, size_t))
882 b82440e1 2023-01-06 thomas {
883 b82440e1 2023-01-06 thomas const struct got_error *err;
884 b82440e1 2023-01-06 thomas char *line = NULL;
885 b82440e1 2023-01-06 thomas size_t linesize = 0;
886 b82440e1 2023-01-06 thomas size_t lineno = 0;
887 b82440e1 2023-01-06 thomas ssize_t linelen = 0;
888 b82440e1 2023-01-06 thomas
889 b82440e1 2023-01-06 thomas for (;;) {
890 b82440e1 2023-01-06 thomas err = got_object_blob_getline(&line, &linelen, &linesize,
891 b82440e1 2023-01-06 thomas blob);
892 b82440e1 2023-01-06 thomas if (err || linelen == -1)
893 b82440e1 2023-01-06 thomas break;
894 b82440e1 2023-01-06 thomas lineno++;
895 8f464e92 2023-02-17 thomas if (cb(tp, line, lineno) == -1) {
896 8f464e92 2023-02-17 thomas err = got_error(GOT_ERR_CANCELLED);
897 b82440e1 2023-01-06 thomas break;
898 8f464e92 2023-02-17 thomas }
899 b82440e1 2023-01-06 thomas }
900 b82440e1 2023-01-06 thomas
901 b82440e1 2023-01-06 thomas free(line);
902 b82440e1 2023-01-06 thomas
903 b82440e1 2023-01-06 thomas if (err) {
904 b82440e1 2023-01-06 thomas log_warnx("%s: got_object_blob_getline failed: %s",
905 b82440e1 2023-01-06 thomas __func__, err->msg);
906 b82440e1 2023-01-06 thomas return -1;
907 b82440e1 2023-01-06 thomas }
908 b82440e1 2023-01-06 thomas return 0;
909 8a35f56c 2022-07-16 thomas }
910 8a35f56c 2022-07-16 thomas
911 8a35f56c 2022-07-16 thomas struct blame_cb_args {
912 8a35f56c 2022-07-16 thomas struct blame_line *lines;
913 8a35f56c 2022-07-16 thomas int nlines;
914 8a35f56c 2022-07-16 thomas int nlines_prec;
915 8a35f56c 2022-07-16 thomas int lineno_cur;
916 8a35f56c 2022-07-16 thomas off_t *line_offsets;
917 8a35f56c 2022-07-16 thomas FILE *f;
918 8a35f56c 2022-07-16 thomas struct got_repository *repo;
919 8a35f56c 2022-07-16 thomas struct request *c;
920 1cd5d437 2023-01-15 thomas got_render_blame_line_cb cb;
921 8a35f56c 2022-07-16 thomas };
922 8a35f56c 2022-07-16 thomas
923 8a35f56c 2022-07-16 thomas static const struct got_error *
924 8a35f56c 2022-07-16 thomas got_gotweb_blame_cb(void *arg, int nlines, int lineno,
925 8a35f56c 2022-07-16 thomas struct got_commit_object *commit, struct got_object_id *id)
926 8a35f56c 2022-07-16 thomas {
927 8a35f56c 2022-07-16 thomas const struct got_error *err = NULL;
928 8a35f56c 2022-07-16 thomas struct blame_cb_args *a = arg;
929 8a35f56c 2022-07-16 thomas struct blame_line *bline;
930 8a35f56c 2022-07-16 thomas struct request *c = a->c;
931 1cd5d437 2023-01-15 thomas char *line = NULL;
932 8a35f56c 2022-07-16 thomas size_t linesize = 0;
933 8a35f56c 2022-07-16 thomas off_t offset;
934 8a35f56c 2022-07-16 thomas struct tm tm;
935 8a35f56c 2022-07-16 thomas time_t committer_time;
936 8a35f56c 2022-07-16 thomas
937 8a35f56c 2022-07-16 thomas if (nlines != a->nlines ||
938 8a35f56c 2022-07-16 thomas (lineno != -1 && lineno < 1) || lineno > a->nlines)
939 8a35f56c 2022-07-16 thomas return got_error(GOT_ERR_RANGE);
940 8a35f56c 2022-07-16 thomas
941 8a35f56c 2022-07-16 thomas if (lineno == -1)
942 8a35f56c 2022-07-16 thomas return NULL; /* no change in this commit */
943 8a35f56c 2022-07-16 thomas
944 8a35f56c 2022-07-16 thomas /* Annotate this line. */
945 8a35f56c 2022-07-16 thomas bline = &a->lines[lineno - 1];
946 8a35f56c 2022-07-16 thomas if (bline->annotated)
947 8a35f56c 2022-07-16 thomas return NULL;
948 8a35f56c 2022-07-16 thomas err = got_object_id_str(&bline->id_str, id);
949 8a35f56c 2022-07-16 thomas if (err)
950 8a35f56c 2022-07-16 thomas return err;
951 8a35f56c 2022-07-16 thomas
952 8a35f56c 2022-07-16 thomas bline->committer = strdup(got_object_commit_get_committer(commit));
953 8a35f56c 2022-07-16 thomas if (bline->committer == NULL) {
954 8a35f56c 2022-07-16 thomas err = got_error_from_errno("strdup");
955 8a35f56c 2022-07-16 thomas goto done;
956 8a35f56c 2022-07-16 thomas }
957 8a35f56c 2022-07-16 thomas
958 8a35f56c 2022-07-16 thomas committer_time = got_object_commit_get_committer_time(commit);
959 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
960 8a35f56c 2022-07-16 thomas return got_error_from_errno("gmtime_r");
961 8a35f56c 2022-07-16 thomas if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
962 8a35f56c 2022-07-16 thomas &tm) == 0) {
963 8a35f56c 2022-07-16 thomas err = got_error(GOT_ERR_NO_SPACE);
964 8a35f56c 2022-07-16 thomas goto done;
965 8a35f56c 2022-07-16 thomas }
966 8a35f56c 2022-07-16 thomas bline->annotated = 1;
967 8a35f56c 2022-07-16 thomas
968 8a35f56c 2022-07-16 thomas /* Print lines annotated so far. */
969 8a35f56c 2022-07-16 thomas bline = &a->lines[a->lineno_cur - 1];
970 8a35f56c 2022-07-16 thomas if (!bline->annotated)
971 8a35f56c 2022-07-16 thomas goto done;
972 8a35f56c 2022-07-16 thomas
973 8a35f56c 2022-07-16 thomas offset = a->line_offsets[a->lineno_cur - 1];
974 8a35f56c 2022-07-16 thomas if (fseeko(a->f, offset, SEEK_SET) == -1) {
975 8a35f56c 2022-07-16 thomas err = got_error_from_errno("fseeko");
976 8a35f56c 2022-07-16 thomas goto done;
977 8a35f56c 2022-07-16 thomas }
978 8a35f56c 2022-07-16 thomas
979 b5c07627 2022-08-18 thomas while (a->lineno_cur <= a->nlines && bline->annotated) {
980 8a35f56c 2022-07-16 thomas if (getline(&line, &linesize, a->f) == -1) {
981 8a35f56c 2022-07-16 thomas if (ferror(a->f))
982 8a35f56c 2022-07-16 thomas err = got_error_from_errno("getline");
983 8a35f56c 2022-07-16 thomas break;
984 8a35f56c 2022-07-16 thomas }
985 8a35f56c 2022-07-16 thomas
986 1cd5d437 2023-01-15 thomas if (a->cb(c->tp, line, bline, a->nlines_prec,
987 ad5cb26d 2023-02-17 thomas a->lineno_cur) == -1) {
988 ad5cb26d 2023-02-17 thomas err = got_error(GOT_ERR_CANCELLED);
989 1cd5d437 2023-01-15 thomas break;
990 ad5cb26d 2023-02-17 thomas }
991 79393471 2022-08-27 thomas
992 8a35f56c 2022-07-16 thomas a->lineno_cur++;
993 8a35f56c 2022-07-16 thomas bline = &a->lines[a->lineno_cur - 1];
994 8a35f56c 2022-07-16 thomas }
995 8a35f56c 2022-07-16 thomas done:
996 8a35f56c 2022-07-16 thomas free(line);
997 8a35f56c 2022-07-16 thomas return err;
998 8a35f56c 2022-07-16 thomas }
999 8a35f56c 2022-07-16 thomas
1000 8a35f56c 2022-07-16 thomas const struct got_error *
1001 1cd5d437 2023-01-15 thomas got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1002 8a35f56c 2022-07-16 thomas {
1003 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1004 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1005 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1006 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
1007 8a35f56c 2022-07-16 thomas struct got_object_id *obj_id = NULL, *commit_id = NULL;
1008 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
1009 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1010 8a35f56c 2022-07-16 thomas struct got_blob_object *blob = NULL;
1011 8a35f56c 2022-07-16 thomas char *path = NULL, *in_repo_path = NULL;
1012 8a35f56c 2022-07-16 thomas struct blame_cb_args bca;
1013 db26fc4c 2023-05-16 thomas int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
1014 8a35f56c 2022-07-16 thomas off_t filesize;
1015 8a35f56c 2022-07-16 thomas FILE *f1 = NULL, *f2 = NULL;
1016 8a35f56c 2022-07-16 thomas
1017 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1018 8a35f56c 2022-07-16 thomas bca.f = NULL;
1019 8a35f56c 2022-07-16 thomas bca.lines = NULL;
1020 1cd5d437 2023-01-15 thomas bca.cb = cb;
1021 8a35f56c 2022-07-16 thomas
1022 8a35f56c 2022-07-16 thomas if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1023 8a35f56c 2022-07-16 thomas qs->folder ? "/" : "", qs->file) == -1) {
1024 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1025 8a35f56c 2022-07-16 thomas goto done;
1026 8a35f56c 2022-07-16 thomas }
1027 8a35f56c 2022-07-16 thomas
1028 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, path);
1029 8a35f56c 2022-07-16 thomas if (error)
1030 8a35f56c 2022-07-16 thomas goto done;
1031 8a35f56c 2022-07-16 thomas
1032 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1033 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
1034 8a35f56c 2022-07-16 thomas if (error)
1035 8a35f56c 2022-07-16 thomas goto done;
1036 8a35f56c 2022-07-16 thomas
1037 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
1038 8a35f56c 2022-07-16 thomas if (error)
1039 8a35f56c 2022-07-16 thomas goto done;
1040 8a35f56c 2022-07-16 thomas
1041 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1042 8a35f56c 2022-07-16 thomas if (error)
1043 8a35f56c 2022-07-16 thomas goto done;
1044 8a35f56c 2022-07-16 thomas
1045 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, obj_id);
1046 8a35f56c 2022-07-16 thomas if (error)
1047 8a35f56c 2022-07-16 thomas goto done;
1048 8a35f56c 2022-07-16 thomas
1049 8a35f56c 2022-07-16 thomas if (obj_type != GOT_OBJ_TYPE_BLOB) {
1050 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
1051 8a35f56c 2022-07-16 thomas goto done;
1052 8a35f56c 2022-07-16 thomas }
1053 8a35f56c 2022-07-16 thomas
1054 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1055 8a35f56c 2022-07-16 thomas if (error)
1056 8a35f56c 2022-07-16 thomas goto done;
1057 8a35f56c 2022-07-16 thomas
1058 db26fc4c 2023-05-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1059 8a35f56c 2022-07-16 thomas if (error)
1060 8a35f56c 2022-07-16 thomas goto done;
1061 8a35f56c 2022-07-16 thomas
1062 db26fc4c 2023-05-16 thomas error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1063 8a35f56c 2022-07-16 thomas if (error)
1064 8a35f56c 2022-07-16 thomas goto done;
1065 8a35f56c 2022-07-16 thomas
1066 8a35f56c 2022-07-16 thomas error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1067 8a35f56c 2022-07-16 thomas &bca.line_offsets, bca.f, blob);
1068 8a35f56c 2022-07-16 thomas if (error || bca.nlines == 0)
1069 8a35f56c 2022-07-16 thomas goto done;
1070 8a35f56c 2022-07-16 thomas
1071 8a35f56c 2022-07-16 thomas /* Don't include \n at EOF in the blame line count. */
1072 8a35f56c 2022-07-16 thomas if (bca.line_offsets[bca.nlines - 1] == filesize)
1073 8a35f56c 2022-07-16 thomas bca.nlines--;
1074 8a35f56c 2022-07-16 thomas
1075 8a35f56c 2022-07-16 thomas bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1076 8a35f56c 2022-07-16 thomas if (bca.lines == NULL) {
1077 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1078 8a35f56c 2022-07-16 thomas goto done;
1079 8a35f56c 2022-07-16 thomas }
1080 8a35f56c 2022-07-16 thomas bca.lineno_cur = 1;
1081 8a35f56c 2022-07-16 thomas bca.nlines_prec = 0;
1082 8a35f56c 2022-07-16 thomas i = bca.nlines;
1083 8a35f56c 2022-07-16 thomas while (i > 0) {
1084 8a35f56c 2022-07-16 thomas i /= 10;
1085 8a35f56c 2022-07-16 thomas bca.nlines_prec++;
1086 8a35f56c 2022-07-16 thomas }
1087 8a35f56c 2022-07-16 thomas bca.repo = repo;
1088 8a35f56c 2022-07-16 thomas bca.c = c;
1089 8a35f56c 2022-07-16 thomas
1090 db26fc4c 2023-05-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1091 8a35f56c 2022-07-16 thomas if (error)
1092 8a35f56c 2022-07-16 thomas goto done;
1093 8a35f56c 2022-07-16 thomas
1094 db26fc4c 2023-05-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1095 8a35f56c 2022-07-16 thomas if (error)
1096 8a35f56c 2022-07-16 thomas goto done;
1097 8a35f56c 2022-07-16 thomas
1098 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1099 8a35f56c 2022-07-16 thomas if (error)
1100 8a35f56c 2022-07-16 thomas goto done;
1101 8a35f56c 2022-07-16 thomas
1102 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1103 8a35f56c 2022-07-16 thomas if (error)
1104 8a35f56c 2022-07-16 thomas goto done;
1105 8a35f56c 2022-07-16 thomas
1106 8a35f56c 2022-07-16 thomas error = got_blame(in_repo_path, commit_id, repo,
1107 8a35f56c 2022-07-16 thomas GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1108 db26fc4c 2023-05-16 thomas fd1, fd2, f1, f2);
1109 8a35f56c 2022-07-16 thomas
1110 d38635a3 2022-09-03 thomas done:
1111 d38635a3 2022-09-03 thomas if (bca.lines) {
1112 8a35f56c 2022-07-16 thomas free(bca.line_offsets);
1113 8a35f56c 2022-07-16 thomas for (i = 0; i < bca.nlines; i++) {
1114 8a35f56c 2022-07-16 thomas struct blame_line *bline = &bca.lines[i];
1115 8a35f56c 2022-07-16 thomas free(bline->id_str);
1116 8a35f56c 2022-07-16 thomas free(bline->committer);
1117 8a35f56c 2022-07-16 thomas }
1118 8a35f56c 2022-07-16 thomas }
1119 8a35f56c 2022-07-16 thomas free(bca.lines);
1120 db26fc4c 2023-05-16 thomas if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1121 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1122 db26fc4c 2023-05-16 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1123 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1124 db26fc4c 2023-05-16 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1125 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1126 8a35f56c 2022-07-16 thomas if (bca.f) {
1127 d00235d8 2023-05-17 thomas const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1128 8a35f56c 2022-07-16 thomas if (error == NULL)
1129 8a35f56c 2022-07-16 thomas error = bca_err;
1130 8a35f56c 2022-07-16 thomas }
1131 8a35f56c 2022-07-16 thomas if (f1) {
1132 d00235d8 2023-05-17 thomas const struct got_error *f1_err = got_gotweb_closefile(f1);
1133 8a35f56c 2022-07-16 thomas if (error == NULL)
1134 8a35f56c 2022-07-16 thomas error = f1_err;
1135 8a35f56c 2022-07-16 thomas }
1136 8a35f56c 2022-07-16 thomas if (f2) {
1137 d00235d8 2023-05-17 thomas const struct got_error *f2_err = got_gotweb_closefile(f2);
1138 8a35f56c 2022-07-16 thomas if (error == NULL)
1139 8a35f56c 2022-07-16 thomas error = f2_err;
1140 8a35f56c 2022-07-16 thomas }
1141 8a35f56c 2022-07-16 thomas if (commit)
1142 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
1143 8a35f56c 2022-07-16 thomas if (blob)
1144 8a35f56c 2022-07-16 thomas got_object_blob_close(blob);
1145 8a35f56c 2022-07-16 thomas free(in_repo_path);
1146 8a35f56c 2022-07-16 thomas free(commit_id);
1147 aefd24c9 2022-09-03 thomas free(obj_id);
1148 8a35f56c 2022-07-16 thomas free(path);
1149 aefd24c9 2022-09-03 thomas got_ref_list_free(&refs);
1150 8a35f56c 2022-07-16 thomas return error;
1151 8a35f56c 2022-07-16 thomas }
1152 8a35f56c 2022-07-16 thomas
1153 8a35f56c 2022-07-16 thomas const struct got_error *
1154 04b2c111 2023-05-16 thomas got_open_diff_for_output(FILE **fp, struct request *c)
1155 8a35f56c 2022-07-16 thomas {
1156 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1157 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1158 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1159 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1160 8a35f56c 2022-07-16 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
1161 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1162 8a35f56c 2022-07-16 thomas FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1163 db26fc4c 2023-05-16 thomas int obj_type, fd1 = -1, fd2 = -1;
1164 5d860bce 2023-01-07 thomas
1165 dccd05b4 2023-01-10 thomas *fp = NULL;
1166 dccd05b4 2023-01-10 thomas
1167 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1168 8a35f56c 2022-07-16 thomas
1169 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1170 8a35f56c 2022-07-16 thomas if (error)
1171 8a35f56c 2022-07-16 thomas return error;
1172 8a35f56c 2022-07-16 thomas
1173 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1174 8a35f56c 2022-07-16 thomas if (error)
1175 8a35f56c 2022-07-16 thomas return error;
1176 8a35f56c 2022-07-16 thomas
1177 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1178 8a35f56c 2022-07-16 thomas if (error)
1179 8a35f56c 2022-07-16 thomas return error;
1180 8a35f56c 2022-07-16 thomas
1181 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1182 8a35f56c 2022-07-16 thomas
1183 8a35f56c 2022-07-16 thomas if (rc->parent_id != NULL &&
1184 8a35f56c 2022-07-16 thomas strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1185 dccd05b4 2023-01-10 thomas error = got_repo_match_object_id(&id1, NULL,
1186 8a35f56c 2022-07-16 thomas rc->parent_id, GOT_OBJ_TYPE_ANY,
1187 8a35f56c 2022-07-16 thomas &refs, repo);
1188 8a35f56c 2022-07-16 thomas if (error)
1189 8a35f56c 2022-07-16 thomas goto done;
1190 8a35f56c 2022-07-16 thomas }
1191 8a35f56c 2022-07-16 thomas
1192 dccd05b4 2023-01-10 thomas error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1193 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_ANY, &refs, repo);
1194 8a35f56c 2022-07-16 thomas if (error)
1195 8a35f56c 2022-07-16 thomas goto done;
1196 8a35f56c 2022-07-16 thomas
1197 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, id2);
1198 8a35f56c 2022-07-16 thomas if (error)
1199 8a35f56c 2022-07-16 thomas goto done;
1200 8a35f56c 2022-07-16 thomas
1201 db26fc4c 2023-05-16 thomas error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1202 8a35f56c 2022-07-16 thomas if (error)
1203 8a35f56c 2022-07-16 thomas goto done;
1204 8a35f56c 2022-07-16 thomas
1205 db26fc4c 2023-05-16 thomas error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1206 8a35f56c 2022-07-16 thomas if (error)
1207 8a35f56c 2022-07-16 thomas goto done;
1208 8a35f56c 2022-07-16 thomas
1209 8a35f56c 2022-07-16 thomas switch (obj_type) {
1210 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_BLOB:
1211 db26fc4c 2023-05-16 thomas error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1212 8a35f56c 2022-07-16 thomas id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1213 be97ab03 2023-01-19 thomas NULL, repo, f3);
1214 8a35f56c 2022-07-16 thomas break;
1215 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_TREE:
1216 db26fc4c 2023-05-16 thomas error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1217 8a35f56c 2022-07-16 thomas id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1218 be97ab03 2023-01-19 thomas NULL, repo, f3);
1219 8a35f56c 2022-07-16 thomas break;
1220 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_COMMIT:
1221 db26fc4c 2023-05-16 thomas error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1222 db26fc4c 2023-05-16 thomas fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1223 be97ab03 2023-01-19 thomas NULL, repo, f3);
1224 8a35f56c 2022-07-16 thomas break;
1225 8a35f56c 2022-07-16 thomas default:
1226 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
1227 8a35f56c 2022-07-16 thomas }
1228 8a35f56c 2022-07-16 thomas if (error)
1229 8a35f56c 2022-07-16 thomas goto done;
1230 8a35f56c 2022-07-16 thomas
1231 8a35f56c 2022-07-16 thomas if (fseek(f1, 0, SEEK_SET) == -1) {
1232 8a35f56c 2022-07-16 thomas error = got_ferror(f1, GOT_ERR_IO);
1233 8a35f56c 2022-07-16 thomas goto done;
1234 8a35f56c 2022-07-16 thomas }
1235 8a35f56c 2022-07-16 thomas
1236 8a35f56c 2022-07-16 thomas if (fseek(f2, 0, SEEK_SET) == -1) {
1237 8a35f56c 2022-07-16 thomas error = got_ferror(f2, GOT_ERR_IO);
1238 8a35f56c 2022-07-16 thomas goto done;
1239 8a35f56c 2022-07-16 thomas }
1240 8a35f56c 2022-07-16 thomas
1241 8a35f56c 2022-07-16 thomas if (fseek(f3, 0, SEEK_SET) == -1) {
1242 8a35f56c 2022-07-16 thomas error = got_ferror(f3, GOT_ERR_IO);
1243 8a35f56c 2022-07-16 thomas goto done;
1244 8a35f56c 2022-07-16 thomas }
1245 8a35f56c 2022-07-16 thomas
1246 dccd05b4 2023-01-10 thomas *fp = f3;
1247 79393471 2022-08-27 thomas
1248 8a35f56c 2022-07-16 thomas done:
1249 db26fc4c 2023-05-16 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1250 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1251 db26fc4c 2023-05-16 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1252 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1253 8a35f56c 2022-07-16 thomas if (f1) {
1254 d00235d8 2023-05-17 thomas const struct got_error *f1_err = got_gotweb_closefile(f1);
1255 8a35f56c 2022-07-16 thomas if (error == NULL)
1256 8a35f56c 2022-07-16 thomas error = f1_err;
1257 8a35f56c 2022-07-16 thomas }
1258 8a35f56c 2022-07-16 thomas if (f2) {
1259 d00235d8 2023-05-17 thomas const struct got_error *f2_err = got_gotweb_closefile(f2);
1260 8a35f56c 2022-07-16 thomas if (error == NULL)
1261 8a35f56c 2022-07-16 thomas error = f2_err;
1262 8a35f56c 2022-07-16 thomas }
1263 dccd05b4 2023-01-10 thomas if (error && f3) {
1264 d00235d8 2023-05-17 thomas got_gotweb_closefile(f3);
1265 dccd05b4 2023-01-10 thomas *fp = NULL;
1266 8a35f56c 2022-07-16 thomas }
1267 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
1268 8a35f56c 2022-07-16 thomas free(id1);
1269 8a35f56c 2022-07-16 thomas free(id2);
1270 8a35f56c 2022-07-16 thomas return error;
1271 8a35f56c 2022-07-16 thomas }
1272 8a35f56c 2022-07-16 thomas
1273 8a35f56c 2022-07-16 thomas static const struct got_error *
1274 8a35f56c 2022-07-16 thomas got_init_repo_commit(struct repo_commit **rc)
1275 8a35f56c 2022-07-16 thomas {
1276 8a35f56c 2022-07-16 thomas *rc = calloc(1, sizeof(**rc));
1277 8a35f56c 2022-07-16 thomas if (*rc == NULL)
1278 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
1279 8a35f56c 2022-07-16 thomas
1280 8a35f56c 2022-07-16 thomas (*rc)->path = NULL;
1281 8a35f56c 2022-07-16 thomas (*rc)->refs_str = NULL;
1282 8a35f56c 2022-07-16 thomas (*rc)->commit_id = NULL;
1283 8a35f56c 2022-07-16 thomas (*rc)->committer = NULL;
1284 8a35f56c 2022-07-16 thomas (*rc)->author = NULL;
1285 8a35f56c 2022-07-16 thomas (*rc)->parent_id = NULL;
1286 8a35f56c 2022-07-16 thomas (*rc)->tree_id = NULL;
1287 8a35f56c 2022-07-16 thomas (*rc)->commit_msg = NULL;
1288 8a35f56c 2022-07-16 thomas
1289 e2af4cd7 2022-08-31 thomas return NULL;
1290 8a35f56c 2022-07-16 thomas }
1291 8a35f56c 2022-07-16 thomas
1292 8a35f56c 2022-07-16 thomas static const struct got_error *
1293 8a35f56c 2022-07-16 thomas got_init_repo_tag(struct repo_tag **rt)
1294 8a35f56c 2022-07-16 thomas {
1295 8a35f56c 2022-07-16 thomas *rt = calloc(1, sizeof(**rt));
1296 8a35f56c 2022-07-16 thomas if (*rt == NULL)
1297 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
1298 8a35f56c 2022-07-16 thomas
1299 8a35f56c 2022-07-16 thomas (*rt)->commit_id = NULL;
1300 8a35f56c 2022-07-16 thomas (*rt)->tag_name = NULL;
1301 8a35f56c 2022-07-16 thomas (*rt)->tag_commit = NULL;
1302 8a35f56c 2022-07-16 thomas (*rt)->commit_msg = NULL;
1303 8a35f56c 2022-07-16 thomas (*rt)->tagger = NULL;
1304 8a35f56c 2022-07-16 thomas
1305 e2af4cd7 2022-08-31 thomas return NULL;
1306 8a35f56c 2022-07-16 thomas }