2 a596b957 2022-07-14 tracey * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
6 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
7 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
9 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 b4c20a19 2022-07-15 naddy #include <sys/queue.h>
19 a596b957 2022-07-14 tracey #include <sys/socket.h>
20 a596b957 2022-07-14 tracey #include <sys/stat.h>
22 a596b957 2022-07-14 tracey #include <event.h>
23 a596b957 2022-07-14 tracey #include <imsg.h>
24 a596b957 2022-07-14 tracey #include <sha1.h>
25 a596b957 2022-07-14 tracey #include <stdlib.h>
26 a596b957 2022-07-14 tracey #include <stdio.h>
27 a596b957 2022-07-14 tracey #include <string.h>
28 a596b957 2022-07-14 tracey #include <unistd.h>
30 a596b957 2022-07-14 tracey #include "got_error.h"
31 a596b957 2022-07-14 tracey #include "got_object.h"
32 a596b957 2022-07-14 tracey #include "got_reference.h"
33 a596b957 2022-07-14 tracey #include "got_repository.h"
34 a596b957 2022-07-14 tracey #include "got_path.h"
35 a596b957 2022-07-14 tracey #include "got_cancel.h"
36 a596b957 2022-07-14 tracey #include "got_diff.h"
37 a596b957 2022-07-14 tracey #include "got_commit_graph.h"
38 a596b957 2022-07-14 tracey #include "got_blame.h"
39 a596b957 2022-07-14 tracey #include "got_privsep.h"
41 a596b957 2022-07-14 tracey #include "proc.h"
42 a596b957 2022-07-14 tracey #include "gotwebd.h"
44 a596b957 2022-07-14 tracey static const struct got_error *got_init_repo_commit(struct repo_commit **);
45 a596b957 2022-07-14 tracey static const struct got_error *got_init_repo_tag(struct repo_tag **);
46 a596b957 2022-07-14 tracey static const struct got_error *got_get_repo_commit(struct request *,
47 a596b957 2022-07-14 tracey struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
48 a596b957 2022-07-14 tracey struct got_object_id *);
49 a596b957 2022-07-14 tracey static const struct got_error *got_gotweb_dupfd(int *, int *);
50 a596b957 2022-07-14 tracey static const struct got_error *got_gotweb_openfile(FILE **, int *, int *);
51 a596b957 2022-07-14 tracey static const struct got_error *got_gotweb_flushfile(FILE *, int);
52 a596b957 2022-07-14 tracey static const struct got_error *got_gotweb_blame_cb(void *, int, int,
53 a596b957 2022-07-14 tracey struct got_commit_object *,struct got_object_id *);
55 a596b957 2022-07-14 tracey static const struct got_error *
56 a596b957 2022-07-14 tracey got_gotweb_flushfile(FILE *f, int fd)
58 a596b957 2022-07-14 tracey if (fseek(f, 0, SEEK_SET) == -1)
59 a596b957 2022-07-14 tracey return got_error_from_errno("fseek");
61 a596b957 2022-07-14 tracey if (ftruncate(fd, 0) == -1)
62 a596b957 2022-07-14 tracey return got_error_from_errno("ftruncate");
64 a596b957 2022-07-14 tracey if (fsync(fd) == -1)
65 a596b957 2022-07-14 tracey return got_error_from_errno("fsync");
67 a596b957 2022-07-14 tracey if (f && fclose(f) == EOF)
68 a596b957 2022-07-14 tracey return got_error_from_errno("fclose");
70 a596b957 2022-07-14 tracey if (fd != -1 && close(fd) != -1)
71 a596b957 2022-07-14 tracey return got_error_from_errno("close");
73 a596b957 2022-07-14 tracey return NULL;
76 a596b957 2022-07-14 tracey static const struct got_error *
77 a596b957 2022-07-14 tracey got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
79 a596b957 2022-07-14 tracey *fd = dup(*priv_fd);
80 e9d3ad59 2022-08-31 stsp if (*fd == -1)
81 e9d3ad59 2022-08-31 stsp return got_error_from_errno("dup");
83 a596b957 2022-07-14 tracey *f = fdopen(*fd, "w+");
84 a596b957 2022-07-14 tracey if (*f == NULL) {
85 a596b957 2022-07-14 tracey close(*fd);
86 dd84f505 2022-08-31 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
89 dd84f505 2022-08-31 stsp return NULL;
92 a596b957 2022-07-14 tracey static const struct got_error *
93 a596b957 2022-07-14 tracey got_gotweb_dupfd(int *priv_fd, int *fd)
95 a596b957 2022-07-14 tracey *fd = dup(*priv_fd);
97 a596b957 2022-07-14 tracey if (*fd < 0)
98 a596b957 2022-07-14 tracey return NULL;
100 dd84f505 2022-08-31 stsp return NULL;
103 a596b957 2022-07-14 tracey const struct got_error *
104 c127fc49 2022-11-22 op got_get_repo_owner(char **owner, struct request *c)
106 a596b957 2022-07-14 tracey struct server *srv = c->srv;
107 a596b957 2022-07-14 tracey struct transport *t = c->t;
108 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
109 a596b957 2022-07-14 tracey const char *gitconfig_owner;
111 a596b957 2022-07-14 tracey *owner = NULL;
113 a596b957 2022-07-14 tracey if (srv->show_repo_owner == 0)
114 a596b957 2022-07-14 tracey return NULL;
116 a596b957 2022-07-14 tracey gitconfig_owner = got_repo_get_gitconfig_owner(repo);
117 a596b957 2022-07-14 tracey if (gitconfig_owner) {
118 a596b957 2022-07-14 tracey *owner = strdup(gitconfig_owner);
119 1999985f 2022-08-30 tracey if (*owner == NULL)
120 1999985f 2022-08-30 tracey return got_error_from_errno("strdup");
122 1999985f 2022-08-30 tracey *owner = strdup("");
123 a596b957 2022-07-14 tracey if (*owner == NULL)
124 a596b957 2022-07-14 tracey return got_error_from_errno("strdup");
126 dd84f505 2022-08-31 stsp return NULL;
129 a596b957 2022-07-14 tracey const struct got_error *
130 c127fc49 2022-11-22 op got_get_repo_age(char **repo_age, struct request *c,
131 a596b957 2022-07-14 tracey const char *refname, int ref_tm)
133 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
134 a596b957 2022-07-14 tracey struct server *srv = c->srv;
135 a596b957 2022-07-14 tracey struct transport *t = c->t;
136 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
137 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
138 a596b957 2022-07-14 tracey struct got_reflist_head refs;
139 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
140 a596b957 2022-07-14 tracey time_t committer_time = 0, cmp_time = 0;
142 a596b957 2022-07-14 tracey *repo_age = NULL;
143 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
145 a596b957 2022-07-14 tracey if (srv->show_repo_age == 0)
146 a596b957 2022-07-14 tracey return NULL;
148 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/heads",
149 a596b957 2022-07-14 tracey got_ref_cmp_by_name, NULL);
150 a596b957 2022-07-14 tracey if (error)
151 a596b957 2022-07-14 tracey goto done;
154 a596b957 2022-07-14 tracey * Find the youngest branch tip in the repository, or the age of
155 a596b957 2022-07-14 tracey * the a specific branch tip if a name was provided by the caller.
157 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
158 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
160 a596b957 2022-07-14 tracey if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
161 a596b957 2022-07-14 tracey continue;
163 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, re->ref);
164 a596b957 2022-07-14 tracey if (error)
165 a596b957 2022-07-14 tracey goto done;
167 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
168 a596b957 2022-07-14 tracey free(id);
169 a596b957 2022-07-14 tracey if (error)
170 a596b957 2022-07-14 tracey goto done;
172 a596b957 2022-07-14 tracey committer_time =
173 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
174 a596b957 2022-07-14 tracey got_object_commit_close(commit);
175 a596b957 2022-07-14 tracey if (cmp_time < committer_time)
176 a596b957 2022-07-14 tracey cmp_time = committer_time;
178 a596b957 2022-07-14 tracey if (refname)
182 a596b957 2022-07-14 tracey if (cmp_time != 0) {
183 a596b957 2022-07-14 tracey committer_time = cmp_time;
184 a596b957 2022-07-14 tracey error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
187 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
188 a596b957 2022-07-14 tracey return error;
191 a596b957 2022-07-14 tracey static const struct got_error *
192 a596b957 2022-07-14 tracey got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
193 a596b957 2022-07-14 tracey struct got_commit_object *commit, struct got_reflist_head *refs,
194 a596b957 2022-07-14 tracey struct got_object_id *id)
196 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
197 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
198 a596b957 2022-07-14 tracey struct got_object_id *id2 = NULL;
199 a596b957 2022-07-14 tracey struct got_object_qid *parent_id;
200 a596b957 2022-07-14 tracey struct transport *t = c->t;
201 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
202 a596b957 2022-07-14 tracey char *commit_msg = NULL, *commit_msg0;
204 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, refs, entry) {
206 a596b957 2022-07-14 tracey const char *name;
207 a596b957 2022-07-14 tracey struct got_tag_object *tag = NULL;
208 a596b957 2022-07-14 tracey struct got_object_id *ref_id;
211 a596b957 2022-07-14 tracey if (got_ref_is_symbolic(re->ref))
212 a596b957 2022-07-14 tracey continue;
214 a596b957 2022-07-14 tracey name = got_ref_get_name(re->ref);
215 a596b957 2022-07-14 tracey if (strncmp(name, "refs/", 5) == 0)
216 a596b957 2022-07-14 tracey name += 5;
217 a596b957 2022-07-14 tracey if (strncmp(name, "got/", 4) == 0)
218 a596b957 2022-07-14 tracey continue;
219 a596b957 2022-07-14 tracey if (strncmp(name, "heads/", 6) == 0)
220 a596b957 2022-07-14 tracey name += 6;
221 a596b957 2022-07-14 tracey if (strncmp(name, "remotes/", 8) == 0) {
222 a596b957 2022-07-14 tracey name += 8;
223 341fa7ca 2022-09-01 op if (strstr(name, "/" GOT_REF_HEAD) != NULL)
224 a596b957 2022-07-14 tracey continue;
226 a596b957 2022-07-14 tracey error = got_ref_resolve(&ref_id, t->repo, re->ref);
227 a596b957 2022-07-14 tracey if (error)
228 a596b957 2022-07-14 tracey return error;
229 a596b957 2022-07-14 tracey if (strncmp(name, "tags/", 5) == 0) {
230 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, t->repo, ref_id);
231 a596b957 2022-07-14 tracey if (error) {
232 a596b957 2022-07-14 tracey if (error->code != GOT_ERR_OBJ_TYPE) {
233 a596b957 2022-07-14 tracey free(ref_id);
234 a596b957 2022-07-14 tracey continue;
237 a596b957 2022-07-14 tracey * Ref points at something other
238 a596b957 2022-07-14 tracey * than a tag.
240 a596b957 2022-07-14 tracey error = NULL;
241 a596b957 2022-07-14 tracey tag = NULL;
244 a596b957 2022-07-14 tracey cmp = got_object_id_cmp(tag ?
245 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag) : ref_id, id);
246 a596b957 2022-07-14 tracey free(ref_id);
248 a596b957 2022-07-14 tracey got_object_tag_close(tag);
249 a596b957 2022-07-14 tracey if (cmp != 0)
250 a596b957 2022-07-14 tracey continue;
251 a596b957 2022-07-14 tracey s = repo_commit->refs_str;
252 a596b957 2022-07-14 tracey if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
253 a596b957 2022-07-14 tracey s ? ", " : "", name) == -1) {
254 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
256 a596b957 2022-07-14 tracey repo_commit->refs_str = NULL;
257 a596b957 2022-07-14 tracey return error;
262 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->commit_id, id);
263 a596b957 2022-07-14 tracey if (error)
264 a596b957 2022-07-14 tracey return error;
266 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->tree_id,
267 a596b957 2022-07-14 tracey got_object_commit_get_tree_id(commit));
268 a596b957 2022-07-14 tracey if (error)
269 a596b957 2022-07-14 tracey return error;
271 a596b957 2022-07-14 tracey if (qs->action == DIFF) {
272 a596b957 2022-07-14 tracey parent_id = STAILQ_FIRST(
273 a596b957 2022-07-14 tracey got_object_commit_get_parent_ids(commit));
274 a596b957 2022-07-14 tracey if (parent_id != NULL) {
275 a596b957 2022-07-14 tracey id2 = got_object_id_dup(&parent_id->id);
276 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->parent_id, id2);
277 a596b957 2022-07-14 tracey if (error)
278 a596b957 2022-07-14 tracey return error;
279 a596b957 2022-07-14 tracey free(id2);
281 a596b957 2022-07-14 tracey repo_commit->parent_id = strdup("/dev/null");
282 a596b957 2022-07-14 tracey if (repo_commit->parent_id == NULL) {
283 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
284 a596b957 2022-07-14 tracey return error;
289 a596b957 2022-07-14 tracey repo_commit->committer_time =
290 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
292 a596b957 2022-07-14 tracey repo_commit->author =
293 a596b957 2022-07-14 tracey strdup(got_object_commit_get_author(commit));
294 a596b957 2022-07-14 tracey if (repo_commit->author == NULL) {
295 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
296 a596b957 2022-07-14 tracey return error;
298 a596b957 2022-07-14 tracey repo_commit->committer =
299 a596b957 2022-07-14 tracey strdup(got_object_commit_get_committer(commit));
300 a596b957 2022-07-14 tracey if (repo_commit->committer == NULL) {
301 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
302 a596b957 2022-07-14 tracey return error;
304 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
305 a596b957 2022-07-14 tracey if (error)
306 a596b957 2022-07-14 tracey return error;
308 a596b957 2022-07-14 tracey commit_msg = commit_msg0;
309 a596b957 2022-07-14 tracey while (*commit_msg == '\n')
310 a596b957 2022-07-14 tracey commit_msg++;
312 a596b957 2022-07-14 tracey repo_commit->commit_msg = strdup(commit_msg);
313 a596b957 2022-07-14 tracey if (repo_commit->commit_msg == NULL)
314 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
315 a596b957 2022-07-14 tracey free(commit_msg0);
316 a596b957 2022-07-14 tracey return error;
319 a596b957 2022-07-14 tracey const struct got_error *
320 a596b957 2022-07-14 tracey got_get_repo_commits(struct request *c, int limit)
322 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
323 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
324 a596b957 2022-07-14 tracey struct got_commit_graph *graph = NULL;
325 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
326 a596b957 2022-07-14 tracey struct got_reflist_head refs;
327 bce44e0b 2022-09-02 op struct got_reference *ref = NULL;
328 a596b957 2022-07-14 tracey struct repo_commit *repo_commit = NULL;
329 a596b957 2022-07-14 tracey struct server *srv = c->srv;
330 a596b957 2022-07-14 tracey struct transport *t = c->t;
331 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
332 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
333 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
334 a596b957 2022-07-14 tracey char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
335 a596b957 2022-07-14 tracey int chk_next = 0, chk_multi = 0, commit_found = 0;
336 a596b957 2022-07-14 tracey int obj_type, limit_chk = 0;
338 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
340 a596b957 2022-07-14 tracey if (qs->file != NULL && strlen(qs->file) > 0)
341 a596b957 2022-07-14 tracey if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
342 a596b957 2022-07-14 tracey qs->file) == -1)
343 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
345 a596b957 2022-07-14 tracey if (asprintf(&repo_path, "%s/%s", srv->repos_path,
346 89ae185c 2022-09-01 op repo_dir->name) == -1) {
347 89ae185c 2022-09-01 op error = got_error_from_errno("asprintf");
352 a596b957 2022-07-14 tracey * XXX: jumping directly to a commit id via
353 a596b957 2022-07-14 tracey * got_repo_match_object_id_prefix significantly improves performance,
354 a596b957 2022-07-14 tracey * but does not allow us to create a PREVIOUS button, since commits can
355 a596b957 2022-07-14 tracey * only be itereated forward. So, we have to match as we iterate from
356 a596b957 2022-07-14 tracey * the headref.
358 a596b957 2022-07-14 tracey if (qs->action == BRIEFS || qs->action == COMMITS ||
359 a596b957 2022-07-14 tracey (qs->action == TREE && qs->commit == NULL)) {
360 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->headref, 0);
361 a596b957 2022-07-14 tracey if (error)
362 a596b957 2022-07-14 tracey goto done;
364 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
365 a596b957 2022-07-14 tracey if (error)
366 a596b957 2022-07-14 tracey goto done;
367 a596b957 2022-07-14 tracey } else if (qs->commit != NULL) {
368 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->commit, 0);
369 a596b957 2022-07-14 tracey if (error == NULL) {
370 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
371 a596b957 2022-07-14 tracey if (error)
372 a596b957 2022-07-14 tracey goto done;
373 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, id);
374 a596b957 2022-07-14 tracey if (error)
375 a596b957 2022-07-14 tracey goto done;
376 a596b957 2022-07-14 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
377 a596b957 2022-07-14 tracey struct got_tag_object *tag;
378 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, repo, id);
379 a596b957 2022-07-14 tracey if (error)
380 a596b957 2022-07-14 tracey goto done;
381 a596b957 2022-07-14 tracey if (got_object_tag_get_object_type(tag) !=
382 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT) {
383 a596b957 2022-07-14 tracey got_object_tag_close(tag);
384 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
385 a596b957 2022-07-14 tracey goto done;
387 a596b957 2022-07-14 tracey free(id);
388 a596b957 2022-07-14 tracey id = got_object_id_dup(
389 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag));
390 a596b957 2022-07-14 tracey if (id == NULL)
391 a596b957 2022-07-14 tracey error = got_error_from_errno(
392 a596b957 2022-07-14 tracey "got_object_id_dup");
393 a596b957 2022-07-14 tracey got_object_tag_close(tag);
394 a596b957 2022-07-14 tracey if (error)
395 a596b957 2022-07-14 tracey goto done;
396 a596b957 2022-07-14 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
397 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
398 a596b957 2022-07-14 tracey goto done;
401 a596b957 2022-07-14 tracey error = got_repo_match_object_id_prefix(&id, qs->commit,
402 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, repo);
403 a596b957 2022-07-14 tracey if (error)
404 a596b957 2022-07-14 tracey goto done;
407 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, repo_path);
408 a596b957 2022-07-14 tracey if (error)
409 a596b957 2022-07-14 tracey goto done;
411 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
412 a596b957 2022-07-14 tracey if (error)
413 a596b957 2022-07-14 tracey goto done;
415 a596b957 2022-07-14 tracey if (qs->file != NULL && strlen(qs->file) > 0) {
416 a596b957 2022-07-14 tracey error = got_commit_graph_open(&graph, file_path, 0);
417 a596b957 2022-07-14 tracey if (error)
418 a596b957 2022-07-14 tracey goto done;
420 a596b957 2022-07-14 tracey error = got_commit_graph_open(&graph, in_repo_path, 0);
421 a596b957 2022-07-14 tracey if (error)
422 a596b957 2022-07-14 tracey goto done;
425 a596b957 2022-07-14 tracey error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
426 a596b957 2022-07-14 tracey if (error)
427 a596b957 2022-07-14 tracey goto done;
429 a596b957 2022-07-14 tracey for (;;) {
430 d9787ed8 2022-09-10 op struct got_object_id next_id;
432 6227cf0e 2022-09-05 op error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
434 a596b957 2022-07-14 tracey if (error) {
435 a596b957 2022-07-14 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
436 a596b957 2022-07-14 tracey error = NULL;
437 a596b957 2022-07-14 tracey goto done;
440 d9787ed8 2022-09-10 op error = got_object_open_as_commit(&commit, repo, &next_id);
441 a596b957 2022-07-14 tracey if (error)
442 a596b957 2022-07-14 tracey goto done;
444 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
446 a596b957 2022-07-14 tracey if (error)
447 a596b957 2022-07-14 tracey goto done;
449 4a962942 2022-09-02 op error = got_init_repo_commit(&repo_commit);
453 a596b957 2022-07-14 tracey error = got_get_repo_commit(c, repo_commit, commit,
454 d9787ed8 2022-09-10 op &refs, &next_id);
456 4f0a80ed 2022-09-07 op gotweb_free_repo_commit(repo_commit);
457 a596b957 2022-07-14 tracey goto done;
460 4f0a80ed 2022-09-07 op if (limit_chk == ((limit * qs->page) - limit) &&
461 4f0a80ed 2022-09-07 op commit_found == 0 && repo_commit->commit_id != NULL) {
462 4f0a80ed 2022-09-07 op t->prev_id = strdup(repo_commit->commit_id);
463 4f0a80ed 2022-09-07 op if (t->prev_id == NULL) {
464 4f0a80ed 2022-09-07 op error = got_error_from_errno("strdup");
465 4f0a80ed 2022-09-07 op gotweb_free_repo_commit(repo_commit);
470 a596b957 2022-07-14 tracey if (qs->commit != NULL && commit_found == 0 && limit != 1) {
471 a596b957 2022-07-14 tracey if (strcmp(qs->commit, repo_commit->commit_id) == 0)
472 a596b957 2022-07-14 tracey commit_found = 1;
473 a596b957 2022-07-14 tracey else if (qs->file != NULL && strlen(qs->file) > 0 &&
474 a596b957 2022-07-14 tracey qs->page == 0)
475 a596b957 2022-07-14 tracey commit_found = 1;
477 4f0a80ed 2022-09-07 op gotweb_free_repo_commit(repo_commit);
478 a596b957 2022-07-14 tracey limit_chk++;
479 a596b957 2022-07-14 tracey continue;
483 4f0a80ed 2022-09-07 op TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
485 a596b957 2022-07-14 tracey if (limit == 1 && chk_multi == 0 &&
486 a596b957 2022-07-14 tracey srv->max_commits_display != 1)
487 a596b957 2022-07-14 tracey commit_found = 1;
489 a596b957 2022-07-14 tracey chk_multi = 1;
492 a596b957 2022-07-14 tracey * check for one more commit before breaking,
493 a596b957 2022-07-14 tracey * so we know whether to navigate through briefs
494 a596b957 2022-07-14 tracey * commits and summary
496 a596b957 2022-07-14 tracey if (chk_next && (qs->action == BRIEFS ||
497 a596b957 2022-07-14 tracey qs->action == COMMITS || qs->action == SUMMARY)) {
498 4a962942 2022-09-02 op t->next_id = strdup(repo_commit->commit_id);
499 a596b957 2022-07-14 tracey if (t->next_id == NULL) {
500 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
501 a596b957 2022-07-14 tracey goto done;
503 a596b957 2022-07-14 tracey if (commit) {
504 a596b957 2022-07-14 tracey got_object_commit_close(commit);
505 a596b957 2022-07-14 tracey commit = NULL;
507 4a962942 2022-09-02 op TAILQ_REMOVE(&t->repo_commits, repo_commit,
509 4a962942 2022-09-02 op gotweb_free_repo_commit(repo_commit);
510 a596b957 2022-07-14 tracey goto done;
513 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
514 a596b957 2022-07-14 tracey if (error || (limit && --limit == 0)) {
515 a596b957 2022-07-14 tracey if (commit_found || (qs->file != NULL &&
516 a596b957 2022-07-14 tracey strlen(qs->file) > 0))
517 a596b957 2022-07-14 tracey if (chk_multi == 0)
519 a596b957 2022-07-14 tracey chk_next = 1;
521 a596b957 2022-07-14 tracey if (commit) {
522 a596b957 2022-07-14 tracey got_object_commit_close(commit);
523 a596b957 2022-07-14 tracey commit = NULL;
528 bce44e0b 2022-09-02 op got_ref_close(ref);
529 a596b957 2022-07-14 tracey if (commit)
530 a596b957 2022-07-14 tracey got_object_commit_close(commit);
531 a596b957 2022-07-14 tracey if (graph)
532 a596b957 2022-07-14 tracey got_commit_graph_close(graph);
533 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
534 dfa5768d 2022-09-02 op free(in_repo_path);
535 a596b957 2022-07-14 tracey free(file_path);
536 a596b957 2022-07-14 tracey free(repo_path);
537 a596b957 2022-07-14 tracey free(id);
538 a596b957 2022-07-14 tracey return error;
541 a596b957 2022-07-14 tracey const struct got_error *
542 a596b957 2022-07-14 tracey got_get_repo_tags(struct request *c, int limit)
544 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
545 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
546 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
547 a596b957 2022-07-14 tracey struct got_reflist_head refs;
548 a596b957 2022-07-14 tracey struct got_reference *ref;
549 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
550 a596b957 2022-07-14 tracey struct server *srv = c->srv;
551 a596b957 2022-07-14 tracey struct transport *t = c->t;
552 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
553 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
554 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
555 a596b957 2022-07-14 tracey struct got_tag_object *tag = NULL;
556 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
557 a596b957 2022-07-14 tracey char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
558 625e5896 2022-09-01 op char *tag_commit = NULL, *tag_commit0 = NULL;
559 a596b957 2022-07-14 tracey char *commit_msg = NULL, *commit_msg0 = NULL;
560 a596b957 2022-07-14 tracey int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
562 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
564 a596b957 2022-07-14 tracey if (asprintf(&repo_path, "%s/%s", srv->repos_path,
565 a596b957 2022-07-14 tracey repo_dir->name) == -1)
566 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
568 1abb18e1 2022-12-20 op if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
569 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->headref, 0);
570 a596b957 2022-07-14 tracey if (error)
571 a596b957 2022-07-14 tracey goto err;
572 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
573 a596b957 2022-07-14 tracey got_ref_close(ref);
574 a596b957 2022-07-14 tracey if (error)
575 a596b957 2022-07-14 tracey goto err;
576 a596b957 2022-07-14 tracey } else if (qs->commit == NULL && qs->action == TAG) {
577 a596b957 2022-07-14 tracey error = got_error_msg(GOT_ERR_EOF, "commit id missing");
578 a596b957 2022-07-14 tracey goto err;
580 a596b957 2022-07-14 tracey error = got_repo_match_object_id_prefix(&id, qs->commit,
581 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, repo);
582 a596b957 2022-07-14 tracey if (error)
583 a596b957 2022-07-14 tracey goto err;
586 a596b957 2022-07-14 tracey if (qs->action != SUMMARY && qs->action != TAGS) {
587 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
588 a596b957 2022-07-14 tracey if (error)
589 a596b957 2022-07-14 tracey goto err;
590 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
591 a596b957 2022-07-14 tracey if (error)
592 a596b957 2022-07-14 tracey goto err;
593 a596b957 2022-07-14 tracey if (commit) {
594 a596b957 2022-07-14 tracey got_object_commit_close(commit);
595 a596b957 2022-07-14 tracey commit = NULL;
599 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, repo_path);
600 a596b957 2022-07-14 tracey if (error)
601 a596b957 2022-07-14 tracey goto err;
603 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
605 a596b957 2022-07-14 tracey if (error)
606 a596b957 2022-07-14 tracey goto err;
608 a596b957 2022-07-14 tracey if (limit == 1)
609 a596b957 2022-07-14 tracey chk_multi = 0;
612 a596b957 2022-07-14 tracey * XXX: again, see previous message about caching
615 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
616 a596b957 2022-07-14 tracey struct repo_tag *new_repo_tag = NULL;
617 a596b957 2022-07-14 tracey error = got_init_repo_tag(&new_repo_tag);
618 a596b957 2022-07-14 tracey if (error)
619 a596b957 2022-07-14 tracey goto err;
621 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
623 a596b957 2022-07-14 tracey new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
624 a596b957 2022-07-14 tracey if (new_repo_tag->tag_name == NULL) {
625 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
626 a596b957 2022-07-14 tracey goto err;
632 b163541d 2022-09-02 op free(id_str);
633 b163541d 2022-09-02 op id_str = NULL;
635 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, re->ref);
636 a596b957 2022-07-14 tracey if (error)
637 a596b957 2022-07-14 tracey goto done;
640 bc95141c 2022-09-02 op got_object_tag_close(tag);
641 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, repo, id);
642 a596b957 2022-07-14 tracey if (error) {
643 b163541d 2022-09-02 op if (error->code != GOT_ERR_OBJ_TYPE)
644 a596b957 2022-07-14 tracey goto done;
645 a596b957 2022-07-14 tracey /* "lightweight" tag */
646 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
648 a596b957 2022-07-14 tracey goto done;
649 a596b957 2022-07-14 tracey new_repo_tag->tagger =
650 a596b957 2022-07-14 tracey strdup(got_object_commit_get_committer(commit));
651 a596b957 2022-07-14 tracey if (new_repo_tag->tagger == NULL) {
652 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
653 a596b957 2022-07-14 tracey goto err;
655 a596b957 2022-07-14 tracey new_repo_tag->tagger_time =
656 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
657 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str, id);
658 a596b957 2022-07-14 tracey if (error)
659 a596b957 2022-07-14 tracey goto err;
661 a596b957 2022-07-14 tracey new_repo_tag->tagger =
662 a596b957 2022-07-14 tracey strdup(got_object_tag_get_tagger(tag));
663 a596b957 2022-07-14 tracey if (new_repo_tag->tagger == NULL) {
664 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
665 a596b957 2022-07-14 tracey goto err;
667 a596b957 2022-07-14 tracey new_repo_tag->tagger_time =
668 a596b957 2022-07-14 tracey got_object_tag_get_tagger_time(tag);
669 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str,
670 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag));
671 a596b957 2022-07-14 tracey if (error)
672 a596b957 2022-07-14 tracey goto err;
675 a596b957 2022-07-14 tracey new_repo_tag->commit_id = strdup(id_str);
676 a596b957 2022-07-14 tracey if (new_repo_tag->commit_id == NULL)
677 a596b957 2022-07-14 tracey goto err;
679 a596b957 2022-07-14 tracey if (commit_found == 0 && qs->commit != NULL &&
680 a596b957 2022-07-14 tracey strncmp(id_str, qs->commit, strlen(id_str)) != 0)
681 a596b957 2022-07-14 tracey continue;
683 a596b957 2022-07-14 tracey commit_found = 1;
685 a596b957 2022-07-14 tracey t->tag_count++;
688 a596b957 2022-07-14 tracey * check for one more commit before breaking,
689 a596b957 2022-07-14 tracey * so we know whether to navigate through briefs
690 a596b957 2022-07-14 tracey * commits and summary
692 a596b957 2022-07-14 tracey if (chk_next) {
693 a596b957 2022-07-14 tracey t->next_id = strdup(new_repo_tag->commit_id);
694 a596b957 2022-07-14 tracey if (t->next_id == NULL) {
695 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
696 a596b957 2022-07-14 tracey goto err;
698 a596b957 2022-07-14 tracey if (commit) {
699 a596b957 2022-07-14 tracey got_object_commit_close(commit);
700 a596b957 2022-07-14 tracey commit = NULL;
702 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
703 a596b957 2022-07-14 tracey gotweb_free_repo_tag(new_repo_tag);
704 a596b957 2022-07-14 tracey goto done;
707 a596b957 2022-07-14 tracey if (commit) {
708 625e5896 2022-09-01 op error = got_object_commit_get_logmsg(&tag_commit0,
710 a596b957 2022-07-14 tracey if (error)
712 a596b957 2022-07-14 tracey got_object_commit_close(commit);
713 a596b957 2022-07-14 tracey commit = NULL;
715 625e5896 2022-09-01 op tag_commit0 = strdup(got_object_tag_get_message(tag));
716 625e5896 2022-09-01 op if (tag_commit0 == NULL) {
717 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
722 625e5896 2022-09-01 op tag_commit = tag_commit0;
723 625e5896 2022-09-01 op while (*tag_commit == '\n')
724 625e5896 2022-09-01 op tag_commit++;
725 625e5896 2022-09-01 op new_repo_tag->tag_commit = strdup(tag_commit);
726 625e5896 2022-09-01 op if (new_repo_tag->tag_commit == NULL) {
727 625e5896 2022-09-01 op error = got_error_from_errno("strdup");
728 625e5896 2022-09-01 op free(tag_commit0);
731 625e5896 2022-09-01 op free(tag_commit0);
733 a596b957 2022-07-14 tracey if (qs->action != SUMMARY && qs->action != TAGS) {
734 a596b957 2022-07-14 tracey commit_msg = commit_msg0;
735 a596b957 2022-07-14 tracey while (*commit_msg == '\n')
736 a596b957 2022-07-14 tracey commit_msg++;
738 a596b957 2022-07-14 tracey new_repo_tag->commit_msg = strdup(commit_msg);
739 a596b957 2022-07-14 tracey if (new_repo_tag->commit_msg == NULL) {
740 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
741 a596b957 2022-07-14 tracey goto err;
745 a596b957 2022-07-14 tracey if (limit && --limit == 0) {
746 a596b957 2022-07-14 tracey if (chk_multi == 0)
748 a596b957 2022-07-14 tracey chk_next = 1;
754 a596b957 2022-07-14 tracey * we have tailq populated, so find previous commit id
755 a596b957 2022-07-14 tracey * for navigation through briefs and commits
757 a596b957 2022-07-14 tracey if (t->tag_count == 0) {
758 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
759 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
760 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
763 a596b957 2022-07-14 tracey if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
764 a596b957 2022-07-14 tracey commit_found = 0;
765 a596b957 2022-07-14 tracey TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
767 a596b957 2022-07-14 tracey if (commit_found == 0 && rt->commit_id != NULL &&
768 a596b957 2022-07-14 tracey strcmp(qs->commit, rt->commit_id) != 0) {
769 a596b957 2022-07-14 tracey continue;
771 a596b957 2022-07-14 tracey commit_found = 1;
772 a596b957 2022-07-14 tracey if (c_cnt == srv->max_commits_display ||
773 a596b957 2022-07-14 tracey rt == TAILQ_FIRST(&t->repo_tags)) {
774 a596b957 2022-07-14 tracey t->prev_id = strdup(rt->commit_id);
775 a596b957 2022-07-14 tracey if (t->prev_id == NULL)
776 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
783 a596b957 2022-07-14 tracey if (commit)
784 a596b957 2022-07-14 tracey got_object_commit_close(commit);
786 b163541d 2022-09-02 op got_object_tag_close(tag);
787 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
788 5a57034b 2022-09-02 op free(commit_msg0);
789 b163541d 2022-09-02 op free(in_repo_path);
790 a596b957 2022-07-14 tracey free(repo_path);
791 a596b957 2022-07-14 tracey free(id);
792 b163541d 2022-09-02 op free(id_str);
793 a596b957 2022-07-14 tracey return error;
796 a596b957 2022-07-14 tracey const struct got_error *
797 a596b957 2022-07-14 tracey got_output_repo_tree(struct request *c)
799 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
800 a596b957 2022-07-14 tracey struct transport *t = c->t;
801 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
802 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
803 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
804 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
805 a596b957 2022-07-14 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
806 a596b957 2022-07-14 tracey struct got_reflist_head refs;
807 a596b957 2022-07-14 tracey struct got_tree_object *tree = NULL;
808 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
809 8d02314f 2022-09-07 op const char *name, *folder;
810 e5e662e4 2022-09-02 op char *escaped_name = NULL, *path = NULL;
811 01498c42 2022-08-19 op int nentries, i, r;
813 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
815 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
817 a596b957 2022-07-14 tracey if (qs->folder != NULL) {
818 a596b957 2022-07-14 tracey path = strdup(qs->folder);
819 a596b957 2022-07-14 tracey if (path == NULL) {
820 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
821 a596b957 2022-07-14 tracey goto done;
824 6977f45a 2022-09-02 op error = got_repo_map_path(&path, repo, repo_dir->path);
825 a596b957 2022-07-14 tracey if (error)
826 a596b957 2022-07-14 tracey goto done;
829 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
830 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
831 a596b957 2022-07-14 tracey if (error)
832 a596b957 2022-07-14 tracey goto done;
834 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
835 a596b957 2022-07-14 tracey if (error)
836 a596b957 2022-07-14 tracey goto done;
838 a596b957 2022-07-14 tracey error = got_object_id_by_path(&tree_id, repo, commit, path);
839 a596b957 2022-07-14 tracey if (error)
840 a596b957 2022-07-14 tracey goto done;
842 a596b957 2022-07-14 tracey error = got_object_open_as_tree(&tree, repo, tree_id);
843 a596b957 2022-07-14 tracey if (error)
844 a596b957 2022-07-14 tracey goto done;
846 a596b957 2022-07-14 tracey nentries = got_object_tree_get_nentries(tree);
848 01498c42 2022-08-19 op folder = qs->folder ? qs->folder : "";
850 a596b957 2022-07-14 tracey for (i = 0; i < nentries; i++) {
851 2c6b5972 2022-09-02 op const char *modestr;
852 a596b957 2022-07-14 tracey struct got_tree_entry *te;
853 a596b957 2022-07-14 tracey mode_t mode;
855 a596b957 2022-07-14 tracey te = got_object_tree_get_entry(tree, i);
857 a596b957 2022-07-14 tracey mode = got_tree_entry_get_mode(te);
858 2c6b5972 2022-09-02 op if (got_object_tree_entry_is_submodule(te))
859 2c6b5972 2022-09-02 op modestr = "$";
860 2c6b5972 2022-09-02 op else if (S_ISLNK(mode))
861 2c6b5972 2022-09-02 op modestr = "@";
862 2c6b5972 2022-09-02 op else if (S_ISDIR(mode))
863 2c6b5972 2022-09-02 op modestr = "/";
864 2c6b5972 2022-09-02 op else if (mode & S_IXUSR)
865 2c6b5972 2022-09-02 op modestr = "*";
867 2c6b5972 2022-09-02 op modestr = "";
869 d927f8c8 2022-08-20 op name = got_tree_entry_get_name(te);
870 d927f8c8 2022-08-20 op error = gotweb_escape_html(&escaped_name, name);
874 a596b957 2022-07-14 tracey if (S_ISDIR(mode)) {
875 8d02314f 2022-09-07 op struct gotweb_url url = {
876 8d02314f 2022-09-07 op .index_page = -1,
878 8d02314f 2022-09-07 op .action = TREE,
879 8d02314f 2022-09-07 op .commit = rc->commit_id,
880 8d02314f 2022-09-07 op .path = qs->path,
881 8d02314f 2022-09-07 op /* `folder' is filled later */
883 8d02314f 2022-09-07 op char *path = NULL;
885 8d02314f 2022-09-07 op if (fcgi_printf(c,"<div class='tree_wrapper'>\n"
886 8d02314f 2022-09-07 op "<div class='tree_line'>") == -1)
889 8d02314f 2022-09-07 op if (asprintf(&path, "%s/%s", folder, name) == -1) {
890 8d02314f 2022-09-07 op error = got_error_from_errno("asprintf");
893 8d02314f 2022-09-07 op url.folder = path;
894 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "%s%s", escaped_name,
898 a596b957 2022-07-14 tracey goto done;
900 8d02314f 2022-09-07 op if (fcgi_printf(c, "</div>\n" /* .tree_line */
901 8d02314f 2022-09-07 op "<div class='tree_line_blank'> </div>\n"
902 8d02314f 2022-09-07 op "</div>\n") == -1)
905 8d02314f 2022-09-07 op struct gotweb_url url = {
906 8d02314f 2022-09-07 op .index_page = -1,
908 8d02314f 2022-09-07 op .path = qs->path,
909 8d02314f 2022-09-07 op .commit = rc->commit_id,
910 8d02314f 2022-09-07 op .folder = folder,
911 8d02314f 2022-09-07 op .file = name,
914 8d02314f 2022-09-07 op if (fcgi_printf(c, "<div class='tree_wrapper'>\n"
915 8d02314f 2022-09-07 op "<div class='tree_line'>") == -1)
918 8d02314f 2022-09-07 op url.action = BLOB;
919 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "%s%s", escaped_name,
922 a596b957 2022-07-14 tracey goto done;
924 8d02314f 2022-09-07 op if (fcgi_printf(c, "</div>\n" /* .tree_line */
925 8d02314f 2022-09-07 op "<div class='tree_line_blank'>") == -1)
928 8d02314f 2022-09-07 op url.action = COMMITS;
929 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "commits");
933 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
936 8d02314f 2022-09-07 op url.action = BLAME;
937 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "blame");
941 8d02314f 2022-09-07 op if (fcgi_printf(c,
942 8d02314f 2022-09-07 op "</div>\n" /* .tree_line_blank */
943 8d02314f 2022-09-07 op "</div>\n") == -1) /* .tree_wrapper */
946 b7efc9b3 2022-08-25 op free(escaped_name);
947 b7efc9b3 2022-08-25 op escaped_name = NULL;
950 b7efc9b3 2022-08-25 op free(escaped_name);
951 a596b957 2022-07-14 tracey free(path);
952 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
953 a596b957 2022-07-14 tracey if (commit)
954 a596b957 2022-07-14 tracey got_object_commit_close(commit);
956 58354f54 2022-09-06 op got_object_tree_close(tree);
957 a596b957 2022-07-14 tracey free(commit_id);
958 a596b957 2022-07-14 tracey free(tree_id);
959 a596b957 2022-07-14 tracey return error;
962 a596b957 2022-07-14 tracey const struct got_error *
963 a596b957 2022-07-14 tracey got_output_file_blob(struct request *c)
965 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
966 a596b957 2022-07-14 tracey struct transport *t = c->t;
967 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
968 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
969 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
970 a596b957 2022-07-14 tracey struct got_object_id *commit_id = NULL;
971 a596b957 2022-07-14 tracey struct got_reflist_head refs;
972 a596b957 2022-07-14 tracey struct got_blob_object *blob = NULL;
973 a596b957 2022-07-14 tracey char *path = NULL, *in_repo_path = NULL;
974 0b287d3f 2023-01-04 op int bin, obj_type, fd = -1;
976 a596b957 2022-07-14 tracey const uint8_t *buf;
978 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
980 a596b957 2022-07-14 tracey if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
981 a596b957 2022-07-14 tracey qs->folder ? "/" : "", qs->file) == -1) {
982 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
983 a596b957 2022-07-14 tracey goto done;
986 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, path);
987 a596b957 2022-07-14 tracey if (error)
988 a596b957 2022-07-14 tracey goto done;
990 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
991 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
992 a596b957 2022-07-14 tracey if (error)
993 a596b957 2022-07-14 tracey goto done;
995 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
996 a596b957 2022-07-14 tracey if (error)
997 a596b957 2022-07-14 tracey goto done;
999 a596b957 2022-07-14 tracey error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1000 a596b957 2022-07-14 tracey if (error)
1001 a596b957 2022-07-14 tracey goto done;
1003 a596b957 2022-07-14 tracey if (commit_id == NULL) {
1004 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_NO_OBJ);
1005 a596b957 2022-07-14 tracey goto done;
1008 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, commit_id);
1009 a596b957 2022-07-14 tracey if (error)
1010 a596b957 2022-07-14 tracey goto done;
1012 a596b957 2022-07-14 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
1013 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1014 a596b957 2022-07-14 tracey goto done;
1017 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1018 a596b957 2022-07-14 tracey if (error)
1019 a596b957 2022-07-14 tracey goto done;
1021 a596b957 2022-07-14 tracey error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1022 a596b957 2022-07-14 tracey if (error)
1023 a596b957 2022-07-14 tracey goto done;
1025 0b287d3f 2023-01-04 op error = got_object_blob_is_binary(&bin, blob);
1030 0b287d3f 2023-01-04 op error = gotweb_render_content_type_file(c,
1031 0b287d3f 2023-01-04 op "application/octet-stream", qs->file, NULL);
1033 0b287d3f 2023-01-04 op error = gotweb_render_content_type(c, "text/plain");
1035 0b287d3f 2023-01-04 op if (error) {
1036 0b287d3f 2023-01-04 op log_warnx("%s: %s", __func__, error->msg);
1041 a596b957 2022-07-14 tracey error = got_object_blob_read_block(&len, blob);
1042 a596b957 2022-07-14 tracey if (error)
1043 a596b957 2022-07-14 tracey goto done;
1044 0b287d3f 2023-01-04 op if (len == 0)
1046 a596b957 2022-07-14 tracey buf = got_object_blob_get_read_buf(blob);
1047 0b287d3f 2023-01-04 op fcgi_gen_binary_response(c, buf, len);
1050 a596b957 2022-07-14 tracey if (commit)
1051 a596b957 2022-07-14 tracey got_object_commit_close(commit);
1052 a596b957 2022-07-14 tracey if (fd != -1 && close(fd) == -1 && error == NULL)
1053 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1054 a596b957 2022-07-14 tracey if (blob)
1055 a596b957 2022-07-14 tracey got_object_blob_close(blob);
1056 a596b957 2022-07-14 tracey free(in_repo_path);
1057 a596b957 2022-07-14 tracey free(commit_id);
1058 a596b957 2022-07-14 tracey free(path);
1059 a596b957 2022-07-14 tracey return error;
1062 a596b957 2022-07-14 tracey struct blame_line {
1063 a596b957 2022-07-14 tracey int annotated;
1064 a596b957 2022-07-14 tracey char *id_str;
1065 a596b957 2022-07-14 tracey char *committer;
1066 a596b957 2022-07-14 tracey char datebuf[11]; /* YYYY-MM-DD + NUL */
1069 a596b957 2022-07-14 tracey struct blame_cb_args {
1070 a596b957 2022-07-14 tracey struct blame_line *lines;
1071 a596b957 2022-07-14 tracey int nlines;
1072 a596b957 2022-07-14 tracey int nlines_prec;
1073 a596b957 2022-07-14 tracey int lineno_cur;
1074 a596b957 2022-07-14 tracey off_t *line_offsets;
1075 a596b957 2022-07-14 tracey FILE *f;
1076 a596b957 2022-07-14 tracey struct got_repository *repo;
1077 a596b957 2022-07-14 tracey struct request *c;
1080 a596b957 2022-07-14 tracey static const struct got_error *
1081 a596b957 2022-07-14 tracey got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1082 a596b957 2022-07-14 tracey struct got_commit_object *commit, struct got_object_id *id)
1084 a596b957 2022-07-14 tracey const struct got_error *err = NULL;
1085 a596b957 2022-07-14 tracey struct blame_cb_args *a = arg;
1086 a596b957 2022-07-14 tracey struct blame_line *bline;
1087 a596b957 2022-07-14 tracey struct request *c = a->c;
1088 a596b957 2022-07-14 tracey struct transport *t = c->t;
1089 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
1090 a596b957 2022-07-14 tracey char *line = NULL, *eline = NULL;
1091 a596b957 2022-07-14 tracey size_t linesize = 0;
1092 a596b957 2022-07-14 tracey off_t offset;
1093 a596b957 2022-07-14 tracey struct tm tm;
1094 a596b957 2022-07-14 tracey time_t committer_time;
1097 a596b957 2022-07-14 tracey if (nlines != a->nlines ||
1098 a596b957 2022-07-14 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
1099 a596b957 2022-07-14 tracey return got_error(GOT_ERR_RANGE);
1101 a596b957 2022-07-14 tracey if (lineno == -1)
1102 a596b957 2022-07-14 tracey return NULL; /* no change in this commit */
1104 a596b957 2022-07-14 tracey /* Annotate this line. */
1105 a596b957 2022-07-14 tracey bline = &a->lines[lineno - 1];
1106 a596b957 2022-07-14 tracey if (bline->annotated)
1107 a596b957 2022-07-14 tracey return NULL;
1108 a596b957 2022-07-14 tracey err = got_object_id_str(&bline->id_str, id);
1109 a596b957 2022-07-14 tracey if (err)
1110 a596b957 2022-07-14 tracey return err;
1112 a596b957 2022-07-14 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
1113 a596b957 2022-07-14 tracey if (bline->committer == NULL) {
1114 a596b957 2022-07-14 tracey err = got_error_from_errno("strdup");
1115 a596b957 2022-07-14 tracey goto done;
1118 a596b957 2022-07-14 tracey committer_time = got_object_commit_get_committer_time(commit);
1119 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1120 a596b957 2022-07-14 tracey return got_error_from_errno("gmtime_r");
1121 a596b957 2022-07-14 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1122 a596b957 2022-07-14 tracey &tm) == 0) {
1123 a596b957 2022-07-14 tracey err = got_error(GOT_ERR_NO_SPACE);
1124 a596b957 2022-07-14 tracey goto done;
1126 a596b957 2022-07-14 tracey bline->annotated = 1;
1128 a596b957 2022-07-14 tracey /* Print lines annotated so far. */
1129 a596b957 2022-07-14 tracey bline = &a->lines[a->lineno_cur - 1];
1130 a596b957 2022-07-14 tracey if (!bline->annotated)
1131 a596b957 2022-07-14 tracey goto done;
1133 a596b957 2022-07-14 tracey offset = a->line_offsets[a->lineno_cur - 1];
1134 a596b957 2022-07-14 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
1135 a596b957 2022-07-14 tracey err = got_error_from_errno("fseeko");
1136 a596b957 2022-07-14 tracey goto done;
1139 85f2c2e0 2022-08-18 op while (a->lineno_cur <= a->nlines && bline->annotated) {
1140 a596b957 2022-07-14 tracey char *smallerthan, *at, *nl, *committer;
1141 a596b957 2022-07-14 tracey size_t len;
1143 a596b957 2022-07-14 tracey if (getline(&line, &linesize, a->f) == -1) {
1144 a596b957 2022-07-14 tracey if (ferror(a->f))
1145 a596b957 2022-07-14 tracey err = got_error_from_errno("getline");
1149 a596b957 2022-07-14 tracey committer = bline->committer;
1150 a596b957 2022-07-14 tracey smallerthan = strchr(committer, '<');
1151 a596b957 2022-07-14 tracey if (smallerthan && smallerthan[1] != '\0')
1152 a596b957 2022-07-14 tracey committer = smallerthan + 1;
1153 a596b957 2022-07-14 tracey at = strchr(committer, '@');
1155 a596b957 2022-07-14 tracey *at = '\0';
1156 a596b957 2022-07-14 tracey len = strlen(committer);
1157 a596b957 2022-07-14 tracey if (len >= 9)
1158 a596b957 2022-07-14 tracey committer[8] = '\0';
1160 a596b957 2022-07-14 tracey nl = strchr(line, '\n');
1162 a596b957 2022-07-14 tracey *nl = '\0';
1164 a596b957 2022-07-14 tracey err = gotweb_escape_html(&eline, line);
1165 a596b957 2022-07-14 tracey if (err)
1166 a596b957 2022-07-14 tracey goto done;
1168 8d02314f 2022-09-07 op if (fcgi_printf(c, "<div class='blame_wrapper'>"
1169 01498c42 2022-08-19 op "<div class='blame_number'>%.*d</div>"
1170 8d02314f 2022-09-07 op "<div class='blame_hash'>",
1171 8d02314f 2022-09-07 op a->nlines_prec, a->lineno_cur) == -1)
1174 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1175 8d02314f 2022-09-07 op .action = DIFF,
1176 8d02314f 2022-09-07 op .index_page = -1,
1178 8d02314f 2022-09-07 op .path = repo_dir->name,
1179 8d02314f 2022-09-07 op .commit = bline->id_str,
1180 8d02314f 2022-09-07 op }, "%.8s", bline->id_str);
1181 8d02314f 2022-09-07 op if (r == -1)
1184 8d02314f 2022-09-07 op r = fcgi_printf(c,
1186 01498c42 2022-08-19 op "<div class='blame_date'>%s</div>"
1187 01498c42 2022-08-19 op "<div class='blame_author'>%s</div>"
1188 01498c42 2022-08-19 op "<div class='blame_code'>%s</div>"
1189 01498c42 2022-08-19 op "</div>", /* .blame_wrapper */
1190 01498c42 2022-08-19 op bline->datebuf,
1193 01498c42 2022-08-19 op if (r == -1)
1194 a596b957 2022-07-14 tracey goto done;
1196 a596b957 2022-07-14 tracey a->lineno_cur++;
1197 a596b957 2022-07-14 tracey bline = &a->lines[a->lineno_cur - 1];
1199 8a4f92ca 2022-09-03 op free(eline);
1200 8a4f92ca 2022-09-03 op eline = NULL;
1203 a596b957 2022-07-14 tracey free(line);
1204 a596b957 2022-07-14 tracey free(eline);
1205 a596b957 2022-07-14 tracey return err;
1208 a596b957 2022-07-14 tracey const struct got_error *
1209 a596b957 2022-07-14 tracey got_output_file_blame(struct request *c)
1211 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1212 a596b957 2022-07-14 tracey struct transport *t = c->t;
1213 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1214 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
1215 a596b957 2022-07-14 tracey struct got_object_id *obj_id = NULL, *commit_id = NULL;
1216 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
1217 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1218 a596b957 2022-07-14 tracey struct got_blob_object *blob = NULL;
1219 a596b957 2022-07-14 tracey char *path = NULL, *in_repo_path = NULL;
1220 a596b957 2022-07-14 tracey struct blame_cb_args bca;
1221 a596b957 2022-07-14 tracey int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1222 a596b957 2022-07-14 tracey int fd6 = -1;
1223 a596b957 2022-07-14 tracey off_t filesize;
1224 a596b957 2022-07-14 tracey FILE *f1 = NULL, *f2 = NULL;
1226 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1227 a596b957 2022-07-14 tracey bca.f = NULL;
1228 a596b957 2022-07-14 tracey bca.lines = NULL;
1230 a596b957 2022-07-14 tracey if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1231 a596b957 2022-07-14 tracey qs->folder ? "/" : "", qs->file) == -1) {
1232 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1233 a596b957 2022-07-14 tracey goto done;
1236 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, path);
1237 a596b957 2022-07-14 tracey if (error)
1238 a596b957 2022-07-14 tracey goto done;
1240 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1241 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
1242 a596b957 2022-07-14 tracey if (error)
1243 a596b957 2022-07-14 tracey goto done;
1245 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
1246 a596b957 2022-07-14 tracey if (error)
1247 a596b957 2022-07-14 tracey goto done;
1249 a596b957 2022-07-14 tracey error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1250 a596b957 2022-07-14 tracey if (error)
1251 a596b957 2022-07-14 tracey goto done;
1253 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, obj_id);
1254 a596b957 2022-07-14 tracey if (error)
1255 a596b957 2022-07-14 tracey goto done;
1257 a596b957 2022-07-14 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
1258 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1259 a596b957 2022-07-14 tracey goto done;
1262 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1263 a596b957 2022-07-14 tracey if (error)
1264 a596b957 2022-07-14 tracey goto done;
1266 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1267 a596b957 2022-07-14 tracey if (error)
1268 a596b957 2022-07-14 tracey goto done;
1270 a596b957 2022-07-14 tracey error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1271 a596b957 2022-07-14 tracey if (error)
1272 a596b957 2022-07-14 tracey goto done;
1274 a596b957 2022-07-14 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1275 a596b957 2022-07-14 tracey &bca.line_offsets, bca.f, blob);
1276 a596b957 2022-07-14 tracey if (error || bca.nlines == 0)
1277 a596b957 2022-07-14 tracey goto done;
1279 a596b957 2022-07-14 tracey /* Don't include \n at EOF in the blame line count. */
1280 a596b957 2022-07-14 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
1281 a596b957 2022-07-14 tracey bca.nlines--;
1283 a596b957 2022-07-14 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1284 a596b957 2022-07-14 tracey if (bca.lines == NULL) {
1285 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1286 a596b957 2022-07-14 tracey goto done;
1288 a596b957 2022-07-14 tracey bca.lineno_cur = 1;
1289 a596b957 2022-07-14 tracey bca.nlines_prec = 0;
1290 a596b957 2022-07-14 tracey i = bca.nlines;
1291 a596b957 2022-07-14 tracey while (i > 0) {
1292 a596b957 2022-07-14 tracey i /= 10;
1293 a596b957 2022-07-14 tracey bca.nlines_prec++;
1295 a596b957 2022-07-14 tracey bca.repo = repo;
1296 a596b957 2022-07-14 tracey bca.c = c;
1298 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1299 a596b957 2022-07-14 tracey if (error)
1300 a596b957 2022-07-14 tracey goto done;
1302 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1303 a596b957 2022-07-14 tracey if (error)
1304 a596b957 2022-07-14 tracey goto done;
1306 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1307 a596b957 2022-07-14 tracey if (error)
1308 a596b957 2022-07-14 tracey goto done;
1310 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1311 a596b957 2022-07-14 tracey if (error)
1312 a596b957 2022-07-14 tracey goto done;
1314 a596b957 2022-07-14 tracey error = got_blame(in_repo_path, commit_id, repo,
1315 a596b957 2022-07-14 tracey GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1316 a596b957 2022-07-14 tracey fd3, fd4, f1, f2);
1319 a33a44db 2022-09-03 op if (bca.lines) {
1320 a596b957 2022-07-14 tracey free(bca.line_offsets);
1321 a596b957 2022-07-14 tracey for (i = 0; i < bca.nlines; i++) {
1322 a596b957 2022-07-14 tracey struct blame_line *bline = &bca.lines[i];
1323 a596b957 2022-07-14 tracey free(bline->id_str);
1324 a596b957 2022-07-14 tracey free(bline->committer);
1327 a596b957 2022-07-14 tracey free(bca.lines);
1328 a596b957 2022-07-14 tracey if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1329 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1330 a596b957 2022-07-14 tracey if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1331 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1332 a596b957 2022-07-14 tracey if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1333 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1334 a596b957 2022-07-14 tracey if (bca.f) {
1335 a596b957 2022-07-14 tracey const struct got_error *bca_err =
1336 a596b957 2022-07-14 tracey got_gotweb_flushfile(bca.f, fd1);
1337 a596b957 2022-07-14 tracey if (error == NULL)
1338 a596b957 2022-07-14 tracey error = bca_err;
1340 a596b957 2022-07-14 tracey if (f1) {
1341 a596b957 2022-07-14 tracey const struct got_error *f1_err =
1342 a596b957 2022-07-14 tracey got_gotweb_flushfile(f1, fd5);
1343 a596b957 2022-07-14 tracey if (error == NULL)
1344 a596b957 2022-07-14 tracey error = f1_err;
1346 a596b957 2022-07-14 tracey if (f2) {
1347 a596b957 2022-07-14 tracey const struct got_error *f2_err =
1348 a596b957 2022-07-14 tracey got_gotweb_flushfile(f2, fd6);
1349 a596b957 2022-07-14 tracey if (error == NULL)
1350 a596b957 2022-07-14 tracey error = f2_err;
1352 a596b957 2022-07-14 tracey if (commit)
1353 a596b957 2022-07-14 tracey got_object_commit_close(commit);
1354 a596b957 2022-07-14 tracey if (blob)
1355 a596b957 2022-07-14 tracey got_object_blob_close(blob);
1356 a596b957 2022-07-14 tracey free(in_repo_path);
1357 a596b957 2022-07-14 tracey free(commit_id);
1358 b94206d0 2022-09-03 op free(obj_id);
1359 a596b957 2022-07-14 tracey free(path);
1360 b94206d0 2022-09-03 op got_ref_list_free(&refs);
1361 a596b957 2022-07-14 tracey return error;
1364 a596b957 2022-07-14 tracey const struct got_error *
1365 a596b957 2022-07-14 tracey got_output_repo_diff(struct request *c)
1367 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1368 a596b957 2022-07-14 tracey struct transport *t = c->t;
1369 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1370 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
1371 a596b957 2022-07-14 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1372 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1373 a596b957 2022-07-14 tracey FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1374 a596b957 2022-07-14 tracey char *label1 = NULL, *label2 = NULL, *line = NULL;
1375 a596b957 2022-07-14 tracey char *newline, *eline = NULL, *color = NULL;
1376 a596b957 2022-07-14 tracey int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1377 a596b957 2022-07-14 tracey size_t linesize = 0;
1378 a596b957 2022-07-14 tracey ssize_t linelen;
1379 a596b957 2022-07-14 tracey int wrlen = 0;
1381 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1383 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1384 a596b957 2022-07-14 tracey if (error)
1385 a596b957 2022-07-14 tracey return error;
1387 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1388 a596b957 2022-07-14 tracey if (error)
1389 a596b957 2022-07-14 tracey return error;
1391 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1392 a596b957 2022-07-14 tracey if (error)
1393 a596b957 2022-07-14 tracey return error;
1395 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
1397 a596b957 2022-07-14 tracey if (rc->parent_id != NULL &&
1398 a596b957 2022-07-14 tracey strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1399 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&id1, &label1,
1400 a596b957 2022-07-14 tracey rc->parent_id, GOT_OBJ_TYPE_ANY,
1401 a596b957 2022-07-14 tracey &refs, repo);
1402 a596b957 2022-07-14 tracey if (error)
1403 a596b957 2022-07-14 tracey goto done;
1406 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1407 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_ANY, &refs, repo);
1408 a596b957 2022-07-14 tracey if (error)
1409 a596b957 2022-07-14 tracey goto done;
1411 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, id2);
1412 a596b957 2022-07-14 tracey if (error)
1413 a596b957 2022-07-14 tracey goto done;
1415 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1416 a596b957 2022-07-14 tracey if (error)
1417 a596b957 2022-07-14 tracey goto done;
1419 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1420 a596b957 2022-07-14 tracey if (error)
1421 a596b957 2022-07-14 tracey goto done;
1423 a596b957 2022-07-14 tracey switch (obj_type) {
1424 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_BLOB:
1425 a596b957 2022-07-14 tracey error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1426 a596b957 2022-07-14 tracey id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1427 a596b957 2022-07-14 tracey repo, f3);
1429 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_TREE:
1430 a596b957 2022-07-14 tracey error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1431 a596b957 2022-07-14 tracey id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1432 a596b957 2022-07-14 tracey repo, f3);
1434 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_COMMIT:
1435 a596b957 2022-07-14 tracey error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1436 a596b957 2022-07-14 tracey fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1437 a596b957 2022-07-14 tracey repo, f3);
1439 a596b957 2022-07-14 tracey default:
1440 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1442 a596b957 2022-07-14 tracey if (error)
1443 a596b957 2022-07-14 tracey goto done;
1445 a596b957 2022-07-14 tracey if (fseek(f1, 0, SEEK_SET) == -1) {
1446 a596b957 2022-07-14 tracey error = got_ferror(f1, GOT_ERR_IO);
1447 a596b957 2022-07-14 tracey goto done;
1450 a596b957 2022-07-14 tracey if (fseek(f2, 0, SEEK_SET) == -1) {
1451 a596b957 2022-07-14 tracey error = got_ferror(f2, GOT_ERR_IO);
1452 a596b957 2022-07-14 tracey goto done;
1455 a596b957 2022-07-14 tracey if (fseek(f3, 0, SEEK_SET) == -1) {
1456 a596b957 2022-07-14 tracey error = got_ferror(f3, GOT_ERR_IO);
1457 a596b957 2022-07-14 tracey goto done;
1460 a596b957 2022-07-14 tracey while ((linelen = getline(&line, &linesize, f3)) != -1) {
1461 a596b957 2022-07-14 tracey if (strncmp(line, "-", 1) == 0) {
1462 a596b957 2022-07-14 tracey color = strdup("diff_minus");
1463 a596b957 2022-07-14 tracey if (color == NULL) {
1464 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1465 a596b957 2022-07-14 tracey goto done;
1467 a596b957 2022-07-14 tracey } else if (strncmp(line, "+", 1) == 0) {
1468 a596b957 2022-07-14 tracey color = strdup("diff_plus");
1469 a596b957 2022-07-14 tracey if (color == NULL) {
1470 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1471 a596b957 2022-07-14 tracey goto done;
1473 a596b957 2022-07-14 tracey } else if (strncmp(line, "@@", 2) == 0) {
1474 a596b957 2022-07-14 tracey color = strdup("diff_chunk_header");
1475 a596b957 2022-07-14 tracey if (color == NULL) {
1476 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1477 a596b957 2022-07-14 tracey goto done;
1479 a596b957 2022-07-14 tracey } else if (strncmp(line, "@@", 2) == 0) {
1480 a596b957 2022-07-14 tracey color = strdup("diff_chunk_header");
1481 a596b957 2022-07-14 tracey if (color == NULL) {
1482 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1483 a596b957 2022-07-14 tracey goto done;
1485 a596b957 2022-07-14 tracey } else if (strncmp(line, "commit +", 8) == 0) {
1486 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1487 a596b957 2022-07-14 tracey if (color == NULL) {
1488 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1489 a596b957 2022-07-14 tracey goto done;
1491 a596b957 2022-07-14 tracey } else if (strncmp(line, "commit -", 8) == 0) {
1492 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1493 a596b957 2022-07-14 tracey if (color == NULL) {
1494 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1495 a596b957 2022-07-14 tracey goto done;
1497 a596b957 2022-07-14 tracey } else if (strncmp(line, "blob +", 6) == 0) {
1498 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1499 a596b957 2022-07-14 tracey if (color == NULL) {
1500 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1501 a596b957 2022-07-14 tracey goto done;
1503 a596b957 2022-07-14 tracey } else if (strncmp(line, "blob -", 6) == 0) {
1504 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1505 a596b957 2022-07-14 tracey if (color == NULL) {
1506 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1507 a596b957 2022-07-14 tracey goto done;
1509 a596b957 2022-07-14 tracey } else if (strncmp(line, "file +", 6) == 0) {
1510 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1511 a596b957 2022-07-14 tracey if (color == NULL) {
1512 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1513 a596b957 2022-07-14 tracey goto done;
1515 a596b957 2022-07-14 tracey } else if (strncmp(line, "file -", 6) == 0) {
1516 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1517 a596b957 2022-07-14 tracey if (color == NULL) {
1518 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1519 a596b957 2022-07-14 tracey goto done;
1521 a596b957 2022-07-14 tracey } else if (strncmp(line, "from:", 5) == 0) {
1522 a596b957 2022-07-14 tracey color = strdup("diff_author");
1523 a596b957 2022-07-14 tracey if (color == NULL) {
1524 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1525 a596b957 2022-07-14 tracey goto done;
1527 a596b957 2022-07-14 tracey } else if (strncmp(line, "via:", 4) == 0) {
1528 a596b957 2022-07-14 tracey color = strdup("diff_author");
1529 a596b957 2022-07-14 tracey if (color == NULL) {
1530 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1531 a596b957 2022-07-14 tracey goto done;
1533 a596b957 2022-07-14 tracey } else if (strncmp(line, "date:", 5) == 0) {
1534 a596b957 2022-07-14 tracey color = strdup("diff_date");
1535 a596b957 2022-07-14 tracey if (color == NULL) {
1536 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1537 a596b957 2022-07-14 tracey goto done;
1541 a596b957 2022-07-14 tracey newline = strchr(line, '\n');
1542 a596b957 2022-07-14 tracey if (newline)
1543 a596b957 2022-07-14 tracey *newline = '\0';
1545 a596b957 2022-07-14 tracey error = gotweb_escape_html(&eline, line);
1546 a596b957 2022-07-14 tracey if (error)
1547 a596b957 2022-07-14 tracey goto done;
1549 01498c42 2022-08-19 op fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1550 01498c42 2022-08-19 op color ? color : "", eline);
1551 a596b957 2022-07-14 tracey free(eline);
1552 a596b957 2022-07-14 tracey eline = NULL;
1554 a596b957 2022-07-14 tracey if (linelen > 0)
1555 a596b957 2022-07-14 tracey wrlen = wrlen + linelen;
1556 a596b957 2022-07-14 tracey free(color);
1557 a596b957 2022-07-14 tracey color = NULL;
1559 a596b957 2022-07-14 tracey if (linelen == -1 && ferror(f3))
1560 a596b957 2022-07-14 tracey error = got_error_from_errno("getline");
1562 a596b957 2022-07-14 tracey free(color);
1563 a596b957 2022-07-14 tracey if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1564 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1565 a596b957 2022-07-14 tracey if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1566 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1567 a596b957 2022-07-14 tracey if (f1) {
1568 a596b957 2022-07-14 tracey const struct got_error *f1_err =
1569 a596b957 2022-07-14 tracey got_gotweb_flushfile(f1, fd1);
1570 a596b957 2022-07-14 tracey if (error == NULL)
1571 a596b957 2022-07-14 tracey error = f1_err;
1573 a596b957 2022-07-14 tracey if (f2) {
1574 a596b957 2022-07-14 tracey const struct got_error *f2_err =
1575 a596b957 2022-07-14 tracey got_gotweb_flushfile(f2, fd2);
1576 a596b957 2022-07-14 tracey if (error == NULL)
1577 a596b957 2022-07-14 tracey error = f2_err;
1579 a596b957 2022-07-14 tracey if (f3) {
1580 a596b957 2022-07-14 tracey const struct got_error *f3_err =
1581 a596b957 2022-07-14 tracey got_gotweb_flushfile(f3, fd3);
1582 a596b957 2022-07-14 tracey if (error == NULL)
1583 a596b957 2022-07-14 tracey error = f3_err;
1585 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
1586 a596b957 2022-07-14 tracey free(line);
1587 a596b957 2022-07-14 tracey free(eline);
1588 a596b957 2022-07-14 tracey free(label1);
1589 a596b957 2022-07-14 tracey free(label2);
1590 a596b957 2022-07-14 tracey free(id1);
1591 a596b957 2022-07-14 tracey free(id2);
1592 a596b957 2022-07-14 tracey return error;
1595 a596b957 2022-07-14 tracey static const struct got_error *
1596 a596b957 2022-07-14 tracey got_init_repo_commit(struct repo_commit **rc)
1598 a596b957 2022-07-14 tracey *rc = calloc(1, sizeof(**rc));
1599 a596b957 2022-07-14 tracey if (*rc == NULL)
1600 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
1602 a596b957 2022-07-14 tracey (*rc)->path = NULL;
1603 a596b957 2022-07-14 tracey (*rc)->refs_str = NULL;
1604 a596b957 2022-07-14 tracey (*rc)->commit_id = NULL;
1605 a596b957 2022-07-14 tracey (*rc)->committer = NULL;
1606 a596b957 2022-07-14 tracey (*rc)->author = NULL;
1607 a596b957 2022-07-14 tracey (*rc)->parent_id = NULL;
1608 a596b957 2022-07-14 tracey (*rc)->tree_id = NULL;
1609 a596b957 2022-07-14 tracey (*rc)->commit_msg = NULL;
1611 dd84f505 2022-08-31 stsp return NULL;
1614 a596b957 2022-07-14 tracey static const struct got_error *
1615 a596b957 2022-07-14 tracey got_init_repo_tag(struct repo_tag **rt)
1617 a596b957 2022-07-14 tracey *rt = calloc(1, sizeof(**rt));
1618 a596b957 2022-07-14 tracey if (*rt == NULL)
1619 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
1621 a596b957 2022-07-14 tracey (*rt)->commit_id = NULL;
1622 a596b957 2022-07-14 tracey (*rt)->tag_name = NULL;
1623 a596b957 2022-07-14 tracey (*rt)->tag_commit = NULL;
1624 a596b957 2022-07-14 tracey (*rt)->commit_msg = NULL;
1625 a596b957 2022-07-14 tracey (*rt)->tagger = NULL;
1627 dd84f505 2022-08-31 stsp return NULL;