Blame


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