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