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 a596b957 2022-07-14 tracey #include <sys/socket.h>
19 a596b957 2022-07-14 tracey #include <sys/stat.h>
20 a596b957 2022-07-14 tracey
21 a596b957 2022-07-14 tracey #include <event.h>
22 a596b957 2022-07-14 tracey #include <imsg.h>
23 a596b957 2022-07-14 tracey #include <sha1.h>
24 a596b957 2022-07-14 tracey #include <stdlib.h>
25 a596b957 2022-07-14 tracey #include <stdio.h>
26 a596b957 2022-07-14 tracey #include <string.h>
27 a596b957 2022-07-14 tracey #include <unistd.h>
28 a596b957 2022-07-14 tracey
29 a596b957 2022-07-14 tracey #include "got_error.h"
30 a596b957 2022-07-14 tracey #include "got_object.h"
31 a596b957 2022-07-14 tracey #include "got_reference.h"
32 a596b957 2022-07-14 tracey #include "got_repository.h"
33 a596b957 2022-07-14 tracey #include "got_path.h"
34 a596b957 2022-07-14 tracey #include "got_cancel.h"
35 a596b957 2022-07-14 tracey #include "got_diff.h"
36 a596b957 2022-07-14 tracey #include "got_commit_graph.h"
37 a596b957 2022-07-14 tracey #include "got_blame.h"
38 a596b957 2022-07-14 tracey #include "got_privsep.h"
39 a596b957 2022-07-14 tracey
40 a596b957 2022-07-14 tracey #include "proc.h"
41 a596b957 2022-07-14 tracey #include "gotwebd.h"
42 a596b957 2022-07-14 tracey
43 a596b957 2022-07-14 tracey static const struct got_error *got_init_repo_commit(struct repo_commit **);
44 a596b957 2022-07-14 tracey static const struct got_error *got_init_repo_tag(struct repo_tag **);
45 a596b957 2022-07-14 tracey static const struct got_error *got_get_repo_commit(struct request *,
46 a596b957 2022-07-14 tracey struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
47 a596b957 2022-07-14 tracey struct got_object_id *);
48 a596b957 2022-07-14 tracey static const struct got_error *got_gotweb_dupfd(int *, int *);
49 a596b957 2022-07-14 tracey static const struct got_error *got_gotweb_openfile(FILE **, int *, int *);
50 a596b957 2022-07-14 tracey static const struct got_error *got_gotweb_flushfile(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 a596b957 2022-07-14 tracey static int
55 a596b957 2022-07-14 tracey isbinary(const uint8_t *buf, size_t n)
56 a596b957 2022-07-14 tracey {
57 a596b957 2022-07-14 tracey size_t i;
58 a596b957 2022-07-14 tracey
59 a596b957 2022-07-14 tracey for (i = 0; i < n; i++)
60 a596b957 2022-07-14 tracey if (buf[i] == 0)
61 a596b957 2022-07-14 tracey return 1;
62 a596b957 2022-07-14 tracey return 0;
63 a596b957 2022-07-14 tracey }
64 a596b957 2022-07-14 tracey
65 a596b957 2022-07-14 tracey
66 a596b957 2022-07-14 tracey static const struct got_error *
67 a596b957 2022-07-14 tracey got_gotweb_flushfile(FILE *f, int fd)
68 a596b957 2022-07-14 tracey {
69 a596b957 2022-07-14 tracey if (fseek(f, 0, SEEK_SET) == -1)
70 a596b957 2022-07-14 tracey return got_error_from_errno("fseek");
71 a596b957 2022-07-14 tracey
72 a596b957 2022-07-14 tracey if (ftruncate(fd, 0) == -1)
73 a596b957 2022-07-14 tracey return got_error_from_errno("ftruncate");
74 a596b957 2022-07-14 tracey
75 a596b957 2022-07-14 tracey if (fsync(fd) == -1)
76 a596b957 2022-07-14 tracey return got_error_from_errno("fsync");
77 a596b957 2022-07-14 tracey
78 a596b957 2022-07-14 tracey if (f && fclose(f) == EOF)
79 a596b957 2022-07-14 tracey return got_error_from_errno("fclose");
80 a596b957 2022-07-14 tracey
81 a596b957 2022-07-14 tracey if (fd != -1 && close(fd) != -1)
82 a596b957 2022-07-14 tracey return got_error_from_errno("close");
83 a596b957 2022-07-14 tracey
84 a596b957 2022-07-14 tracey return NULL;
85 a596b957 2022-07-14 tracey }
86 a596b957 2022-07-14 tracey
87 a596b957 2022-07-14 tracey static const struct got_error *
88 a596b957 2022-07-14 tracey got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
89 a596b957 2022-07-14 tracey {
90 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
91 a596b957 2022-07-14 tracey
92 a596b957 2022-07-14 tracey *fd = dup(*priv_fd);
93 a596b957 2022-07-14 tracey
94 a596b957 2022-07-14 tracey if (*fd < 0)
95 a596b957 2022-07-14 tracey return NULL;
96 a596b957 2022-07-14 tracey
97 a596b957 2022-07-14 tracey *f = fdopen(*fd, "w+");
98 a596b957 2022-07-14 tracey if (*f == NULL) {
99 a596b957 2022-07-14 tracey close(*fd);
100 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_PRIVSEP_NO_FD);
101 a596b957 2022-07-14 tracey }
102 a596b957 2022-07-14 tracey
103 a596b957 2022-07-14 tracey return error;
104 a596b957 2022-07-14 tracey }
105 a596b957 2022-07-14 tracey
106 a596b957 2022-07-14 tracey static const struct got_error *
107 a596b957 2022-07-14 tracey got_gotweb_dupfd(int *priv_fd, int *fd)
108 a596b957 2022-07-14 tracey {
109 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
110 a596b957 2022-07-14 tracey
111 a596b957 2022-07-14 tracey *fd = dup(*priv_fd);
112 a596b957 2022-07-14 tracey
113 a596b957 2022-07-14 tracey if (*fd < 0)
114 a596b957 2022-07-14 tracey return NULL;
115 a596b957 2022-07-14 tracey
116 a596b957 2022-07-14 tracey return error;
117 a596b957 2022-07-14 tracey }
118 a596b957 2022-07-14 tracey
119 a596b957 2022-07-14 tracey const struct got_error *
120 a596b957 2022-07-14 tracey got_get_repo_owner(char **owner, struct request *c, char *dir)
121 a596b957 2022-07-14 tracey {
122 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
123 a596b957 2022-07-14 tracey struct server *srv = c->srv;
124 a596b957 2022-07-14 tracey struct transport *t = c->t;
125 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
126 a596b957 2022-07-14 tracey const char *gitconfig_owner;
127 a596b957 2022-07-14 tracey
128 a596b957 2022-07-14 tracey *owner = NULL;
129 a596b957 2022-07-14 tracey
130 a596b957 2022-07-14 tracey if (srv->show_repo_owner == 0)
131 a596b957 2022-07-14 tracey return NULL;
132 a596b957 2022-07-14 tracey
133 a596b957 2022-07-14 tracey gitconfig_owner = got_repo_get_gitconfig_owner(repo);
134 a596b957 2022-07-14 tracey if (gitconfig_owner) {
135 a596b957 2022-07-14 tracey *owner = strdup(gitconfig_owner);
136 a596b957 2022-07-14 tracey if (*owner == NULL)
137 a596b957 2022-07-14 tracey return got_error_from_errno("strdup");
138 a596b957 2022-07-14 tracey }
139 a596b957 2022-07-14 tracey return error;
140 a596b957 2022-07-14 tracey }
141 a596b957 2022-07-14 tracey
142 a596b957 2022-07-14 tracey const struct got_error *
143 a596b957 2022-07-14 tracey got_get_repo_age(char **repo_age, struct request *c, char *dir,
144 a596b957 2022-07-14 tracey const char *refname, int ref_tm)
145 a596b957 2022-07-14 tracey {
146 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
147 a596b957 2022-07-14 tracey struct server *srv = c->srv;
148 a596b957 2022-07-14 tracey struct transport *t = c->t;
149 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
150 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
151 a596b957 2022-07-14 tracey struct got_reflist_head refs;
152 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
153 a596b957 2022-07-14 tracey time_t committer_time = 0, cmp_time = 0;
154 a596b957 2022-07-14 tracey
155 a596b957 2022-07-14 tracey *repo_age = NULL;
156 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
157 a596b957 2022-07-14 tracey
158 a596b957 2022-07-14 tracey if (srv->show_repo_age == 0)
159 a596b957 2022-07-14 tracey return NULL;
160 a596b957 2022-07-14 tracey
161 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/heads",
162 a596b957 2022-07-14 tracey got_ref_cmp_by_name, NULL);
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 /*
167 a596b957 2022-07-14 tracey * Find the youngest branch tip in the repository, or the age of
168 a596b957 2022-07-14 tracey * the a specific branch tip if a name was provided by the caller.
169 a596b957 2022-07-14 tracey */
170 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
171 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
172 a596b957 2022-07-14 tracey
173 a596b957 2022-07-14 tracey if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
174 a596b957 2022-07-14 tracey continue;
175 a596b957 2022-07-14 tracey
176 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, re->ref);
177 a596b957 2022-07-14 tracey if (error)
178 a596b957 2022-07-14 tracey goto done;
179 a596b957 2022-07-14 tracey
180 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
181 a596b957 2022-07-14 tracey free(id);
182 a596b957 2022-07-14 tracey if (error)
183 a596b957 2022-07-14 tracey goto done;
184 a596b957 2022-07-14 tracey
185 a596b957 2022-07-14 tracey committer_time =
186 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
187 a596b957 2022-07-14 tracey got_object_commit_close(commit);
188 a596b957 2022-07-14 tracey if (cmp_time < committer_time)
189 a596b957 2022-07-14 tracey cmp_time = committer_time;
190 a596b957 2022-07-14 tracey
191 a596b957 2022-07-14 tracey if (refname)
192 a596b957 2022-07-14 tracey break;
193 a596b957 2022-07-14 tracey }
194 a596b957 2022-07-14 tracey
195 a596b957 2022-07-14 tracey if (cmp_time != 0) {
196 a596b957 2022-07-14 tracey committer_time = cmp_time;
197 a596b957 2022-07-14 tracey error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
198 a596b957 2022-07-14 tracey }
199 a596b957 2022-07-14 tracey done:
200 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
201 a596b957 2022-07-14 tracey return error;
202 a596b957 2022-07-14 tracey }
203 a596b957 2022-07-14 tracey
204 a596b957 2022-07-14 tracey static const struct got_error *
205 a596b957 2022-07-14 tracey got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
206 a596b957 2022-07-14 tracey struct got_commit_object *commit, struct got_reflist_head *refs,
207 a596b957 2022-07-14 tracey struct got_object_id *id)
208 a596b957 2022-07-14 tracey {
209 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
210 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
211 a596b957 2022-07-14 tracey struct got_object_id *id2 = NULL;
212 a596b957 2022-07-14 tracey struct got_object_qid *parent_id;
213 a596b957 2022-07-14 tracey struct transport *t = c->t;
214 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
215 a596b957 2022-07-14 tracey char *commit_msg = NULL, *commit_msg0;
216 a596b957 2022-07-14 tracey
217 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, refs, entry) {
218 a596b957 2022-07-14 tracey char *s;
219 a596b957 2022-07-14 tracey const char *name;
220 a596b957 2022-07-14 tracey struct got_tag_object *tag = NULL;
221 a596b957 2022-07-14 tracey struct got_object_id *ref_id;
222 a596b957 2022-07-14 tracey int cmp;
223 a596b957 2022-07-14 tracey
224 a596b957 2022-07-14 tracey if (got_ref_is_symbolic(re->ref))
225 a596b957 2022-07-14 tracey continue;
226 a596b957 2022-07-14 tracey
227 a596b957 2022-07-14 tracey name = got_ref_get_name(re->ref);
228 a596b957 2022-07-14 tracey if (strncmp(name, "refs/", 5) == 0)
229 a596b957 2022-07-14 tracey name += 5;
230 a596b957 2022-07-14 tracey if (strncmp(name, "got/", 4) == 0)
231 a596b957 2022-07-14 tracey continue;
232 a596b957 2022-07-14 tracey if (strncmp(name, "heads/", 6) == 0)
233 a596b957 2022-07-14 tracey name += 6;
234 a596b957 2022-07-14 tracey if (strncmp(name, "remotes/", 8) == 0) {
235 a596b957 2022-07-14 tracey name += 8;
236 a596b957 2022-07-14 tracey s = strstr(name, "/" GOT_REF_HEAD);
237 a596b957 2022-07-14 tracey if (s != NULL && s[strlen(s)] == '\0')
238 a596b957 2022-07-14 tracey continue;
239 a596b957 2022-07-14 tracey }
240 a596b957 2022-07-14 tracey error = got_ref_resolve(&ref_id, t->repo, re->ref);
241 a596b957 2022-07-14 tracey if (error)
242 a596b957 2022-07-14 tracey return error;
243 a596b957 2022-07-14 tracey if (strncmp(name, "tags/", 5) == 0) {
244 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, t->repo, ref_id);
245 a596b957 2022-07-14 tracey if (error) {
246 a596b957 2022-07-14 tracey if (error->code != GOT_ERR_OBJ_TYPE) {
247 a596b957 2022-07-14 tracey free(ref_id);
248 a596b957 2022-07-14 tracey continue;
249 a596b957 2022-07-14 tracey }
250 a596b957 2022-07-14 tracey /*
251 a596b957 2022-07-14 tracey * Ref points at something other
252 a596b957 2022-07-14 tracey * than a tag.
253 a596b957 2022-07-14 tracey */
254 a596b957 2022-07-14 tracey error = NULL;
255 a596b957 2022-07-14 tracey tag = NULL;
256 a596b957 2022-07-14 tracey }
257 a596b957 2022-07-14 tracey }
258 a596b957 2022-07-14 tracey cmp = got_object_id_cmp(tag ?
259 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag) : ref_id, id);
260 a596b957 2022-07-14 tracey free(ref_id);
261 a596b957 2022-07-14 tracey if (tag)
262 a596b957 2022-07-14 tracey got_object_tag_close(tag);
263 a596b957 2022-07-14 tracey if (cmp != 0)
264 a596b957 2022-07-14 tracey continue;
265 a596b957 2022-07-14 tracey s = repo_commit->refs_str;
266 a596b957 2022-07-14 tracey if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
267 a596b957 2022-07-14 tracey s ? ", " : "", name) == -1) {
268 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
269 a596b957 2022-07-14 tracey free(s);
270 a596b957 2022-07-14 tracey repo_commit->refs_str = NULL;
271 a596b957 2022-07-14 tracey return error;
272 a596b957 2022-07-14 tracey }
273 a596b957 2022-07-14 tracey free(s);
274 a596b957 2022-07-14 tracey }
275 a596b957 2022-07-14 tracey
276 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->commit_id, id);
277 a596b957 2022-07-14 tracey if (error)
278 a596b957 2022-07-14 tracey return error;
279 a596b957 2022-07-14 tracey
280 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->tree_id,
281 a596b957 2022-07-14 tracey got_object_commit_get_tree_id(commit));
282 a596b957 2022-07-14 tracey if (error)
283 a596b957 2022-07-14 tracey return error;
284 a596b957 2022-07-14 tracey
285 a596b957 2022-07-14 tracey if (qs->action == DIFF) {
286 a596b957 2022-07-14 tracey parent_id = STAILQ_FIRST(
287 a596b957 2022-07-14 tracey got_object_commit_get_parent_ids(commit));
288 a596b957 2022-07-14 tracey if (parent_id != NULL) {
289 a596b957 2022-07-14 tracey id2 = got_object_id_dup(&parent_id->id);
290 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->parent_id, id2);
291 a596b957 2022-07-14 tracey if (error)
292 a596b957 2022-07-14 tracey return error;
293 a596b957 2022-07-14 tracey free(id2);
294 a596b957 2022-07-14 tracey } else {
295 a596b957 2022-07-14 tracey repo_commit->parent_id = strdup("/dev/null");
296 a596b957 2022-07-14 tracey if (repo_commit->parent_id == NULL) {
297 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
298 a596b957 2022-07-14 tracey return error;
299 a596b957 2022-07-14 tracey }
300 a596b957 2022-07-14 tracey }
301 a596b957 2022-07-14 tracey }
302 a596b957 2022-07-14 tracey
303 a596b957 2022-07-14 tracey repo_commit->committer_time =
304 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
305 a596b957 2022-07-14 tracey
306 a596b957 2022-07-14 tracey repo_commit->author =
307 a596b957 2022-07-14 tracey strdup(got_object_commit_get_author(commit));
308 a596b957 2022-07-14 tracey if (repo_commit->author == NULL) {
309 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
310 a596b957 2022-07-14 tracey return error;
311 a596b957 2022-07-14 tracey }
312 a596b957 2022-07-14 tracey repo_commit->committer =
313 a596b957 2022-07-14 tracey strdup(got_object_commit_get_committer(commit));
314 a596b957 2022-07-14 tracey if (repo_commit->committer == NULL) {
315 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
316 a596b957 2022-07-14 tracey return error;
317 a596b957 2022-07-14 tracey }
318 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
319 a596b957 2022-07-14 tracey if (error)
320 a596b957 2022-07-14 tracey return error;
321 a596b957 2022-07-14 tracey
322 a596b957 2022-07-14 tracey commit_msg = commit_msg0;
323 a596b957 2022-07-14 tracey while (*commit_msg == '\n')
324 a596b957 2022-07-14 tracey commit_msg++;
325 a596b957 2022-07-14 tracey
326 a596b957 2022-07-14 tracey repo_commit->commit_msg = strdup(commit_msg);
327 a596b957 2022-07-14 tracey if (repo_commit->commit_msg == NULL)
328 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
329 a596b957 2022-07-14 tracey free(commit_msg0);
330 a596b957 2022-07-14 tracey return error;
331 a596b957 2022-07-14 tracey }
332 a596b957 2022-07-14 tracey
333 a596b957 2022-07-14 tracey const struct got_error *
334 a596b957 2022-07-14 tracey got_get_repo_commits(struct request *c, int limit)
335 a596b957 2022-07-14 tracey {
336 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
337 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
338 a596b957 2022-07-14 tracey struct got_commit_graph *graph = NULL;
339 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
340 a596b957 2022-07-14 tracey struct got_reflist_head refs;
341 a596b957 2022-07-14 tracey struct got_reference *ref;
342 a596b957 2022-07-14 tracey struct repo_commit *repo_commit = NULL;
343 a596b957 2022-07-14 tracey struct server *srv = c->srv;
344 a596b957 2022-07-14 tracey struct transport *t = c->t;
345 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
346 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
347 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
348 a596b957 2022-07-14 tracey char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
349 a596b957 2022-07-14 tracey int chk_next = 0, chk_multi = 0, commit_found = 0;
350 a596b957 2022-07-14 tracey int obj_type, limit_chk = 0;
351 a596b957 2022-07-14 tracey
352 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
353 a596b957 2022-07-14 tracey
354 a596b957 2022-07-14 tracey if (qs->file != NULL && strlen(qs->file) > 0)
355 a596b957 2022-07-14 tracey if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
356 a596b957 2022-07-14 tracey qs->file) == -1)
357 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
358 a596b957 2022-07-14 tracey
359 a596b957 2022-07-14 tracey if (asprintf(&repo_path, "%s/%s", srv->repos_path,
360 a596b957 2022-07-14 tracey repo_dir->name) == -1)
361 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
362 a596b957 2022-07-14 tracey
363 a596b957 2022-07-14 tracey error = got_init_repo_commit(&repo_commit);
364 a596b957 2022-07-14 tracey if (error)
365 a596b957 2022-07-14 tracey return error;
366 a596b957 2022-07-14 tracey
367 a596b957 2022-07-14 tracey /*
368 a596b957 2022-07-14 tracey * XXX: jumping directly to a commit id via
369 a596b957 2022-07-14 tracey * got_repo_match_object_id_prefix significantly improves performance,
370 a596b957 2022-07-14 tracey * but does not allow us to create a PREVIOUS button, since commits can
371 a596b957 2022-07-14 tracey * only be itereated forward. So, we have to match as we iterate from
372 a596b957 2022-07-14 tracey * the headref.
373 a596b957 2022-07-14 tracey */
374 a596b957 2022-07-14 tracey if (qs->action == BRIEFS || qs->action == COMMITS ||
375 a596b957 2022-07-14 tracey (qs->action == TREE && qs->commit == NULL)) {
376 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->headref, 0);
377 a596b957 2022-07-14 tracey if (error)
378 a596b957 2022-07-14 tracey goto done;
379 a596b957 2022-07-14 tracey
380 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
381 a596b957 2022-07-14 tracey got_ref_close(ref);
382 a596b957 2022-07-14 tracey if (error)
383 a596b957 2022-07-14 tracey goto done;
384 a596b957 2022-07-14 tracey } else if (qs->commit != NULL) {
385 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->commit, 0);
386 a596b957 2022-07-14 tracey if (error == NULL) {
387 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
388 a596b957 2022-07-14 tracey if (error)
389 a596b957 2022-07-14 tracey goto done;
390 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, id);
391 a596b957 2022-07-14 tracey got_ref_close(ref);
392 a596b957 2022-07-14 tracey if (error)
393 a596b957 2022-07-14 tracey goto done;
394 a596b957 2022-07-14 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
395 a596b957 2022-07-14 tracey struct got_tag_object *tag;
396 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, repo, id);
397 a596b957 2022-07-14 tracey if (error)
398 a596b957 2022-07-14 tracey goto done;
399 a596b957 2022-07-14 tracey if (got_object_tag_get_object_type(tag) !=
400 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT) {
401 a596b957 2022-07-14 tracey got_object_tag_close(tag);
402 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
403 a596b957 2022-07-14 tracey goto done;
404 a596b957 2022-07-14 tracey }
405 a596b957 2022-07-14 tracey free(id);
406 a596b957 2022-07-14 tracey id = got_object_id_dup(
407 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag));
408 a596b957 2022-07-14 tracey if (id == NULL)
409 a596b957 2022-07-14 tracey error = got_error_from_errno(
410 a596b957 2022-07-14 tracey "got_object_id_dup");
411 a596b957 2022-07-14 tracey got_object_tag_close(tag);
412 a596b957 2022-07-14 tracey if (error)
413 a596b957 2022-07-14 tracey goto done;
414 a596b957 2022-07-14 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
415 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
416 a596b957 2022-07-14 tracey goto done;
417 a596b957 2022-07-14 tracey }
418 a596b957 2022-07-14 tracey }
419 a596b957 2022-07-14 tracey error = got_repo_match_object_id_prefix(&id, qs->commit,
420 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, repo);
421 a596b957 2022-07-14 tracey if (error)
422 a596b957 2022-07-14 tracey goto done;
423 a596b957 2022-07-14 tracey }
424 a596b957 2022-07-14 tracey
425 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, repo_path);
426 a596b957 2022-07-14 tracey if (error)
427 a596b957 2022-07-14 tracey goto done;
428 a596b957 2022-07-14 tracey
429 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
430 a596b957 2022-07-14 tracey if (error)
431 a596b957 2022-07-14 tracey goto done;
432 a596b957 2022-07-14 tracey
433 a596b957 2022-07-14 tracey if (qs->file != NULL && strlen(qs->file) > 0) {
434 a596b957 2022-07-14 tracey error = got_commit_graph_open(&graph, file_path, 0);
435 a596b957 2022-07-14 tracey if (error)
436 a596b957 2022-07-14 tracey goto done;
437 a596b957 2022-07-14 tracey } else {
438 a596b957 2022-07-14 tracey error = got_commit_graph_open(&graph, in_repo_path, 0);
439 a596b957 2022-07-14 tracey if (error)
440 a596b957 2022-07-14 tracey goto done;
441 a596b957 2022-07-14 tracey }
442 a596b957 2022-07-14 tracey
443 a596b957 2022-07-14 tracey error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
444 a596b957 2022-07-14 tracey if (error)
445 a596b957 2022-07-14 tracey goto done;
446 a596b957 2022-07-14 tracey
447 a596b957 2022-07-14 tracey for (;;) {
448 a596b957 2022-07-14 tracey if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
449 a596b957 2022-07-14 tracey commit_found == 0 && repo_commit->commit_id != NULL) {
450 a596b957 2022-07-14 tracey t->prev_id = strdup(repo_commit->commit_id);
451 a596b957 2022-07-14 tracey if (t->prev_id == NULL) {
452 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
453 a596b957 2022-07-14 tracey goto done;
454 a596b957 2022-07-14 tracey }
455 a596b957 2022-07-14 tracey }
456 a596b957 2022-07-14 tracey
457 a596b957 2022-07-14 tracey error = got_commit_graph_iter_next(&id, graph, repo, NULL,
458 a596b957 2022-07-14 tracey NULL);
459 a596b957 2022-07-14 tracey if (error) {
460 a596b957 2022-07-14 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
461 a596b957 2022-07-14 tracey error = NULL;
462 a596b957 2022-07-14 tracey goto done;
463 a596b957 2022-07-14 tracey }
464 a596b957 2022-07-14 tracey if (id == NULL)
465 a596b957 2022-07-14 tracey goto done;
466 a596b957 2022-07-14 tracey
467 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
468 a596b957 2022-07-14 tracey if (error)
469 a596b957 2022-07-14 tracey goto done;
470 a596b957 2022-07-14 tracey
471 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
472 a596b957 2022-07-14 tracey NULL);
473 a596b957 2022-07-14 tracey if (error)
474 a596b957 2022-07-14 tracey goto done;
475 a596b957 2022-07-14 tracey
476 a596b957 2022-07-14 tracey error = got_get_repo_commit(c, repo_commit, commit,
477 a596b957 2022-07-14 tracey &refs, id);
478 a596b957 2022-07-14 tracey if (error)
479 a596b957 2022-07-14 tracey goto done;
480 a596b957 2022-07-14 tracey
481 a596b957 2022-07-14 tracey if (qs->commit != NULL && commit_found == 0 && limit != 1) {
482 a596b957 2022-07-14 tracey if (strcmp(qs->commit, repo_commit->commit_id) == 0)
483 a596b957 2022-07-14 tracey commit_found = 1;
484 a596b957 2022-07-14 tracey else if (qs->file != NULL && strlen(qs->file) > 0 &&
485 a596b957 2022-07-14 tracey qs->page == 0)
486 a596b957 2022-07-14 tracey commit_found = 1;
487 a596b957 2022-07-14 tracey else {
488 a596b957 2022-07-14 tracey limit_chk++;
489 a596b957 2022-07-14 tracey free(id);
490 a596b957 2022-07-14 tracey id = NULL;
491 a596b957 2022-07-14 tracey continue;
492 a596b957 2022-07-14 tracey }
493 a596b957 2022-07-14 tracey }
494 a596b957 2022-07-14 tracey
495 a596b957 2022-07-14 tracey struct repo_commit *new_repo_commit = NULL;
496 a596b957 2022-07-14 tracey error = got_init_repo_commit(&new_repo_commit);
497 a596b957 2022-07-14 tracey if (error)
498 a596b957 2022-07-14 tracey goto done;
499 a596b957 2022-07-14 tracey
500 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&t->repo_commits, new_repo_commit, entry);
501 a596b957 2022-07-14 tracey
502 a596b957 2022-07-14 tracey error = got_get_repo_commit(c, new_repo_commit, commit,
503 a596b957 2022-07-14 tracey &refs, id);
504 a596b957 2022-07-14 tracey if (error)
505 a596b957 2022-07-14 tracey goto done;
506 a596b957 2022-07-14 tracey
507 a596b957 2022-07-14 tracey free(id);
508 a596b957 2022-07-14 tracey id = NULL;
509 a596b957 2022-07-14 tracey
510 a596b957 2022-07-14 tracey if (limit == 1 && chk_multi == 0 &&
511 a596b957 2022-07-14 tracey srv->max_commits_display != 1)
512 a596b957 2022-07-14 tracey commit_found = 1;
513 a596b957 2022-07-14 tracey else {
514 a596b957 2022-07-14 tracey chk_multi = 1;
515 a596b957 2022-07-14 tracey
516 a596b957 2022-07-14 tracey /*
517 a596b957 2022-07-14 tracey * check for one more commit before breaking,
518 a596b957 2022-07-14 tracey * so we know whether to navigate through briefs
519 a596b957 2022-07-14 tracey * commits and summary
520 a596b957 2022-07-14 tracey */
521 a596b957 2022-07-14 tracey if (chk_next && (qs->action == BRIEFS ||
522 a596b957 2022-07-14 tracey qs->action == COMMITS || qs->action == SUMMARY)) {
523 a596b957 2022-07-14 tracey t->next_id = strdup(new_repo_commit->commit_id);
524 a596b957 2022-07-14 tracey if (t->next_id == NULL) {
525 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
526 a596b957 2022-07-14 tracey goto done;
527 a596b957 2022-07-14 tracey }
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 commit = NULL;
531 a596b957 2022-07-14 tracey }
532 a596b957 2022-07-14 tracey if (t->next_id == NULL) {
533 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
534 a596b957 2022-07-14 tracey goto done;
535 a596b957 2022-07-14 tracey }
536 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_commits, new_repo_commit,
537 a596b957 2022-07-14 tracey entry);
538 a596b957 2022-07-14 tracey gotweb_free_repo_commit(new_repo_commit);
539 a596b957 2022-07-14 tracey goto done;
540 a596b957 2022-07-14 tracey }
541 a596b957 2022-07-14 tracey }
542 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
543 a596b957 2022-07-14 tracey if (error || (limit && --limit == 0)) {
544 a596b957 2022-07-14 tracey if (commit_found || (qs->file != NULL &&
545 a596b957 2022-07-14 tracey strlen(qs->file) > 0))
546 a596b957 2022-07-14 tracey if (chk_multi == 0)
547 a596b957 2022-07-14 tracey break;
548 a596b957 2022-07-14 tracey chk_next = 1;
549 a596b957 2022-07-14 tracey }
550 a596b957 2022-07-14 tracey if (commit) {
551 a596b957 2022-07-14 tracey got_object_commit_close(commit);
552 a596b957 2022-07-14 tracey commit = NULL;
553 a596b957 2022-07-14 tracey }
554 a596b957 2022-07-14 tracey }
555 a596b957 2022-07-14 tracey done:
556 a596b957 2022-07-14 tracey gotweb_free_repo_commit(repo_commit);
557 a596b957 2022-07-14 tracey if (commit)
558 a596b957 2022-07-14 tracey got_object_commit_close(commit);
559 a596b957 2022-07-14 tracey if (graph)
560 a596b957 2022-07-14 tracey got_commit_graph_close(graph);
561 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
562 a596b957 2022-07-14 tracey free(file_path);
563 a596b957 2022-07-14 tracey free(repo_path);
564 a596b957 2022-07-14 tracey free(id);
565 a596b957 2022-07-14 tracey return error;
566 a596b957 2022-07-14 tracey }
567 a596b957 2022-07-14 tracey
568 a596b957 2022-07-14 tracey const struct got_error *
569 a596b957 2022-07-14 tracey got_get_repo_tags(struct request *c, int limit)
570 a596b957 2022-07-14 tracey {
571 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
572 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
573 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
574 a596b957 2022-07-14 tracey struct got_reflist_head refs;
575 a596b957 2022-07-14 tracey struct got_reference *ref;
576 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
577 a596b957 2022-07-14 tracey struct server *srv = c->srv;
578 a596b957 2022-07-14 tracey struct transport *t = c->t;
579 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
580 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
581 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
582 a596b957 2022-07-14 tracey struct got_tag_object *tag = NULL;
583 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
584 a596b957 2022-07-14 tracey char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
585 a596b957 2022-07-14 tracey char *commit_msg = NULL, *commit_msg0 = NULL;
586 a596b957 2022-07-14 tracey int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
587 a596b957 2022-07-14 tracey
588 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
589 a596b957 2022-07-14 tracey
590 a596b957 2022-07-14 tracey if (asprintf(&repo_path, "%s/%s", srv->repos_path,
591 a596b957 2022-07-14 tracey repo_dir->name) == -1)
592 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
593 a596b957 2022-07-14 tracey
594 a596b957 2022-07-14 tracey if (error)
595 a596b957 2022-07-14 tracey return error;
596 a596b957 2022-07-14 tracey
597 a596b957 2022-07-14 tracey if (qs->commit == NULL && qs->action == TAGS) {
598 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->headref, 0);
599 a596b957 2022-07-14 tracey if (error)
600 a596b957 2022-07-14 tracey goto err;
601 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
602 a596b957 2022-07-14 tracey got_ref_close(ref);
603 a596b957 2022-07-14 tracey if (error)
604 a596b957 2022-07-14 tracey goto err;
605 a596b957 2022-07-14 tracey } else if (qs->commit == NULL && qs->action == TAG) {
606 a596b957 2022-07-14 tracey error = got_error_msg(GOT_ERR_EOF, "commit id missing");
607 a596b957 2022-07-14 tracey goto err;
608 a596b957 2022-07-14 tracey } else {
609 a596b957 2022-07-14 tracey error = got_repo_match_object_id_prefix(&id, qs->commit,
610 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, repo);
611 a596b957 2022-07-14 tracey if (error)
612 a596b957 2022-07-14 tracey goto err;
613 a596b957 2022-07-14 tracey }
614 a596b957 2022-07-14 tracey
615 a596b957 2022-07-14 tracey if (qs->action != SUMMARY && qs->action != TAGS) {
616 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
617 a596b957 2022-07-14 tracey if (error)
618 a596b957 2022-07-14 tracey goto err;
619 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
620 a596b957 2022-07-14 tracey if (error)
621 a596b957 2022-07-14 tracey goto err;
622 a596b957 2022-07-14 tracey if (commit) {
623 a596b957 2022-07-14 tracey got_object_commit_close(commit);
624 a596b957 2022-07-14 tracey commit = NULL;
625 a596b957 2022-07-14 tracey }
626 a596b957 2022-07-14 tracey }
627 a596b957 2022-07-14 tracey
628 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, repo_path);
629 a596b957 2022-07-14 tracey if (error)
630 a596b957 2022-07-14 tracey goto err;
631 a596b957 2022-07-14 tracey
632 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
633 a596b957 2022-07-14 tracey repo);
634 a596b957 2022-07-14 tracey if (error)
635 a596b957 2022-07-14 tracey goto err;
636 a596b957 2022-07-14 tracey
637 a596b957 2022-07-14 tracey if (limit == 1)
638 a596b957 2022-07-14 tracey chk_multi = 0;
639 a596b957 2022-07-14 tracey
640 a596b957 2022-07-14 tracey /*
641 a596b957 2022-07-14 tracey * XXX: again, see previous message about caching
642 a596b957 2022-07-14 tracey */
643 a596b957 2022-07-14 tracey
644 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
645 a596b957 2022-07-14 tracey struct repo_tag *new_repo_tag = NULL;
646 a596b957 2022-07-14 tracey error = got_init_repo_tag(&new_repo_tag);
647 a596b957 2022-07-14 tracey if (error)
648 a596b957 2022-07-14 tracey goto err;
649 a596b957 2022-07-14 tracey
650 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
651 a596b957 2022-07-14 tracey
652 a596b957 2022-07-14 tracey new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
653 a596b957 2022-07-14 tracey if (new_repo_tag->tag_name == NULL) {
654 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
655 a596b957 2022-07-14 tracey goto err;
656 a596b957 2022-07-14 tracey }
657 a596b957 2022-07-14 tracey
658 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, re->ref);
659 a596b957 2022-07-14 tracey if (error)
660 a596b957 2022-07-14 tracey goto done;
661 a596b957 2022-07-14 tracey
662 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, repo, id);
663 a596b957 2022-07-14 tracey if (error) {
664 a596b957 2022-07-14 tracey if (error->code != GOT_ERR_OBJ_TYPE) {
665 a596b957 2022-07-14 tracey free(id);
666 a596b957 2022-07-14 tracey id = NULL;
667 a596b957 2022-07-14 tracey goto done;
668 a596b957 2022-07-14 tracey }
669 a596b957 2022-07-14 tracey /* "lightweight" tag */
670 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
671 a596b957 2022-07-14 tracey if (error) {
672 a596b957 2022-07-14 tracey free(id);
673 a596b957 2022-07-14 tracey id = NULL;
674 a596b957 2022-07-14 tracey goto done;
675 a596b957 2022-07-14 tracey }
676 a596b957 2022-07-14 tracey new_repo_tag->tagger =
677 a596b957 2022-07-14 tracey strdup(got_object_commit_get_committer(commit));
678 a596b957 2022-07-14 tracey if (new_repo_tag->tagger == NULL) {
679 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
680 a596b957 2022-07-14 tracey goto err;
681 a596b957 2022-07-14 tracey }
682 a596b957 2022-07-14 tracey new_repo_tag->tagger_time =
683 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
684 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str, id);
685 a596b957 2022-07-14 tracey if (error)
686 a596b957 2022-07-14 tracey goto err;
687 a596b957 2022-07-14 tracey free(id);
688 a596b957 2022-07-14 tracey id = NULL;
689 a596b957 2022-07-14 tracey } else {
690 a596b957 2022-07-14 tracey free(id);
691 a596b957 2022-07-14 tracey id = NULL;
692 a596b957 2022-07-14 tracey new_repo_tag->tagger =
693 a596b957 2022-07-14 tracey strdup(got_object_tag_get_tagger(tag));
694 a596b957 2022-07-14 tracey if (new_repo_tag->tagger == NULL) {
695 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
696 a596b957 2022-07-14 tracey goto err;
697 a596b957 2022-07-14 tracey }
698 a596b957 2022-07-14 tracey new_repo_tag->tagger_time =
699 a596b957 2022-07-14 tracey got_object_tag_get_tagger_time(tag);
700 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str,
701 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag));
702 a596b957 2022-07-14 tracey if (error)
703 a596b957 2022-07-14 tracey goto err;
704 a596b957 2022-07-14 tracey }
705 a596b957 2022-07-14 tracey
706 a596b957 2022-07-14 tracey new_repo_tag->commit_id = strdup(id_str);
707 a596b957 2022-07-14 tracey if (new_repo_tag->commit_id == NULL)
708 a596b957 2022-07-14 tracey goto err;
709 a596b957 2022-07-14 tracey
710 a596b957 2022-07-14 tracey if (commit_found == 0 && qs->commit != NULL &&
711 a596b957 2022-07-14 tracey strncmp(id_str, qs->commit, strlen(id_str)) != 0)
712 a596b957 2022-07-14 tracey continue;
713 a596b957 2022-07-14 tracey else
714 a596b957 2022-07-14 tracey commit_found = 1;
715 a596b957 2022-07-14 tracey
716 a596b957 2022-07-14 tracey t->tag_count++;
717 a596b957 2022-07-14 tracey
718 a596b957 2022-07-14 tracey /*
719 a596b957 2022-07-14 tracey * check for one more commit before breaking,
720 a596b957 2022-07-14 tracey * so we know whether to navigate through briefs
721 a596b957 2022-07-14 tracey * commits and summary
722 a596b957 2022-07-14 tracey */
723 a596b957 2022-07-14 tracey if (chk_next) {
724 a596b957 2022-07-14 tracey t->next_id = strdup(new_repo_tag->commit_id);
725 a596b957 2022-07-14 tracey if (t->next_id == NULL) {
726 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
727 a596b957 2022-07-14 tracey goto err;
728 a596b957 2022-07-14 tracey }
729 a596b957 2022-07-14 tracey if (commit) {
730 a596b957 2022-07-14 tracey got_object_commit_close(commit);
731 a596b957 2022-07-14 tracey commit = NULL;
732 a596b957 2022-07-14 tracey }
733 a596b957 2022-07-14 tracey if (t->next_id == NULL) {
734 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
735 a596b957 2022-07-14 tracey goto err;
736 a596b957 2022-07-14 tracey }
737 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
738 a596b957 2022-07-14 tracey gotweb_free_repo_tag(new_repo_tag);
739 a596b957 2022-07-14 tracey goto done;
740 a596b957 2022-07-14 tracey }
741 a596b957 2022-07-14 tracey
742 a596b957 2022-07-14 tracey if (commit) {
743 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&new_repo_tag->
744 a596b957 2022-07-14 tracey tag_commit, commit);
745 a596b957 2022-07-14 tracey if (error)
746 a596b957 2022-07-14 tracey goto done;
747 a596b957 2022-07-14 tracey got_object_commit_close(commit);
748 a596b957 2022-07-14 tracey commit = NULL;
749 a596b957 2022-07-14 tracey } else {
750 a596b957 2022-07-14 tracey new_repo_tag->tag_commit =
751 a596b957 2022-07-14 tracey strdup(got_object_tag_get_message(tag));
752 a596b957 2022-07-14 tracey if (new_repo_tag->tag_commit == NULL) {
753 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
754 a596b957 2022-07-14 tracey goto done;
755 a596b957 2022-07-14 tracey }
756 a596b957 2022-07-14 tracey }
757 a596b957 2022-07-14 tracey
758 a596b957 2022-07-14 tracey while (*new_repo_tag->tag_commit == '\n')
759 a596b957 2022-07-14 tracey new_repo_tag->tag_commit++;
760 a596b957 2022-07-14 tracey
761 a596b957 2022-07-14 tracey if (qs->action != SUMMARY && qs->action != TAGS) {
762 a596b957 2022-07-14 tracey commit_msg = commit_msg0;
763 a596b957 2022-07-14 tracey while (*commit_msg == '\n')
764 a596b957 2022-07-14 tracey commit_msg++;
765 a596b957 2022-07-14 tracey
766 a596b957 2022-07-14 tracey new_repo_tag->commit_msg = strdup(commit_msg);
767 a596b957 2022-07-14 tracey if (new_repo_tag->commit_msg == NULL) {
768 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
769 a596b957 2022-07-14 tracey free(commit_msg0);
770 a596b957 2022-07-14 tracey goto err;
771 a596b957 2022-07-14 tracey }
772 a596b957 2022-07-14 tracey free(commit_msg0);
773 a596b957 2022-07-14 tracey }
774 a596b957 2022-07-14 tracey
775 a596b957 2022-07-14 tracey if (limit && --limit == 0) {
776 a596b957 2022-07-14 tracey if (chk_multi == 0)
777 a596b957 2022-07-14 tracey break;
778 a596b957 2022-07-14 tracey chk_next = 1;
779 a596b957 2022-07-14 tracey }
780 a596b957 2022-07-14 tracey free(id);
781 a596b957 2022-07-14 tracey id = NULL;
782 a596b957 2022-07-14 tracey }
783 a596b957 2022-07-14 tracey
784 a596b957 2022-07-14 tracey done:
785 a596b957 2022-07-14 tracey /*
786 a596b957 2022-07-14 tracey * we have tailq populated, so find previous commit id
787 a596b957 2022-07-14 tracey * for navigation through briefs and commits
788 a596b957 2022-07-14 tracey */
789 a596b957 2022-07-14 tracey if (t->tag_count == 0) {
790 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
791 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
792 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
793 a596b957 2022-07-14 tracey }
794 a596b957 2022-07-14 tracey }
795 a596b957 2022-07-14 tracey if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
796 a596b957 2022-07-14 tracey commit_found = 0;
797 a596b957 2022-07-14 tracey TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
798 a596b957 2022-07-14 tracey entry) {
799 a596b957 2022-07-14 tracey if (commit_found == 0 && rt->commit_id != NULL &&
800 a596b957 2022-07-14 tracey strcmp(qs->commit, rt->commit_id) != 0) {
801 a596b957 2022-07-14 tracey continue;
802 a596b957 2022-07-14 tracey } else
803 a596b957 2022-07-14 tracey commit_found = 1;
804 a596b957 2022-07-14 tracey if (c_cnt == srv->max_commits_display ||
805 a596b957 2022-07-14 tracey rt == TAILQ_FIRST(&t->repo_tags)) {
806 a596b957 2022-07-14 tracey t->prev_id = strdup(rt->commit_id);
807 a596b957 2022-07-14 tracey if (t->prev_id == NULL)
808 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
809 a596b957 2022-07-14 tracey break;
810 a596b957 2022-07-14 tracey }
811 a596b957 2022-07-14 tracey c_cnt++;
812 a596b957 2022-07-14 tracey }
813 a596b957 2022-07-14 tracey }
814 a596b957 2022-07-14 tracey err:
815 a596b957 2022-07-14 tracey if (commit)
816 a596b957 2022-07-14 tracey got_object_commit_close(commit);
817 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
818 a596b957 2022-07-14 tracey free(repo_path);
819 a596b957 2022-07-14 tracey free(id);
820 a596b957 2022-07-14 tracey return error;
821 a596b957 2022-07-14 tracey }
822 a596b957 2022-07-14 tracey
823 a596b957 2022-07-14 tracey const struct got_error *
824 a596b957 2022-07-14 tracey got_output_repo_tree(struct request *c)
825 a596b957 2022-07-14 tracey {
826 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
827 a596b957 2022-07-14 tracey struct transport *t = c->t;
828 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
829 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
830 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
831 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
832 a596b957 2022-07-14 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
833 a596b957 2022-07-14 tracey struct got_reflist_head refs;
834 a596b957 2022-07-14 tracey struct got_tree_object *tree = NULL;
835 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
836 a596b957 2022-07-14 tracey char *id_str = NULL;
837 a596b957 2022-07-14 tracey char *path = NULL, *in_repo_path = NULL, *build_folder = NULL;
838 a596b957 2022-07-14 tracey char *modestr = NULL, *name = NULL, *class = NULL;
839 a596b957 2022-07-14 tracey int nentries, i, class_flip = 0;
840 a596b957 2022-07-14 tracey
841 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
842 a596b957 2022-07-14 tracey
843 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
844 a596b957 2022-07-14 tracey
845 a596b957 2022-07-14 tracey if (qs->folder != NULL) {
846 a596b957 2022-07-14 tracey path = strdup(qs->folder);
847 a596b957 2022-07-14 tracey if (path == NULL) {
848 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
849 a596b957 2022-07-14 tracey goto done;
850 a596b957 2022-07-14 tracey }
851 a596b957 2022-07-14 tracey } else {
852 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, repo_dir->path);
853 a596b957 2022-07-14 tracey if (error)
854 a596b957 2022-07-14 tracey goto done;
855 a596b957 2022-07-14 tracey free(path);
856 a596b957 2022-07-14 tracey path = in_repo_path;
857 a596b957 2022-07-14 tracey }
858 a596b957 2022-07-14 tracey
859 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
860 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
861 a596b957 2022-07-14 tracey if (error)
862 a596b957 2022-07-14 tracey goto done;
863 a596b957 2022-07-14 tracey
864 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
865 a596b957 2022-07-14 tracey if (error)
866 a596b957 2022-07-14 tracey goto done;
867 a596b957 2022-07-14 tracey
868 a596b957 2022-07-14 tracey error = got_object_id_by_path(&tree_id, repo, commit, path);
869 a596b957 2022-07-14 tracey if (error)
870 a596b957 2022-07-14 tracey goto done;
871 a596b957 2022-07-14 tracey
872 a596b957 2022-07-14 tracey error = got_object_open_as_tree(&tree, repo, tree_id);
873 a596b957 2022-07-14 tracey if (error)
874 a596b957 2022-07-14 tracey goto done;
875 a596b957 2022-07-14 tracey
876 a596b957 2022-07-14 tracey nentries = got_object_tree_get_nentries(tree);
877 a596b957 2022-07-14 tracey
878 a596b957 2022-07-14 tracey for (i = 0; i < nentries; i++) {
879 a596b957 2022-07-14 tracey struct got_tree_entry *te;
880 a596b957 2022-07-14 tracey mode_t mode;
881 a596b957 2022-07-14 tracey
882 a596b957 2022-07-14 tracey te = got_object_tree_get_entry(tree, i);
883 a596b957 2022-07-14 tracey
884 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
885 a596b957 2022-07-14 tracey if (error)
886 a596b957 2022-07-14 tracey goto done;
887 a596b957 2022-07-14 tracey
888 a596b957 2022-07-14 tracey modestr = strdup("");
889 a596b957 2022-07-14 tracey if (modestr == NULL) {
890 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
891 a596b957 2022-07-14 tracey goto done;
892 a596b957 2022-07-14 tracey }
893 a596b957 2022-07-14 tracey mode = got_tree_entry_get_mode(te);
894 a596b957 2022-07-14 tracey if (got_object_tree_entry_is_submodule(te)) {
895 a596b957 2022-07-14 tracey free(modestr);
896 a596b957 2022-07-14 tracey modestr = strdup("$");
897 a596b957 2022-07-14 tracey if (modestr == NULL) {
898 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
899 a596b957 2022-07-14 tracey goto done;
900 a596b957 2022-07-14 tracey }
901 a596b957 2022-07-14 tracey } else if (S_ISLNK(mode)) {
902 a596b957 2022-07-14 tracey free(modestr);
903 a596b957 2022-07-14 tracey modestr = strdup("@");
904 a596b957 2022-07-14 tracey if (modestr == NULL) {
905 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
906 a596b957 2022-07-14 tracey goto done;
907 a596b957 2022-07-14 tracey }
908 a596b957 2022-07-14 tracey } else if (S_ISDIR(mode)) {
909 a596b957 2022-07-14 tracey free(modestr);
910 a596b957 2022-07-14 tracey modestr = strdup("/");
911 a596b957 2022-07-14 tracey if (modestr == NULL) {
912 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
913 a596b957 2022-07-14 tracey goto done;
914 a596b957 2022-07-14 tracey }
915 a596b957 2022-07-14 tracey } else if (mode & S_IXUSR) {
916 a596b957 2022-07-14 tracey free(modestr);
917 a596b957 2022-07-14 tracey modestr = strdup("*");
918 a596b957 2022-07-14 tracey if (modestr == NULL) {
919 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
920 a596b957 2022-07-14 tracey goto done;
921 a596b957 2022-07-14 tracey }
922 a596b957 2022-07-14 tracey }
923 a596b957 2022-07-14 tracey
924 a596b957 2022-07-14 tracey if (class_flip == 0) {
925 a596b957 2022-07-14 tracey class = strdup("back_lightgray");
926 a596b957 2022-07-14 tracey if (class == NULL) {
927 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
928 a596b957 2022-07-14 tracey goto done;
929 a596b957 2022-07-14 tracey }
930 a596b957 2022-07-14 tracey class_flip = 1;
931 a596b957 2022-07-14 tracey } else {
932 a596b957 2022-07-14 tracey class = strdup("back_white");
933 a596b957 2022-07-14 tracey if (class == NULL) {
934 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
935 a596b957 2022-07-14 tracey goto done;
936 a596b957 2022-07-14 tracey }
937 a596b957 2022-07-14 tracey class_flip = 0;
938 a596b957 2022-07-14 tracey }
939 a596b957 2022-07-14 tracey
940 a596b957 2022-07-14 tracey name = strdup(got_tree_entry_get_name(te));
941 a596b957 2022-07-14 tracey if (name == NULL) {
942 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
943 a596b957 2022-07-14 tracey goto done;
944 a596b957 2022-07-14 tracey }
945 a596b957 2022-07-14 tracey if (S_ISDIR(mode)) {
946 a596b957 2022-07-14 tracey if (asprintf(&build_folder, "%s/%s",
947 a596b957 2022-07-14 tracey qs->folder ? qs->folder : "",
948 a596b957 2022-07-14 tracey got_tree_entry_get_name(te)) == -1) {
949 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
950 a596b957 2022-07-14 tracey goto done;
951 a596b957 2022-07-14 tracey }
952 a596b957 2022-07-14 tracey
953 a596b957 2022-07-14 tracey if (fcgi_gen_response(c,
954 a596b957 2022-07-14 tracey "<div id='tree_wrapper'>\n") == -1)
955 a596b957 2022-07-14 tracey goto done;
956 a596b957 2022-07-14 tracey
957 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<div id='tree_line' "
958 a596b957 2022-07-14 tracey "class='") == -1)
959 a596b957 2022-07-14 tracey goto done;
960 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, class) == -1)
961 a596b957 2022-07-14 tracey goto done;
962 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "'>") == -1)
963 a596b957 2022-07-14 tracey goto done;
964 a596b957 2022-07-14 tracey
965 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<a class='diff_directory' "
966 a596b957 2022-07-14 tracey "href='?index_page=") == -1)
967 a596b957 2022-07-14 tracey goto done;
968 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->index_page_str) == -1)
969 a596b957 2022-07-14 tracey goto done;
970 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&path=") == -1)
971 a596b957 2022-07-14 tracey goto done;
972 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->path) == -1)
973 a596b957 2022-07-14 tracey goto done;
974 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&action=tree") == -1)
975 a596b957 2022-07-14 tracey goto done;
976 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&commit=") == -1)
977 a596b957 2022-07-14 tracey goto done;
978 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, rc->commit_id) == -1)
979 a596b957 2022-07-14 tracey goto done;
980 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&folder=") == -1)
981 a596b957 2022-07-14 tracey goto done;
982 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, build_folder) == -1)
983 a596b957 2022-07-14 tracey goto done;
984 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "'>") == -1)
985 a596b957 2022-07-14 tracey goto done;
986 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, name) == -1)
987 a596b957 2022-07-14 tracey goto done;
988 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, modestr) == -1)
989 a596b957 2022-07-14 tracey goto done;
990 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</a>") == -1)
991 a596b957 2022-07-14 tracey goto done;
992 a596b957 2022-07-14 tracey
993 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>\n") == -1)
994 a596b957 2022-07-14 tracey goto done;
995 a596b957 2022-07-14 tracey
996 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<div id='tree_line_blank' "
997 a596b957 2022-07-14 tracey "class='") == -1)
998 a596b957 2022-07-14 tracey goto done;
999 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, class) == -1)
1000 a596b957 2022-07-14 tracey goto done;
1001 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "'>") == -1)
1002 a596b957 2022-07-14 tracey goto done;
1003 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&nbsp;") == -1)
1004 a596b957 2022-07-14 tracey goto done;
1005 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>\n") == -1)
1006 a596b957 2022-07-14 tracey goto done;
1007 a596b957 2022-07-14 tracey
1008 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>\n") == -1)
1009 a596b957 2022-07-14 tracey goto done;
1010 a596b957 2022-07-14 tracey
1011 a596b957 2022-07-14 tracey } else {
1012 a596b957 2022-07-14 tracey free(name);
1013 a596b957 2022-07-14 tracey name = strdup(got_tree_entry_get_name(te));
1014 a596b957 2022-07-14 tracey if (name == NULL) {
1015 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1016 a596b957 2022-07-14 tracey goto done;
1017 a596b957 2022-07-14 tracey }
1018 a596b957 2022-07-14 tracey
1019 a596b957 2022-07-14 tracey if (fcgi_gen_response(c,
1020 a596b957 2022-07-14 tracey "<div id='tree_wrapper'>\n") == -1)
1021 a596b957 2022-07-14 tracey goto done;
1022 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<div id='tree_line' "
1023 a596b957 2022-07-14 tracey "class='") == -1)
1024 a596b957 2022-07-14 tracey goto done;
1025 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, class) == -1)
1026 a596b957 2022-07-14 tracey goto done;
1027 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "'>") == -1)
1028 a596b957 2022-07-14 tracey goto done;
1029 a596b957 2022-07-14 tracey
1030 a596b957 2022-07-14 tracey if (fcgi_gen_response(c,
1031 a596b957 2022-07-14 tracey "<a href='?index_page=") == -1)
1032 a596b957 2022-07-14 tracey goto done;
1033 a596b957 2022-07-14 tracey
1034 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->index_page_str) == -1)
1035 a596b957 2022-07-14 tracey goto done;
1036 a596b957 2022-07-14 tracey
1037 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&path=") == -1)
1038 a596b957 2022-07-14 tracey goto done;
1039 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->path) == -1)
1040 a596b957 2022-07-14 tracey goto done;
1041 a596b957 2022-07-14 tracey
1042 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&action=blob") == -1)
1043 a596b957 2022-07-14 tracey goto done;
1044 a596b957 2022-07-14 tracey
1045 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&commit=") == -1)
1046 a596b957 2022-07-14 tracey goto done;
1047 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, rc->commit_id) == -1)
1048 a596b957 2022-07-14 tracey goto done;
1049 a596b957 2022-07-14 tracey
1050 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&folder=") == -1)
1051 a596b957 2022-07-14 tracey goto done;
1052 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->folder) == -1)
1053 a596b957 2022-07-14 tracey goto done;
1054 a596b957 2022-07-14 tracey
1055 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&file=") == -1)
1056 a596b957 2022-07-14 tracey goto done;
1057 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, name) == -1)
1058 a596b957 2022-07-14 tracey goto done;
1059 a596b957 2022-07-14 tracey
1060 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "'>") == -1)
1061 a596b957 2022-07-14 tracey goto done;
1062 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, name) == -1)
1063 a596b957 2022-07-14 tracey goto done;
1064 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, modestr) == -1)
1065 a596b957 2022-07-14 tracey goto done;
1066 a596b957 2022-07-14 tracey
1067 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</a>") == -1)
1068 a596b957 2022-07-14 tracey goto done;
1069 a596b957 2022-07-14 tracey
1070 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>\n") == -1)
1071 a596b957 2022-07-14 tracey goto done;
1072 a596b957 2022-07-14 tracey
1073 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<div id='tree_line_blank' "
1074 a596b957 2022-07-14 tracey "class='") == -1)
1075 a596b957 2022-07-14 tracey goto done;
1076 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, class) == -1)
1077 a596b957 2022-07-14 tracey goto done;
1078 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "'>") == -1)
1079 a596b957 2022-07-14 tracey goto done;
1080 a596b957 2022-07-14 tracey
1081 a596b957 2022-07-14 tracey if (fcgi_gen_response(c,
1082 a596b957 2022-07-14 tracey "<a href='?index_page=") == -1)
1083 a596b957 2022-07-14 tracey goto done;
1084 a596b957 2022-07-14 tracey
1085 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->index_page_str) == -1)
1086 a596b957 2022-07-14 tracey goto done;
1087 a596b957 2022-07-14 tracey
1088 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&path=") == -1)
1089 a596b957 2022-07-14 tracey goto done;
1090 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->path) == -1)
1091 a596b957 2022-07-14 tracey goto done;
1092 a596b957 2022-07-14 tracey
1093 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&action=commits") == -1)
1094 a596b957 2022-07-14 tracey goto done;
1095 a596b957 2022-07-14 tracey
1096 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&commit=") == -1)
1097 a596b957 2022-07-14 tracey goto done;
1098 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, rc->commit_id) == -1)
1099 a596b957 2022-07-14 tracey goto done;
1100 a596b957 2022-07-14 tracey
1101 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&folder=") == -1)
1102 a596b957 2022-07-14 tracey goto done;
1103 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->folder) == -1)
1104 a596b957 2022-07-14 tracey goto done;
1105 a596b957 2022-07-14 tracey
1106 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&file=") == -1)
1107 a596b957 2022-07-14 tracey goto done;
1108 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, name) == -1)
1109 a596b957 2022-07-14 tracey goto done;
1110 a596b957 2022-07-14 tracey
1111 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "'>") == -1)
1112 a596b957 2022-07-14 tracey goto done;
1113 a596b957 2022-07-14 tracey
1114 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "commits") == -1)
1115 a596b957 2022-07-14 tracey goto done;
1116 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</a>\n") == -1)
1117 a596b957 2022-07-14 tracey goto done;
1118 a596b957 2022-07-14 tracey
1119 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, " | \n") == -1)
1120 a596b957 2022-07-14 tracey goto done;
1121 a596b957 2022-07-14 tracey
1122 a596b957 2022-07-14 tracey if (fcgi_gen_response(c,
1123 a596b957 2022-07-14 tracey "<a href='?index_page=") == -1)
1124 a596b957 2022-07-14 tracey goto done;
1125 a596b957 2022-07-14 tracey
1126 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->index_page_str) == -1)
1127 a596b957 2022-07-14 tracey goto done;
1128 a596b957 2022-07-14 tracey
1129 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&path=") == -1)
1130 a596b957 2022-07-14 tracey goto done;
1131 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->path) == -1)
1132 a596b957 2022-07-14 tracey goto done;
1133 a596b957 2022-07-14 tracey
1134 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&action=blame") == -1)
1135 a596b957 2022-07-14 tracey goto done;
1136 a596b957 2022-07-14 tracey
1137 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&commit=") == -1)
1138 a596b957 2022-07-14 tracey goto done;
1139 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, rc->commit_id) == -1)
1140 a596b957 2022-07-14 tracey goto done;
1141 a596b957 2022-07-14 tracey
1142 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&folder=") == -1)
1143 a596b957 2022-07-14 tracey goto done;
1144 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->folder) == -1)
1145 a596b957 2022-07-14 tracey goto done;
1146 a596b957 2022-07-14 tracey
1147 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&file=") == -1)
1148 a596b957 2022-07-14 tracey goto done;
1149 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, name) == -1)
1150 a596b957 2022-07-14 tracey goto done;
1151 a596b957 2022-07-14 tracey
1152 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "'>") == -1)
1153 a596b957 2022-07-14 tracey goto done;
1154 a596b957 2022-07-14 tracey
1155 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "blame") == -1)
1156 a596b957 2022-07-14 tracey goto done;
1157 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</a>\n") == -1)
1158 a596b957 2022-07-14 tracey goto done;
1159 a596b957 2022-07-14 tracey
1160 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>\n") == -1)
1161 a596b957 2022-07-14 tracey goto done;
1162 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>\n") == -1)
1163 a596b957 2022-07-14 tracey goto done;
1164 a596b957 2022-07-14 tracey }
1165 a596b957 2022-07-14 tracey free(id_str);
1166 a596b957 2022-07-14 tracey id_str = NULL;
1167 a596b957 2022-07-14 tracey free(build_folder);
1168 a596b957 2022-07-14 tracey build_folder = NULL;
1169 a596b957 2022-07-14 tracey free(name);
1170 a596b957 2022-07-14 tracey name = NULL;
1171 a596b957 2022-07-14 tracey free(modestr);
1172 a596b957 2022-07-14 tracey modestr = NULL;
1173 a596b957 2022-07-14 tracey free(class);
1174 a596b957 2022-07-14 tracey class = NULL;
1175 a596b957 2022-07-14 tracey }
1176 a596b957 2022-07-14 tracey done:
1177 a596b957 2022-07-14 tracey free(id_str);
1178 a596b957 2022-07-14 tracey free(build_folder);
1179 a596b957 2022-07-14 tracey free(modestr);
1180 a596b957 2022-07-14 tracey free(path);
1181 a596b957 2022-07-14 tracey free(name);
1182 a596b957 2022-07-14 tracey free(class);
1183 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
1184 a596b957 2022-07-14 tracey if (commit)
1185 a596b957 2022-07-14 tracey got_object_commit_close(commit);
1186 a596b957 2022-07-14 tracey free(commit_id);
1187 a596b957 2022-07-14 tracey free(tree_id);
1188 a596b957 2022-07-14 tracey return error;
1189 a596b957 2022-07-14 tracey }
1190 a596b957 2022-07-14 tracey
1191 a596b957 2022-07-14 tracey const struct got_error *
1192 a596b957 2022-07-14 tracey got_output_file_blob(struct request *c)
1193 a596b957 2022-07-14 tracey {
1194 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1195 a596b957 2022-07-14 tracey struct transport *t = c->t;
1196 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1197 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
1198 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
1199 a596b957 2022-07-14 tracey struct got_object_id *commit_id = NULL;
1200 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1201 a596b957 2022-07-14 tracey struct got_blob_object *blob = NULL;
1202 a596b957 2022-07-14 tracey char *path = NULL, *in_repo_path = NULL;
1203 a596b957 2022-07-14 tracey int obj_type, set_mime = 0, type = 0, fd = -1;
1204 a596b957 2022-07-14 tracey char *buf_output = NULL;
1205 a596b957 2022-07-14 tracey size_t len, hdrlen;
1206 a596b957 2022-07-14 tracey const uint8_t *buf;
1207 a596b957 2022-07-14 tracey
1208 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1209 a596b957 2022-07-14 tracey
1210 a596b957 2022-07-14 tracey if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1211 a596b957 2022-07-14 tracey qs->folder ? "/" : "", qs->file) == -1) {
1212 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1213 a596b957 2022-07-14 tracey goto done;
1214 a596b957 2022-07-14 tracey }
1215 a596b957 2022-07-14 tracey
1216 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, path);
1217 a596b957 2022-07-14 tracey if (error)
1218 a596b957 2022-07-14 tracey goto done;
1219 a596b957 2022-07-14 tracey
1220 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1221 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
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 error = got_object_open_as_commit(&commit, repo, commit_id);
1226 a596b957 2022-07-14 tracey if (error)
1227 a596b957 2022-07-14 tracey goto done;
1228 a596b957 2022-07-14 tracey
1229 a596b957 2022-07-14 tracey error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1230 a596b957 2022-07-14 tracey if (error)
1231 a596b957 2022-07-14 tracey goto done;
1232 a596b957 2022-07-14 tracey
1233 a596b957 2022-07-14 tracey if (commit_id == NULL) {
1234 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_NO_OBJ);
1235 a596b957 2022-07-14 tracey goto done;
1236 a596b957 2022-07-14 tracey }
1237 a596b957 2022-07-14 tracey
1238 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, commit_id);
1239 a596b957 2022-07-14 tracey if (error)
1240 a596b957 2022-07-14 tracey goto done;
1241 a596b957 2022-07-14 tracey
1242 a596b957 2022-07-14 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
1243 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1244 a596b957 2022-07-14 tracey goto done;
1245 a596b957 2022-07-14 tracey }
1246 a596b957 2022-07-14 tracey
1247 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1248 a596b957 2022-07-14 tracey if (error)
1249 a596b957 2022-07-14 tracey goto done;
1250 a596b957 2022-07-14 tracey
1251 a596b957 2022-07-14 tracey error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1252 a596b957 2022-07-14 tracey if (error)
1253 a596b957 2022-07-14 tracey goto done;
1254 a596b957 2022-07-14 tracey hdrlen = got_object_blob_get_hdrlen(blob);
1255 a596b957 2022-07-14 tracey do {
1256 a596b957 2022-07-14 tracey error = got_object_blob_read_block(&len, blob);
1257 a596b957 2022-07-14 tracey if (error)
1258 a596b957 2022-07-14 tracey goto done;
1259 a596b957 2022-07-14 tracey buf = got_object_blob_get_read_buf(blob);
1260 a596b957 2022-07-14 tracey
1261 a596b957 2022-07-14 tracey /*
1262 a596b957 2022-07-14 tracey * Skip blob object header first time around,
1263 a596b957 2022-07-14 tracey * which also contains a zero byte.
1264 a596b957 2022-07-14 tracey */
1265 a596b957 2022-07-14 tracey buf += hdrlen;
1266 a596b957 2022-07-14 tracey if (set_mime == 0) {
1267 a596b957 2022-07-14 tracey if (isbinary(buf, len - hdrlen)) {
1268 a596b957 2022-07-14 tracey error = gotweb_render_content_type_file(c,
1269 a596b957 2022-07-14 tracey "application/octet-stream",
1270 a596b957 2022-07-14 tracey qs->file);
1271 a596b957 2022-07-14 tracey if (error) {
1272 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__,
1273 a596b957 2022-07-14 tracey error->msg);
1274 a596b957 2022-07-14 tracey goto done;
1275 a596b957 2022-07-14 tracey }
1276 a596b957 2022-07-14 tracey type = 0;
1277 a596b957 2022-07-14 tracey } else {
1278 a596b957 2022-07-14 tracey error = gotweb_render_content_type(c,
1279 a596b957 2022-07-14 tracey "text/text");
1280 a596b957 2022-07-14 tracey if (error) {
1281 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__,
1282 a596b957 2022-07-14 tracey error->msg);
1283 a596b957 2022-07-14 tracey goto done;
1284 a596b957 2022-07-14 tracey }
1285 a596b957 2022-07-14 tracey type = 1;
1286 a596b957 2022-07-14 tracey }
1287 a596b957 2022-07-14 tracey }
1288 a596b957 2022-07-14 tracey set_mime = 1;
1289 a596b957 2022-07-14 tracey if (type) {
1290 a596b957 2022-07-14 tracey buf_output = calloc(len - hdrlen + 1,
1291 a596b957 2022-07-14 tracey sizeof(*buf_output));
1292 a596b957 2022-07-14 tracey if (buf_output == NULL) {
1293 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1294 a596b957 2022-07-14 tracey goto done;
1295 a596b957 2022-07-14 tracey }
1296 a596b957 2022-07-14 tracey memcpy(buf_output, buf, len - hdrlen);
1297 a596b957 2022-07-14 tracey fcgi_gen_response(c, buf_output);
1298 a596b957 2022-07-14 tracey free(buf_output);
1299 a596b957 2022-07-14 tracey buf_output = NULL;
1300 a596b957 2022-07-14 tracey } else
1301 a596b957 2022-07-14 tracey fcgi_gen_binary_response(c, buf, len - hdrlen);
1302 a596b957 2022-07-14 tracey
1303 a596b957 2022-07-14 tracey hdrlen = 0;
1304 a596b957 2022-07-14 tracey } while (len != 0);
1305 a596b957 2022-07-14 tracey done:
1306 a596b957 2022-07-14 tracey if (commit)
1307 a596b957 2022-07-14 tracey got_object_commit_close(commit);
1308 a596b957 2022-07-14 tracey if (fd != -1 && close(fd) == -1 && error == NULL)
1309 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1310 a596b957 2022-07-14 tracey if (blob)
1311 a596b957 2022-07-14 tracey got_object_blob_close(blob);
1312 a596b957 2022-07-14 tracey free(buf_output);
1313 a596b957 2022-07-14 tracey free(in_repo_path);
1314 a596b957 2022-07-14 tracey free(commit_id);
1315 a596b957 2022-07-14 tracey free(path);
1316 a596b957 2022-07-14 tracey return error;
1317 a596b957 2022-07-14 tracey }
1318 a596b957 2022-07-14 tracey
1319 a596b957 2022-07-14 tracey struct blame_line {
1320 a596b957 2022-07-14 tracey int annotated;
1321 a596b957 2022-07-14 tracey char *id_str;
1322 a596b957 2022-07-14 tracey char *committer;
1323 a596b957 2022-07-14 tracey char datebuf[11]; /* YYYY-MM-DD + NUL */
1324 a596b957 2022-07-14 tracey };
1325 a596b957 2022-07-14 tracey
1326 a596b957 2022-07-14 tracey struct blame_cb_args {
1327 a596b957 2022-07-14 tracey struct blame_line *lines;
1328 a596b957 2022-07-14 tracey int nlines;
1329 a596b957 2022-07-14 tracey int nlines_prec;
1330 a596b957 2022-07-14 tracey int lineno_cur;
1331 a596b957 2022-07-14 tracey off_t *line_offsets;
1332 a596b957 2022-07-14 tracey FILE *f;
1333 a596b957 2022-07-14 tracey struct got_repository *repo;
1334 a596b957 2022-07-14 tracey struct request *c;
1335 a596b957 2022-07-14 tracey };
1336 a596b957 2022-07-14 tracey
1337 a596b957 2022-07-14 tracey static const struct got_error *
1338 a596b957 2022-07-14 tracey got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1339 a596b957 2022-07-14 tracey struct got_commit_object *commit, struct got_object_id *id)
1340 a596b957 2022-07-14 tracey {
1341 a596b957 2022-07-14 tracey const struct got_error *err = NULL;
1342 a596b957 2022-07-14 tracey struct blame_cb_args *a = arg;
1343 a596b957 2022-07-14 tracey struct blame_line *bline;
1344 a596b957 2022-07-14 tracey struct request *c = a->c;
1345 a596b957 2022-07-14 tracey struct transport *t = c->t;
1346 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
1347 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
1348 a596b957 2022-07-14 tracey char *line = NULL, *eline = NULL;
1349 a596b957 2022-07-14 tracey size_t linesize = 0;
1350 a596b957 2022-07-14 tracey off_t offset;
1351 a596b957 2022-07-14 tracey struct tm tm;
1352 a596b957 2022-07-14 tracey time_t committer_time;
1353 a596b957 2022-07-14 tracey
1354 a596b957 2022-07-14 tracey if (nlines != a->nlines ||
1355 a596b957 2022-07-14 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
1356 a596b957 2022-07-14 tracey return got_error(GOT_ERR_RANGE);
1357 a596b957 2022-07-14 tracey
1358 a596b957 2022-07-14 tracey if (lineno == -1)
1359 a596b957 2022-07-14 tracey return NULL; /* no change in this commit */
1360 a596b957 2022-07-14 tracey
1361 a596b957 2022-07-14 tracey /* Annotate this line. */
1362 a596b957 2022-07-14 tracey bline = &a->lines[lineno - 1];
1363 a596b957 2022-07-14 tracey if (bline->annotated)
1364 a596b957 2022-07-14 tracey return NULL;
1365 a596b957 2022-07-14 tracey err = got_object_id_str(&bline->id_str, id);
1366 a596b957 2022-07-14 tracey if (err)
1367 a596b957 2022-07-14 tracey return err;
1368 a596b957 2022-07-14 tracey
1369 a596b957 2022-07-14 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
1370 a596b957 2022-07-14 tracey if (bline->committer == NULL) {
1371 a596b957 2022-07-14 tracey err = got_error_from_errno("strdup");
1372 a596b957 2022-07-14 tracey goto done;
1373 a596b957 2022-07-14 tracey }
1374 a596b957 2022-07-14 tracey
1375 a596b957 2022-07-14 tracey committer_time = got_object_commit_get_committer_time(commit);
1376 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1377 a596b957 2022-07-14 tracey return got_error_from_errno("gmtime_r");
1378 a596b957 2022-07-14 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1379 a596b957 2022-07-14 tracey &tm) == 0) {
1380 a596b957 2022-07-14 tracey err = got_error(GOT_ERR_NO_SPACE);
1381 a596b957 2022-07-14 tracey goto done;
1382 a596b957 2022-07-14 tracey }
1383 a596b957 2022-07-14 tracey bline->annotated = 1;
1384 a596b957 2022-07-14 tracey
1385 a596b957 2022-07-14 tracey /* Print lines annotated so far. */
1386 a596b957 2022-07-14 tracey bline = &a->lines[a->lineno_cur - 1];
1387 a596b957 2022-07-14 tracey if (!bline->annotated)
1388 a596b957 2022-07-14 tracey goto done;
1389 a596b957 2022-07-14 tracey
1390 a596b957 2022-07-14 tracey offset = a->line_offsets[a->lineno_cur - 1];
1391 a596b957 2022-07-14 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
1392 a596b957 2022-07-14 tracey err = got_error_from_errno("fseeko");
1393 a596b957 2022-07-14 tracey goto done;
1394 a596b957 2022-07-14 tracey }
1395 a596b957 2022-07-14 tracey
1396 a596b957 2022-07-14 tracey while (bline->annotated) {
1397 a596b957 2022-07-14 tracey int out_buff_size = 100;
1398 a596b957 2022-07-14 tracey char *smallerthan, *at, *nl, *committer;
1399 a596b957 2022-07-14 tracey char out_buff[out_buff_size];
1400 a596b957 2022-07-14 tracey size_t len;
1401 a596b957 2022-07-14 tracey
1402 a596b957 2022-07-14 tracey if (getline(&line, &linesize, a->f) == -1) {
1403 a596b957 2022-07-14 tracey if (ferror(a->f))
1404 a596b957 2022-07-14 tracey err = got_error_from_errno("getline");
1405 a596b957 2022-07-14 tracey break;
1406 a596b957 2022-07-14 tracey }
1407 a596b957 2022-07-14 tracey
1408 a596b957 2022-07-14 tracey committer = bline->committer;
1409 a596b957 2022-07-14 tracey smallerthan = strchr(committer, '<');
1410 a596b957 2022-07-14 tracey if (smallerthan && smallerthan[1] != '\0')
1411 a596b957 2022-07-14 tracey committer = smallerthan + 1;
1412 a596b957 2022-07-14 tracey at = strchr(committer, '@');
1413 a596b957 2022-07-14 tracey if (at)
1414 a596b957 2022-07-14 tracey *at = '\0';
1415 a596b957 2022-07-14 tracey len = strlen(committer);
1416 a596b957 2022-07-14 tracey if (len >= 9)
1417 a596b957 2022-07-14 tracey committer[8] = '\0';
1418 a596b957 2022-07-14 tracey
1419 a596b957 2022-07-14 tracey nl = strchr(line, '\n');
1420 a596b957 2022-07-14 tracey if (nl)
1421 a596b957 2022-07-14 tracey *nl = '\0';
1422 a596b957 2022-07-14 tracey
1423 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<div id='blame_wrapper'>") == -1)
1424 a596b957 2022-07-14 tracey goto done;
1425 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<div id='blame_number'>") == -1)
1426 a596b957 2022-07-14 tracey goto done;
1427 a596b957 2022-07-14 tracey if (snprintf(out_buff, strlen(out_buff), "%.*d", a->nlines_prec,
1428 a596b957 2022-07-14 tracey a->lineno_cur) < 0)
1429 a596b957 2022-07-14 tracey goto done;
1430 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, out_buff) == -1)
1431 a596b957 2022-07-14 tracey goto done;
1432 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>") == -1)
1433 a596b957 2022-07-14 tracey goto done;
1434 a596b957 2022-07-14 tracey
1435 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<div id='blame_hash'>") == -1)
1436 a596b957 2022-07-14 tracey goto done;
1437 a596b957 2022-07-14 tracey
1438 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<a href='?index_page=") == -1)
1439 a596b957 2022-07-14 tracey goto done;
1440 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, qs->index_page_str) == -1)
1441 a596b957 2022-07-14 tracey goto done;
1442 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&path=") == -1)
1443 a596b957 2022-07-14 tracey goto done;
1444 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, repo_dir->name) == -1)
1445 a596b957 2022-07-14 tracey goto done;
1446 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "&action=diff&commit=") == -1)
1447 a596b957 2022-07-14 tracey goto done;
1448 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, bline->id_str) == -1)
1449 a596b957 2022-07-14 tracey goto done;
1450 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "'>") == -1)
1451 a596b957 2022-07-14 tracey goto done;
1452 a596b957 2022-07-14 tracey if (snprintf(out_buff, 10, "%.8s", bline->id_str) < 0)
1453 a596b957 2022-07-14 tracey goto done;
1454 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, out_buff) == -1)
1455 a596b957 2022-07-14 tracey goto done;
1456 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</a></div>") == -1)
1457 a596b957 2022-07-14 tracey goto done;
1458 a596b957 2022-07-14 tracey
1459 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<div id='blame_date'>") == -1)
1460 a596b957 2022-07-14 tracey goto done;
1461 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, bline->datebuf) == -1)
1462 a596b957 2022-07-14 tracey goto done;
1463 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>") == -1)
1464 a596b957 2022-07-14 tracey goto done;
1465 a596b957 2022-07-14 tracey
1466 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<div id='blame_author'>") == -1)
1467 a596b957 2022-07-14 tracey goto done;
1468 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, committer) == -1)
1469 a596b957 2022-07-14 tracey goto done;
1470 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>") == -1)
1471 a596b957 2022-07-14 tracey goto done;
1472 a596b957 2022-07-14 tracey
1473 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<div id='blame_code'>") == -1)
1474 a596b957 2022-07-14 tracey goto done;
1475 a596b957 2022-07-14 tracey err = gotweb_escape_html(&eline, line);
1476 a596b957 2022-07-14 tracey if (err)
1477 a596b957 2022-07-14 tracey goto done;
1478 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, eline) == -1)
1479 a596b957 2022-07-14 tracey goto done;
1480 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>") == -1)
1481 a596b957 2022-07-14 tracey goto done;
1482 a596b957 2022-07-14 tracey
1483 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>") == -1)
1484 a596b957 2022-07-14 tracey goto done;
1485 a596b957 2022-07-14 tracey a->lineno_cur++;
1486 a596b957 2022-07-14 tracey bline = &a->lines[a->lineno_cur - 1];
1487 a596b957 2022-07-14 tracey }
1488 a596b957 2022-07-14 tracey done:
1489 a596b957 2022-07-14 tracey free(line);
1490 a596b957 2022-07-14 tracey free(eline);
1491 a596b957 2022-07-14 tracey return err;
1492 a596b957 2022-07-14 tracey }
1493 a596b957 2022-07-14 tracey
1494 a596b957 2022-07-14 tracey const struct got_error *
1495 a596b957 2022-07-14 tracey got_output_file_blame(struct request *c)
1496 a596b957 2022-07-14 tracey {
1497 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1498 a596b957 2022-07-14 tracey struct transport *t = c->t;
1499 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1500 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
1501 a596b957 2022-07-14 tracey struct got_object_id *obj_id = NULL, *commit_id = NULL;
1502 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
1503 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1504 a596b957 2022-07-14 tracey struct got_blob_object *blob = NULL;
1505 a596b957 2022-07-14 tracey char *path = NULL, *in_repo_path = NULL;
1506 a596b957 2022-07-14 tracey struct blame_cb_args bca;
1507 a596b957 2022-07-14 tracey int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1508 a596b957 2022-07-14 tracey int fd6 = -1;
1509 a596b957 2022-07-14 tracey off_t filesize;
1510 a596b957 2022-07-14 tracey FILE *f1 = NULL, *f2 = NULL;
1511 a596b957 2022-07-14 tracey
1512 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1513 a596b957 2022-07-14 tracey bca.f = NULL;
1514 a596b957 2022-07-14 tracey bca.lines = NULL;
1515 a596b957 2022-07-14 tracey
1516 a596b957 2022-07-14 tracey if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1517 a596b957 2022-07-14 tracey qs->folder ? "/" : "", qs->file) == -1) {
1518 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1519 a596b957 2022-07-14 tracey goto done;
1520 a596b957 2022-07-14 tracey }
1521 a596b957 2022-07-14 tracey
1522 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, path);
1523 a596b957 2022-07-14 tracey if (error)
1524 a596b957 2022-07-14 tracey goto done;
1525 a596b957 2022-07-14 tracey
1526 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1527 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
1528 a596b957 2022-07-14 tracey if (error)
1529 a596b957 2022-07-14 tracey goto done;
1530 a596b957 2022-07-14 tracey
1531 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
1532 a596b957 2022-07-14 tracey if (error)
1533 a596b957 2022-07-14 tracey goto done;
1534 a596b957 2022-07-14 tracey
1535 a596b957 2022-07-14 tracey error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1536 a596b957 2022-07-14 tracey if (error)
1537 a596b957 2022-07-14 tracey goto done;
1538 a596b957 2022-07-14 tracey
1539 a596b957 2022-07-14 tracey if (commit_id == NULL) {
1540 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_NO_OBJ);
1541 a596b957 2022-07-14 tracey goto done;
1542 a596b957 2022-07-14 tracey }
1543 a596b957 2022-07-14 tracey
1544 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, obj_id);
1545 a596b957 2022-07-14 tracey if (error)
1546 a596b957 2022-07-14 tracey goto done;
1547 a596b957 2022-07-14 tracey
1548 a596b957 2022-07-14 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
1549 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1550 a596b957 2022-07-14 tracey goto done;
1551 a596b957 2022-07-14 tracey }
1552 a596b957 2022-07-14 tracey
1553 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1554 a596b957 2022-07-14 tracey if (error)
1555 a596b957 2022-07-14 tracey goto done;
1556 a596b957 2022-07-14 tracey
1557 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1558 a596b957 2022-07-14 tracey if (error)
1559 a596b957 2022-07-14 tracey goto done;
1560 a596b957 2022-07-14 tracey
1561 a596b957 2022-07-14 tracey error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1562 a596b957 2022-07-14 tracey if (error)
1563 a596b957 2022-07-14 tracey goto done;
1564 a596b957 2022-07-14 tracey
1565 a596b957 2022-07-14 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1566 a596b957 2022-07-14 tracey &bca.line_offsets, bca.f, blob);
1567 a596b957 2022-07-14 tracey if (error || bca.nlines == 0)
1568 a596b957 2022-07-14 tracey goto done;
1569 a596b957 2022-07-14 tracey
1570 a596b957 2022-07-14 tracey /* Don't include \n at EOF in the blame line count. */
1571 a596b957 2022-07-14 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
1572 a596b957 2022-07-14 tracey bca.nlines--;
1573 a596b957 2022-07-14 tracey
1574 a596b957 2022-07-14 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1575 a596b957 2022-07-14 tracey if (bca.lines == NULL) {
1576 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1577 a596b957 2022-07-14 tracey goto done;
1578 a596b957 2022-07-14 tracey }
1579 a596b957 2022-07-14 tracey bca.lineno_cur = 1;
1580 a596b957 2022-07-14 tracey bca.nlines_prec = 0;
1581 a596b957 2022-07-14 tracey i = bca.nlines;
1582 a596b957 2022-07-14 tracey while (i > 0) {
1583 a596b957 2022-07-14 tracey i /= 10;
1584 a596b957 2022-07-14 tracey bca.nlines_prec++;
1585 a596b957 2022-07-14 tracey }
1586 a596b957 2022-07-14 tracey bca.repo = repo;
1587 a596b957 2022-07-14 tracey bca.c = c;
1588 a596b957 2022-07-14 tracey
1589 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1590 a596b957 2022-07-14 tracey if (error)
1591 a596b957 2022-07-14 tracey goto done;
1592 a596b957 2022-07-14 tracey
1593 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1594 a596b957 2022-07-14 tracey if (error)
1595 a596b957 2022-07-14 tracey goto done;
1596 a596b957 2022-07-14 tracey
1597 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1598 a596b957 2022-07-14 tracey if (error)
1599 a596b957 2022-07-14 tracey goto done;
1600 a596b957 2022-07-14 tracey
1601 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1602 a596b957 2022-07-14 tracey if (error)
1603 a596b957 2022-07-14 tracey goto done;
1604 a596b957 2022-07-14 tracey
1605 a596b957 2022-07-14 tracey error = got_blame(in_repo_path, commit_id, repo,
1606 a596b957 2022-07-14 tracey GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1607 a596b957 2022-07-14 tracey fd3, fd4, f1, f2);
1608 a596b957 2022-07-14 tracey
1609 a596b957 2022-07-14 tracey if (blob) {
1610 a596b957 2022-07-14 tracey free(bca.line_offsets);
1611 a596b957 2022-07-14 tracey for (i = 0; i < bca.nlines; i++) {
1612 a596b957 2022-07-14 tracey struct blame_line *bline = &bca.lines[i];
1613 a596b957 2022-07-14 tracey free(bline->id_str);
1614 a596b957 2022-07-14 tracey free(bline->committer);
1615 a596b957 2022-07-14 tracey }
1616 a596b957 2022-07-14 tracey }
1617 a596b957 2022-07-14 tracey done:
1618 a596b957 2022-07-14 tracey free(bca.lines);
1619 a596b957 2022-07-14 tracey if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1620 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1621 a596b957 2022-07-14 tracey if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1622 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1623 a596b957 2022-07-14 tracey if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1624 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1625 a596b957 2022-07-14 tracey if (bca.f) {
1626 a596b957 2022-07-14 tracey const struct got_error *bca_err =
1627 a596b957 2022-07-14 tracey got_gotweb_flushfile(bca.f, fd1);
1628 a596b957 2022-07-14 tracey if (error == NULL)
1629 a596b957 2022-07-14 tracey error = bca_err;
1630 a596b957 2022-07-14 tracey }
1631 a596b957 2022-07-14 tracey if (f1) {
1632 a596b957 2022-07-14 tracey const struct got_error *f1_err =
1633 a596b957 2022-07-14 tracey got_gotweb_flushfile(f1, fd5);
1634 a596b957 2022-07-14 tracey if (error == NULL)
1635 a596b957 2022-07-14 tracey error = f1_err;
1636 a596b957 2022-07-14 tracey }
1637 a596b957 2022-07-14 tracey if (f2) {
1638 a596b957 2022-07-14 tracey const struct got_error *f2_err =
1639 a596b957 2022-07-14 tracey got_gotweb_flushfile(f2, fd6);
1640 a596b957 2022-07-14 tracey if (error == NULL)
1641 a596b957 2022-07-14 tracey error = f2_err;
1642 a596b957 2022-07-14 tracey }
1643 a596b957 2022-07-14 tracey if (commit)
1644 a596b957 2022-07-14 tracey got_object_commit_close(commit);
1645 a596b957 2022-07-14 tracey if (blob)
1646 a596b957 2022-07-14 tracey got_object_blob_close(blob);
1647 a596b957 2022-07-14 tracey free(in_repo_path);
1648 a596b957 2022-07-14 tracey free(commit_id);
1649 a596b957 2022-07-14 tracey free(path);
1650 a596b957 2022-07-14 tracey return error;
1651 a596b957 2022-07-14 tracey }
1652 a596b957 2022-07-14 tracey
1653 a596b957 2022-07-14 tracey const struct got_error *
1654 a596b957 2022-07-14 tracey got_output_repo_diff(struct request *c)
1655 a596b957 2022-07-14 tracey {
1656 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1657 a596b957 2022-07-14 tracey struct transport *t = c->t;
1658 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1659 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
1660 a596b957 2022-07-14 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1661 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1662 a596b957 2022-07-14 tracey FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1663 a596b957 2022-07-14 tracey char *label1 = NULL, *label2 = NULL, *line = NULL;
1664 a596b957 2022-07-14 tracey char *newline, *eline = NULL, *color = NULL;
1665 a596b957 2022-07-14 tracey int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1666 a596b957 2022-07-14 tracey size_t linesize = 0;
1667 a596b957 2022-07-14 tracey ssize_t linelen;
1668 a596b957 2022-07-14 tracey int wrlen = 0;
1669 a596b957 2022-07-14 tracey
1670 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1671 a596b957 2022-07-14 tracey
1672 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1673 a596b957 2022-07-14 tracey if (error)
1674 a596b957 2022-07-14 tracey return error;
1675 a596b957 2022-07-14 tracey
1676 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1677 a596b957 2022-07-14 tracey if (error)
1678 a596b957 2022-07-14 tracey return error;
1679 a596b957 2022-07-14 tracey
1680 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1681 a596b957 2022-07-14 tracey if (error)
1682 a596b957 2022-07-14 tracey return error;
1683 a596b957 2022-07-14 tracey
1684 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
1685 a596b957 2022-07-14 tracey
1686 a596b957 2022-07-14 tracey if (rc->parent_id != NULL &&
1687 a596b957 2022-07-14 tracey strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1688 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&id1, &label1,
1689 a596b957 2022-07-14 tracey rc->parent_id, GOT_OBJ_TYPE_ANY,
1690 a596b957 2022-07-14 tracey &refs, repo);
1691 a596b957 2022-07-14 tracey if (error)
1692 a596b957 2022-07-14 tracey goto done;
1693 a596b957 2022-07-14 tracey }
1694 a596b957 2022-07-14 tracey
1695 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1696 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_ANY, &refs, repo);
1697 a596b957 2022-07-14 tracey if (error)
1698 a596b957 2022-07-14 tracey goto done;
1699 a596b957 2022-07-14 tracey
1700 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, id2);
1701 a596b957 2022-07-14 tracey if (error)
1702 a596b957 2022-07-14 tracey goto done;
1703 a596b957 2022-07-14 tracey
1704 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1705 a596b957 2022-07-14 tracey if (error)
1706 a596b957 2022-07-14 tracey goto done;
1707 a596b957 2022-07-14 tracey
1708 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1709 a596b957 2022-07-14 tracey if (error)
1710 a596b957 2022-07-14 tracey goto done;
1711 a596b957 2022-07-14 tracey
1712 a596b957 2022-07-14 tracey switch (obj_type) {
1713 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_BLOB:
1714 a596b957 2022-07-14 tracey error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1715 a596b957 2022-07-14 tracey id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1716 a596b957 2022-07-14 tracey repo, f3);
1717 a596b957 2022-07-14 tracey break;
1718 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_TREE:
1719 a596b957 2022-07-14 tracey error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1720 a596b957 2022-07-14 tracey id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1721 a596b957 2022-07-14 tracey repo, f3);
1722 a596b957 2022-07-14 tracey break;
1723 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_COMMIT:
1724 a596b957 2022-07-14 tracey error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1725 a596b957 2022-07-14 tracey fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1726 a596b957 2022-07-14 tracey repo, f3);
1727 a596b957 2022-07-14 tracey break;
1728 a596b957 2022-07-14 tracey default:
1729 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1730 a596b957 2022-07-14 tracey }
1731 a596b957 2022-07-14 tracey if (error)
1732 a596b957 2022-07-14 tracey goto done;
1733 a596b957 2022-07-14 tracey
1734 a596b957 2022-07-14 tracey if (fseek(f1, 0, SEEK_SET) == -1) {
1735 a596b957 2022-07-14 tracey error = got_ferror(f1, GOT_ERR_IO);
1736 a596b957 2022-07-14 tracey goto done;
1737 a596b957 2022-07-14 tracey }
1738 a596b957 2022-07-14 tracey
1739 a596b957 2022-07-14 tracey if (fseek(f2, 0, SEEK_SET) == -1) {
1740 a596b957 2022-07-14 tracey error = got_ferror(f2, GOT_ERR_IO);
1741 a596b957 2022-07-14 tracey goto done;
1742 a596b957 2022-07-14 tracey }
1743 a596b957 2022-07-14 tracey
1744 a596b957 2022-07-14 tracey if (fseek(f3, 0, SEEK_SET) == -1) {
1745 a596b957 2022-07-14 tracey error = got_ferror(f3, GOT_ERR_IO);
1746 a596b957 2022-07-14 tracey goto done;
1747 a596b957 2022-07-14 tracey }
1748 a596b957 2022-07-14 tracey
1749 a596b957 2022-07-14 tracey while ((linelen = getline(&line, &linesize, f3)) != -1) {
1750 a596b957 2022-07-14 tracey if (strncmp(line, "-", 1) == 0) {
1751 a596b957 2022-07-14 tracey color = strdup("diff_minus");
1752 a596b957 2022-07-14 tracey if (color == NULL) {
1753 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1754 a596b957 2022-07-14 tracey goto done;
1755 a596b957 2022-07-14 tracey }
1756 a596b957 2022-07-14 tracey } else if (strncmp(line, "+", 1) == 0) {
1757 a596b957 2022-07-14 tracey color = strdup("diff_plus");
1758 a596b957 2022-07-14 tracey if (color == NULL) {
1759 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1760 a596b957 2022-07-14 tracey goto done;
1761 a596b957 2022-07-14 tracey }
1762 a596b957 2022-07-14 tracey } else if (strncmp(line, "@@", 2) == 0) {
1763 a596b957 2022-07-14 tracey color = strdup("diff_chunk_header");
1764 a596b957 2022-07-14 tracey if (color == NULL) {
1765 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1766 a596b957 2022-07-14 tracey goto done;
1767 a596b957 2022-07-14 tracey }
1768 a596b957 2022-07-14 tracey } else if (strncmp(line, "@@", 2) == 0) {
1769 a596b957 2022-07-14 tracey color = strdup("diff_chunk_header");
1770 a596b957 2022-07-14 tracey if (color == NULL) {
1771 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1772 a596b957 2022-07-14 tracey goto done;
1773 a596b957 2022-07-14 tracey }
1774 a596b957 2022-07-14 tracey } else if (strncmp(line, "commit +", 8) == 0) {
1775 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1776 a596b957 2022-07-14 tracey if (color == NULL) {
1777 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1778 a596b957 2022-07-14 tracey goto done;
1779 a596b957 2022-07-14 tracey }
1780 a596b957 2022-07-14 tracey } else if (strncmp(line, "commit -", 8) == 0) {
1781 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1782 a596b957 2022-07-14 tracey if (color == NULL) {
1783 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1784 a596b957 2022-07-14 tracey goto done;
1785 a596b957 2022-07-14 tracey }
1786 a596b957 2022-07-14 tracey } else if (strncmp(line, "blob +", 6) == 0) {
1787 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1788 a596b957 2022-07-14 tracey if (color == NULL) {
1789 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1790 a596b957 2022-07-14 tracey goto done;
1791 a596b957 2022-07-14 tracey }
1792 a596b957 2022-07-14 tracey } else if (strncmp(line, "blob -", 6) == 0) {
1793 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1794 a596b957 2022-07-14 tracey if (color == NULL) {
1795 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1796 a596b957 2022-07-14 tracey goto done;
1797 a596b957 2022-07-14 tracey }
1798 a596b957 2022-07-14 tracey } else if (strncmp(line, "file +", 6) == 0) {
1799 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1800 a596b957 2022-07-14 tracey if (color == NULL) {
1801 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1802 a596b957 2022-07-14 tracey goto done;
1803 a596b957 2022-07-14 tracey }
1804 a596b957 2022-07-14 tracey } else if (strncmp(line, "file -", 6) == 0) {
1805 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1806 a596b957 2022-07-14 tracey if (color == NULL) {
1807 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1808 a596b957 2022-07-14 tracey goto done;
1809 a596b957 2022-07-14 tracey }
1810 a596b957 2022-07-14 tracey } else if (strncmp(line, "from:", 5) == 0) {
1811 a596b957 2022-07-14 tracey color = strdup("diff_author");
1812 a596b957 2022-07-14 tracey if (color == NULL) {
1813 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1814 a596b957 2022-07-14 tracey goto done;
1815 a596b957 2022-07-14 tracey }
1816 a596b957 2022-07-14 tracey } else if (strncmp(line, "via:", 4) == 0) {
1817 a596b957 2022-07-14 tracey color = strdup("diff_author");
1818 a596b957 2022-07-14 tracey if (color == NULL) {
1819 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1820 a596b957 2022-07-14 tracey goto done;
1821 a596b957 2022-07-14 tracey }
1822 a596b957 2022-07-14 tracey } else if (strncmp(line, "date:", 5) == 0) {
1823 a596b957 2022-07-14 tracey color = strdup("diff_date");
1824 a596b957 2022-07-14 tracey if (color == NULL) {
1825 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1826 a596b957 2022-07-14 tracey goto done;
1827 a596b957 2022-07-14 tracey }
1828 a596b957 2022-07-14 tracey }
1829 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "<div id='diff_line' class='") == -1)
1830 a596b957 2022-07-14 tracey goto done;
1831 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, color ? color : "") == -1)
1832 a596b957 2022-07-14 tracey goto done;
1833 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "'>") == -1)
1834 a596b957 2022-07-14 tracey goto done;
1835 a596b957 2022-07-14 tracey newline = strchr(line, '\n');
1836 a596b957 2022-07-14 tracey if (newline)
1837 a596b957 2022-07-14 tracey *newline = '\0';
1838 a596b957 2022-07-14 tracey
1839 a596b957 2022-07-14 tracey error = gotweb_escape_html(&eline, line);
1840 a596b957 2022-07-14 tracey if (error)
1841 a596b957 2022-07-14 tracey goto done;
1842 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, eline) == -1)
1843 a596b957 2022-07-14 tracey goto done;
1844 a596b957 2022-07-14 tracey free(eline);
1845 a596b957 2022-07-14 tracey eline = NULL;
1846 a596b957 2022-07-14 tracey
1847 a596b957 2022-07-14 tracey if (fcgi_gen_response(c, "</div>\n") == -1)
1848 a596b957 2022-07-14 tracey goto done;
1849 a596b957 2022-07-14 tracey if (linelen > 0)
1850 a596b957 2022-07-14 tracey wrlen = wrlen + linelen;
1851 a596b957 2022-07-14 tracey free(color);
1852 a596b957 2022-07-14 tracey color = NULL;
1853 a596b957 2022-07-14 tracey }
1854 a596b957 2022-07-14 tracey if (linelen == -1 && ferror(f3))
1855 a596b957 2022-07-14 tracey error = got_error_from_errno("getline");
1856 a596b957 2022-07-14 tracey done:
1857 a596b957 2022-07-14 tracey free(color);
1858 a596b957 2022-07-14 tracey if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1859 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1860 a596b957 2022-07-14 tracey if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1861 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1862 a596b957 2022-07-14 tracey if (f1) {
1863 a596b957 2022-07-14 tracey const struct got_error *f1_err =
1864 a596b957 2022-07-14 tracey got_gotweb_flushfile(f1, fd1);
1865 a596b957 2022-07-14 tracey if (error == NULL)
1866 a596b957 2022-07-14 tracey error = f1_err;
1867 a596b957 2022-07-14 tracey }
1868 a596b957 2022-07-14 tracey if (f2) {
1869 a596b957 2022-07-14 tracey const struct got_error *f2_err =
1870 a596b957 2022-07-14 tracey got_gotweb_flushfile(f2, fd2);
1871 a596b957 2022-07-14 tracey if (error == NULL)
1872 a596b957 2022-07-14 tracey error = f2_err;
1873 a596b957 2022-07-14 tracey }
1874 a596b957 2022-07-14 tracey if (f3) {
1875 a596b957 2022-07-14 tracey const struct got_error *f3_err =
1876 a596b957 2022-07-14 tracey got_gotweb_flushfile(f3, fd3);
1877 a596b957 2022-07-14 tracey if (error == NULL)
1878 a596b957 2022-07-14 tracey error = f3_err;
1879 a596b957 2022-07-14 tracey }
1880 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
1881 a596b957 2022-07-14 tracey free(line);
1882 a596b957 2022-07-14 tracey free(eline);
1883 a596b957 2022-07-14 tracey free(label1);
1884 a596b957 2022-07-14 tracey free(label2);
1885 a596b957 2022-07-14 tracey free(id1);
1886 a596b957 2022-07-14 tracey free(id2);
1887 a596b957 2022-07-14 tracey return error;
1888 a596b957 2022-07-14 tracey }
1889 a596b957 2022-07-14 tracey
1890 a596b957 2022-07-14 tracey static const struct got_error *
1891 a596b957 2022-07-14 tracey got_init_repo_commit(struct repo_commit **rc)
1892 a596b957 2022-07-14 tracey {
1893 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1894 a596b957 2022-07-14 tracey
1895 a596b957 2022-07-14 tracey *rc = calloc(1, sizeof(**rc));
1896 a596b957 2022-07-14 tracey if (*rc == NULL)
1897 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
1898 a596b957 2022-07-14 tracey
1899 a596b957 2022-07-14 tracey (*rc)->path = NULL;
1900 a596b957 2022-07-14 tracey (*rc)->refs_str = NULL;
1901 a596b957 2022-07-14 tracey (*rc)->commit_id = NULL;
1902 a596b957 2022-07-14 tracey (*rc)->committer = NULL;
1903 a596b957 2022-07-14 tracey (*rc)->author = NULL;
1904 a596b957 2022-07-14 tracey (*rc)->parent_id = NULL;
1905 a596b957 2022-07-14 tracey (*rc)->tree_id = NULL;
1906 a596b957 2022-07-14 tracey (*rc)->commit_msg = NULL;
1907 a596b957 2022-07-14 tracey
1908 a596b957 2022-07-14 tracey return error;
1909 a596b957 2022-07-14 tracey }
1910 a596b957 2022-07-14 tracey
1911 a596b957 2022-07-14 tracey static const struct got_error *
1912 a596b957 2022-07-14 tracey got_init_repo_tag(struct repo_tag **rt)
1913 a596b957 2022-07-14 tracey {
1914 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1915 a596b957 2022-07-14 tracey
1916 a596b957 2022-07-14 tracey *rt = calloc(1, sizeof(**rt));
1917 a596b957 2022-07-14 tracey if (*rt == NULL)
1918 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
1919 a596b957 2022-07-14 tracey
1920 a596b957 2022-07-14 tracey (*rt)->commit_id = NULL;
1921 a596b957 2022-07-14 tracey (*rt)->tag_name = NULL;
1922 a596b957 2022-07-14 tracey (*rt)->tag_commit = NULL;
1923 a596b957 2022-07-14 tracey (*rt)->commit_msg = NULL;
1924 a596b957 2022-07-14 tracey (*rt)->tagger = NULL;
1925 a596b957 2022-07-14 tracey
1926 a596b957 2022-07-14 tracey return error;
1927 a596b957 2022-07-14 tracey }