Blame


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