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