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 int
56 a596b957 2022-07-14 tracey isbinary(const uint8_t *buf, size_t n)
58 d4d45e43 2022-07-28 op return memchr(buf, '\0', n) != NULL;
62 a596b957 2022-07-14 tracey static const struct got_error *
63 a596b957 2022-07-14 tracey got_gotweb_flushfile(FILE *f, int fd)
65 a596b957 2022-07-14 tracey if (fseek(f, 0, SEEK_SET) == -1)
66 a596b957 2022-07-14 tracey return got_error_from_errno("fseek");
68 a596b957 2022-07-14 tracey if (ftruncate(fd, 0) == -1)
69 a596b957 2022-07-14 tracey return got_error_from_errno("ftruncate");
71 a596b957 2022-07-14 tracey if (fsync(fd) == -1)
72 a596b957 2022-07-14 tracey return got_error_from_errno("fsync");
74 a596b957 2022-07-14 tracey if (f && fclose(f) == EOF)
75 a596b957 2022-07-14 tracey return got_error_from_errno("fclose");
77 a596b957 2022-07-14 tracey if (fd != -1 && close(fd) != -1)
78 a596b957 2022-07-14 tracey return got_error_from_errno("close");
80 a596b957 2022-07-14 tracey return NULL;
83 a596b957 2022-07-14 tracey static const struct got_error *
84 a596b957 2022-07-14 tracey got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
86 a596b957 2022-07-14 tracey *fd = dup(*priv_fd);
87 e9d3ad59 2022-08-31 stsp if (*fd == -1)
88 e9d3ad59 2022-08-31 stsp return got_error_from_errno("dup");
90 a596b957 2022-07-14 tracey *f = fdopen(*fd, "w+");
91 a596b957 2022-07-14 tracey if (*f == NULL) {
92 a596b957 2022-07-14 tracey close(*fd);
93 dd84f505 2022-08-31 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
96 dd84f505 2022-08-31 stsp return NULL;
99 a596b957 2022-07-14 tracey static const struct got_error *
100 a596b957 2022-07-14 tracey got_gotweb_dupfd(int *priv_fd, int *fd)
102 a596b957 2022-07-14 tracey *fd = dup(*priv_fd);
104 a596b957 2022-07-14 tracey if (*fd < 0)
105 a596b957 2022-07-14 tracey return NULL;
107 dd84f505 2022-08-31 stsp return NULL;
110 a596b957 2022-07-14 tracey const struct got_error *
111 a596b957 2022-07-14 tracey got_get_repo_owner(char **owner, struct request *c, char *dir)
113 a596b957 2022-07-14 tracey struct server *srv = c->srv;
114 a596b957 2022-07-14 tracey struct transport *t = c->t;
115 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
116 a596b957 2022-07-14 tracey const char *gitconfig_owner;
118 a596b957 2022-07-14 tracey *owner = NULL;
120 a596b957 2022-07-14 tracey if (srv->show_repo_owner == 0)
121 a596b957 2022-07-14 tracey return NULL;
123 a596b957 2022-07-14 tracey gitconfig_owner = got_repo_get_gitconfig_owner(repo);
124 a596b957 2022-07-14 tracey if (gitconfig_owner) {
125 a596b957 2022-07-14 tracey *owner = strdup(gitconfig_owner);
126 1999985f 2022-08-30 tracey if (*owner == NULL)
127 1999985f 2022-08-30 tracey return got_error_from_errno("strdup");
129 1999985f 2022-08-30 tracey *owner = strdup("");
130 a596b957 2022-07-14 tracey if (*owner == NULL)
131 a596b957 2022-07-14 tracey return got_error_from_errno("strdup");
133 dd84f505 2022-08-31 stsp return NULL;
136 a596b957 2022-07-14 tracey const struct got_error *
137 a596b957 2022-07-14 tracey got_get_repo_age(char **repo_age, struct request *c, char *dir,
138 a596b957 2022-07-14 tracey const char *refname, int ref_tm)
140 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
141 a596b957 2022-07-14 tracey struct server *srv = c->srv;
142 a596b957 2022-07-14 tracey struct transport *t = c->t;
143 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
144 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
145 a596b957 2022-07-14 tracey struct got_reflist_head refs;
146 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
147 a596b957 2022-07-14 tracey time_t committer_time = 0, cmp_time = 0;
149 a596b957 2022-07-14 tracey *repo_age = NULL;
150 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
152 a596b957 2022-07-14 tracey if (srv->show_repo_age == 0)
153 a596b957 2022-07-14 tracey return NULL;
155 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/heads",
156 a596b957 2022-07-14 tracey got_ref_cmp_by_name, NULL);
157 a596b957 2022-07-14 tracey if (error)
158 a596b957 2022-07-14 tracey goto done;
161 a596b957 2022-07-14 tracey * Find the youngest branch tip in the repository, or the age of
162 a596b957 2022-07-14 tracey * the a specific branch tip if a name was provided by the caller.
164 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
165 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
167 a596b957 2022-07-14 tracey if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
168 a596b957 2022-07-14 tracey continue;
170 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, re->ref);
171 a596b957 2022-07-14 tracey if (error)
172 a596b957 2022-07-14 tracey goto done;
174 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
175 a596b957 2022-07-14 tracey free(id);
176 a596b957 2022-07-14 tracey if (error)
177 a596b957 2022-07-14 tracey goto done;
179 a596b957 2022-07-14 tracey committer_time =
180 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
181 a596b957 2022-07-14 tracey got_object_commit_close(commit);
182 a596b957 2022-07-14 tracey if (cmp_time < committer_time)
183 a596b957 2022-07-14 tracey cmp_time = committer_time;
185 a596b957 2022-07-14 tracey if (refname)
189 a596b957 2022-07-14 tracey if (cmp_time != 0) {
190 a596b957 2022-07-14 tracey committer_time = cmp_time;
191 a596b957 2022-07-14 tracey error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
194 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
195 a596b957 2022-07-14 tracey return error;
198 a596b957 2022-07-14 tracey static const struct got_error *
199 a596b957 2022-07-14 tracey got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
200 a596b957 2022-07-14 tracey struct got_commit_object *commit, struct got_reflist_head *refs,
201 a596b957 2022-07-14 tracey struct got_object_id *id)
203 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
204 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
205 a596b957 2022-07-14 tracey struct got_object_id *id2 = NULL;
206 a596b957 2022-07-14 tracey struct got_object_qid *parent_id;
207 a596b957 2022-07-14 tracey struct transport *t = c->t;
208 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
209 a596b957 2022-07-14 tracey char *commit_msg = NULL, *commit_msg0;
211 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, refs, entry) {
213 a596b957 2022-07-14 tracey const char *name;
214 a596b957 2022-07-14 tracey struct got_tag_object *tag = NULL;
215 a596b957 2022-07-14 tracey struct got_object_id *ref_id;
218 a596b957 2022-07-14 tracey if (got_ref_is_symbolic(re->ref))
219 a596b957 2022-07-14 tracey continue;
221 a596b957 2022-07-14 tracey name = got_ref_get_name(re->ref);
222 a596b957 2022-07-14 tracey if (strncmp(name, "refs/", 5) == 0)
223 a596b957 2022-07-14 tracey name += 5;
224 a596b957 2022-07-14 tracey if (strncmp(name, "got/", 4) == 0)
225 a596b957 2022-07-14 tracey continue;
226 a596b957 2022-07-14 tracey if (strncmp(name, "heads/", 6) == 0)
227 a596b957 2022-07-14 tracey name += 6;
228 a596b957 2022-07-14 tracey if (strncmp(name, "remotes/", 8) == 0) {
229 a596b957 2022-07-14 tracey name += 8;
230 a596b957 2022-07-14 tracey s = strstr(name, "/" GOT_REF_HEAD);
231 a596b957 2022-07-14 tracey if (s != NULL && s[strlen(s)] == '\0')
232 a596b957 2022-07-14 tracey continue;
234 a596b957 2022-07-14 tracey error = got_ref_resolve(&ref_id, t->repo, re->ref);
235 a596b957 2022-07-14 tracey if (error)
236 a596b957 2022-07-14 tracey return error;
237 a596b957 2022-07-14 tracey if (strncmp(name, "tags/", 5) == 0) {
238 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, t->repo, ref_id);
239 a596b957 2022-07-14 tracey if (error) {
240 a596b957 2022-07-14 tracey if (error->code != GOT_ERR_OBJ_TYPE) {
241 a596b957 2022-07-14 tracey free(ref_id);
242 a596b957 2022-07-14 tracey continue;
245 a596b957 2022-07-14 tracey * Ref points at something other
246 a596b957 2022-07-14 tracey * than a tag.
248 a596b957 2022-07-14 tracey error = NULL;
249 a596b957 2022-07-14 tracey tag = NULL;
252 a596b957 2022-07-14 tracey cmp = got_object_id_cmp(tag ?
253 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag) : ref_id, id);
254 a596b957 2022-07-14 tracey free(ref_id);
256 a596b957 2022-07-14 tracey got_object_tag_close(tag);
257 a596b957 2022-07-14 tracey if (cmp != 0)
258 a596b957 2022-07-14 tracey continue;
259 a596b957 2022-07-14 tracey s = repo_commit->refs_str;
260 a596b957 2022-07-14 tracey if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
261 a596b957 2022-07-14 tracey s ? ", " : "", name) == -1) {
262 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
264 a596b957 2022-07-14 tracey repo_commit->refs_str = NULL;
265 a596b957 2022-07-14 tracey return error;
270 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->commit_id, id);
271 a596b957 2022-07-14 tracey if (error)
272 a596b957 2022-07-14 tracey return error;
274 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->tree_id,
275 a596b957 2022-07-14 tracey got_object_commit_get_tree_id(commit));
276 a596b957 2022-07-14 tracey if (error)
277 a596b957 2022-07-14 tracey return error;
279 a596b957 2022-07-14 tracey if (qs->action == DIFF) {
280 a596b957 2022-07-14 tracey parent_id = STAILQ_FIRST(
281 a596b957 2022-07-14 tracey got_object_commit_get_parent_ids(commit));
282 a596b957 2022-07-14 tracey if (parent_id != NULL) {
283 a596b957 2022-07-14 tracey id2 = got_object_id_dup(&parent_id->id);
284 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->parent_id, id2);
285 a596b957 2022-07-14 tracey if (error)
286 a596b957 2022-07-14 tracey return error;
287 a596b957 2022-07-14 tracey free(id2);
289 a596b957 2022-07-14 tracey repo_commit->parent_id = strdup("/dev/null");
290 a596b957 2022-07-14 tracey if (repo_commit->parent_id == NULL) {
291 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
292 a596b957 2022-07-14 tracey return error;
297 a596b957 2022-07-14 tracey repo_commit->committer_time =
298 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
300 a596b957 2022-07-14 tracey repo_commit->author =
301 a596b957 2022-07-14 tracey strdup(got_object_commit_get_author(commit));
302 a596b957 2022-07-14 tracey if (repo_commit->author == NULL) {
303 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
304 a596b957 2022-07-14 tracey return error;
306 a596b957 2022-07-14 tracey repo_commit->committer =
307 a596b957 2022-07-14 tracey strdup(got_object_commit_get_committer(commit));
308 a596b957 2022-07-14 tracey if (repo_commit->committer == NULL) {
309 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
310 a596b957 2022-07-14 tracey return error;
312 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
313 a596b957 2022-07-14 tracey if (error)
314 a596b957 2022-07-14 tracey return error;
316 a596b957 2022-07-14 tracey commit_msg = commit_msg0;
317 a596b957 2022-07-14 tracey while (*commit_msg == '\n')
318 a596b957 2022-07-14 tracey commit_msg++;
320 a596b957 2022-07-14 tracey repo_commit->commit_msg = strdup(commit_msg);
321 a596b957 2022-07-14 tracey if (repo_commit->commit_msg == NULL)
322 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
323 a596b957 2022-07-14 tracey free(commit_msg0);
324 a596b957 2022-07-14 tracey return error;
327 a596b957 2022-07-14 tracey const struct got_error *
328 a596b957 2022-07-14 tracey got_get_repo_commits(struct request *c, int limit)
330 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
331 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
332 a596b957 2022-07-14 tracey struct got_commit_graph *graph = NULL;
333 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
334 a596b957 2022-07-14 tracey struct got_reflist_head refs;
335 a596b957 2022-07-14 tracey struct got_reference *ref;
336 a596b957 2022-07-14 tracey struct repo_commit *repo_commit = NULL;
337 a596b957 2022-07-14 tracey struct server *srv = c->srv;
338 a596b957 2022-07-14 tracey struct transport *t = c->t;
339 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
340 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
341 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
342 a596b957 2022-07-14 tracey char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
343 a596b957 2022-07-14 tracey int chk_next = 0, chk_multi = 0, commit_found = 0;
344 a596b957 2022-07-14 tracey int obj_type, limit_chk = 0;
346 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
348 a596b957 2022-07-14 tracey if (qs->file != NULL && strlen(qs->file) > 0)
349 a596b957 2022-07-14 tracey if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
350 a596b957 2022-07-14 tracey qs->file) == -1)
351 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
353 a596b957 2022-07-14 tracey if (asprintf(&repo_path, "%s/%s", srv->repos_path,
354 a596b957 2022-07-14 tracey repo_dir->name) == -1)
355 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
357 a596b957 2022-07-14 tracey error = got_init_repo_commit(&repo_commit);
358 a596b957 2022-07-14 tracey if (error)
359 a596b957 2022-07-14 tracey return error;
362 a596b957 2022-07-14 tracey * XXX: jumping directly to a commit id via
363 a596b957 2022-07-14 tracey * got_repo_match_object_id_prefix significantly improves performance,
364 a596b957 2022-07-14 tracey * but does not allow us to create a PREVIOUS button, since commits can
365 a596b957 2022-07-14 tracey * only be itereated forward. So, we have to match as we iterate from
366 a596b957 2022-07-14 tracey * the headref.
368 a596b957 2022-07-14 tracey if (qs->action == BRIEFS || qs->action == COMMITS ||
369 a596b957 2022-07-14 tracey (qs->action == TREE && qs->commit == NULL)) {
370 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->headref, 0);
371 a596b957 2022-07-14 tracey if (error)
372 a596b957 2022-07-14 tracey goto done;
374 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
375 a596b957 2022-07-14 tracey got_ref_close(ref);
376 a596b957 2022-07-14 tracey if (error)
377 a596b957 2022-07-14 tracey goto done;
378 a596b957 2022-07-14 tracey } else if (qs->commit != NULL) {
379 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->commit, 0);
380 a596b957 2022-07-14 tracey if (error == NULL) {
381 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
382 a596b957 2022-07-14 tracey if (error)
383 a596b957 2022-07-14 tracey goto done;
384 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, id);
385 a596b957 2022-07-14 tracey got_ref_close(ref);
386 a596b957 2022-07-14 tracey if (error)
387 a596b957 2022-07-14 tracey goto done;
388 a596b957 2022-07-14 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
389 a596b957 2022-07-14 tracey struct got_tag_object *tag;
390 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, repo, id);
391 a596b957 2022-07-14 tracey if (error)
392 a596b957 2022-07-14 tracey goto done;
393 a596b957 2022-07-14 tracey if (got_object_tag_get_object_type(tag) !=
394 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT) {
395 a596b957 2022-07-14 tracey got_object_tag_close(tag);
396 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
397 a596b957 2022-07-14 tracey goto done;
399 a596b957 2022-07-14 tracey free(id);
400 a596b957 2022-07-14 tracey id = got_object_id_dup(
401 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag));
402 a596b957 2022-07-14 tracey if (id == NULL)
403 a596b957 2022-07-14 tracey error = got_error_from_errno(
404 a596b957 2022-07-14 tracey "got_object_id_dup");
405 a596b957 2022-07-14 tracey got_object_tag_close(tag);
406 a596b957 2022-07-14 tracey if (error)
407 a596b957 2022-07-14 tracey goto done;
408 a596b957 2022-07-14 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
409 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
410 a596b957 2022-07-14 tracey goto done;
413 a596b957 2022-07-14 tracey error = got_repo_match_object_id_prefix(&id, qs->commit,
414 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, repo);
415 a596b957 2022-07-14 tracey if (error)
416 a596b957 2022-07-14 tracey goto done;
419 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, repo_path);
420 a596b957 2022-07-14 tracey if (error)
421 a596b957 2022-07-14 tracey goto done;
423 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
424 a596b957 2022-07-14 tracey if (error)
425 a596b957 2022-07-14 tracey goto done;
427 a596b957 2022-07-14 tracey if (qs->file != NULL && strlen(qs->file) > 0) {
428 a596b957 2022-07-14 tracey error = got_commit_graph_open(&graph, file_path, 0);
429 a596b957 2022-07-14 tracey if (error)
430 a596b957 2022-07-14 tracey goto done;
432 a596b957 2022-07-14 tracey error = got_commit_graph_open(&graph, in_repo_path, 0);
433 a596b957 2022-07-14 tracey if (error)
434 a596b957 2022-07-14 tracey goto done;
437 a596b957 2022-07-14 tracey error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
438 a596b957 2022-07-14 tracey if (error)
439 a596b957 2022-07-14 tracey goto done;
441 a596b957 2022-07-14 tracey for (;;) {
442 a596b957 2022-07-14 tracey if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
443 a596b957 2022-07-14 tracey commit_found == 0 && repo_commit->commit_id != NULL) {
444 a596b957 2022-07-14 tracey t->prev_id = strdup(repo_commit->commit_id);
445 a596b957 2022-07-14 tracey if (t->prev_id == NULL) {
446 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
447 a596b957 2022-07-14 tracey goto done;
451 a596b957 2022-07-14 tracey error = got_commit_graph_iter_next(&id, graph, repo, NULL,
453 a596b957 2022-07-14 tracey if (error) {
454 a596b957 2022-07-14 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
455 a596b957 2022-07-14 tracey error = NULL;
456 a596b957 2022-07-14 tracey goto done;
458 a596b957 2022-07-14 tracey if (id == NULL)
459 a596b957 2022-07-14 tracey goto done;
461 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
462 a596b957 2022-07-14 tracey if (error)
463 a596b957 2022-07-14 tracey goto done;
465 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
467 a596b957 2022-07-14 tracey if (error)
468 a596b957 2022-07-14 tracey goto done;
470 a596b957 2022-07-14 tracey error = got_get_repo_commit(c, repo_commit, commit,
471 a596b957 2022-07-14 tracey &refs, id);
472 a596b957 2022-07-14 tracey if (error)
473 a596b957 2022-07-14 tracey goto done;
475 a596b957 2022-07-14 tracey if (qs->commit != NULL && commit_found == 0 && limit != 1) {
476 a596b957 2022-07-14 tracey if (strcmp(qs->commit, repo_commit->commit_id) == 0)
477 a596b957 2022-07-14 tracey commit_found = 1;
478 a596b957 2022-07-14 tracey else if (qs->file != NULL && strlen(qs->file) > 0 &&
479 a596b957 2022-07-14 tracey qs->page == 0)
480 a596b957 2022-07-14 tracey commit_found = 1;
482 a596b957 2022-07-14 tracey limit_chk++;
483 a596b957 2022-07-14 tracey free(id);
484 a596b957 2022-07-14 tracey id = NULL;
485 a596b957 2022-07-14 tracey continue;
489 a596b957 2022-07-14 tracey struct repo_commit *new_repo_commit = NULL;
490 a596b957 2022-07-14 tracey error = got_init_repo_commit(&new_repo_commit);
491 a596b957 2022-07-14 tracey if (error)
492 a596b957 2022-07-14 tracey goto done;
494 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&t->repo_commits, new_repo_commit, entry);
496 a596b957 2022-07-14 tracey error = got_get_repo_commit(c, new_repo_commit, commit,
497 a596b957 2022-07-14 tracey &refs, id);
498 a596b957 2022-07-14 tracey if (error)
499 a596b957 2022-07-14 tracey goto done;
501 a596b957 2022-07-14 tracey free(id);
502 a596b957 2022-07-14 tracey id = NULL;
504 a596b957 2022-07-14 tracey if (limit == 1 && chk_multi == 0 &&
505 a596b957 2022-07-14 tracey srv->max_commits_display != 1)
506 a596b957 2022-07-14 tracey commit_found = 1;
508 a596b957 2022-07-14 tracey chk_multi = 1;
511 a596b957 2022-07-14 tracey * check for one more commit before breaking,
512 a596b957 2022-07-14 tracey * so we know whether to navigate through briefs
513 a596b957 2022-07-14 tracey * commits and summary
515 a596b957 2022-07-14 tracey if (chk_next && (qs->action == BRIEFS ||
516 a596b957 2022-07-14 tracey qs->action == COMMITS || qs->action == SUMMARY)) {
517 a596b957 2022-07-14 tracey t->next_id = strdup(new_repo_commit->commit_id);
518 a596b957 2022-07-14 tracey if (t->next_id == NULL) {
519 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
520 a596b957 2022-07-14 tracey goto done;
522 a596b957 2022-07-14 tracey if (commit) {
523 a596b957 2022-07-14 tracey got_object_commit_close(commit);
524 a596b957 2022-07-14 tracey commit = NULL;
526 a596b957 2022-07-14 tracey if (t->next_id == NULL) {
527 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
528 a596b957 2022-07-14 tracey goto done;
530 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_commits, new_repo_commit,
532 a596b957 2022-07-14 tracey gotweb_free_repo_commit(new_repo_commit);
533 a596b957 2022-07-14 tracey goto done;
536 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
537 a596b957 2022-07-14 tracey if (error || (limit && --limit == 0)) {
538 a596b957 2022-07-14 tracey if (commit_found || (qs->file != NULL &&
539 a596b957 2022-07-14 tracey strlen(qs->file) > 0))
540 a596b957 2022-07-14 tracey if (chk_multi == 0)
542 a596b957 2022-07-14 tracey chk_next = 1;
544 a596b957 2022-07-14 tracey if (commit) {
545 a596b957 2022-07-14 tracey got_object_commit_close(commit);
546 a596b957 2022-07-14 tracey commit = NULL;
550 a596b957 2022-07-14 tracey gotweb_free_repo_commit(repo_commit);
551 a596b957 2022-07-14 tracey if (commit)
552 a596b957 2022-07-14 tracey got_object_commit_close(commit);
553 a596b957 2022-07-14 tracey if (graph)
554 a596b957 2022-07-14 tracey got_commit_graph_close(graph);
555 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
556 a596b957 2022-07-14 tracey free(file_path);
557 a596b957 2022-07-14 tracey free(repo_path);
558 a596b957 2022-07-14 tracey free(id);
559 a596b957 2022-07-14 tracey return error;
562 a596b957 2022-07-14 tracey const struct got_error *
563 a596b957 2022-07-14 tracey got_get_repo_tags(struct request *c, int limit)
565 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
566 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
567 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
568 a596b957 2022-07-14 tracey struct got_reflist_head refs;
569 a596b957 2022-07-14 tracey struct got_reference *ref;
570 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
571 a596b957 2022-07-14 tracey struct server *srv = c->srv;
572 a596b957 2022-07-14 tracey struct transport *t = c->t;
573 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
574 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
575 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
576 a596b957 2022-07-14 tracey struct got_tag_object *tag = NULL;
577 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
578 a596b957 2022-07-14 tracey char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
579 a596b957 2022-07-14 tracey char *commit_msg = NULL, *commit_msg0 = NULL;
580 a596b957 2022-07-14 tracey int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
582 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
584 a596b957 2022-07-14 tracey if (asprintf(&repo_path, "%s/%s", srv->repos_path,
585 a596b957 2022-07-14 tracey repo_dir->name) == -1)
586 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
588 a596b957 2022-07-14 tracey if (error)
589 a596b957 2022-07-14 tracey return error;
591 a596b957 2022-07-14 tracey if (qs->commit == NULL && qs->action == TAGS) {
592 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->headref, 0);
593 a596b957 2022-07-14 tracey if (error)
594 a596b957 2022-07-14 tracey goto err;
595 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
596 a596b957 2022-07-14 tracey got_ref_close(ref);
597 a596b957 2022-07-14 tracey if (error)
598 a596b957 2022-07-14 tracey goto err;
599 a596b957 2022-07-14 tracey } else if (qs->commit == NULL && qs->action == TAG) {
600 a596b957 2022-07-14 tracey error = got_error_msg(GOT_ERR_EOF, "commit id missing");
601 a596b957 2022-07-14 tracey goto err;
603 a596b957 2022-07-14 tracey error = got_repo_match_object_id_prefix(&id, qs->commit,
604 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, repo);
605 a596b957 2022-07-14 tracey if (error)
606 a596b957 2022-07-14 tracey goto err;
609 a596b957 2022-07-14 tracey if (qs->action != SUMMARY && qs->action != TAGS) {
610 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
611 a596b957 2022-07-14 tracey if (error)
612 a596b957 2022-07-14 tracey goto err;
613 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
614 a596b957 2022-07-14 tracey if (error)
615 a596b957 2022-07-14 tracey goto err;
616 a596b957 2022-07-14 tracey if (commit) {
617 a596b957 2022-07-14 tracey got_object_commit_close(commit);
618 a596b957 2022-07-14 tracey commit = NULL;
622 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, repo_path);
623 a596b957 2022-07-14 tracey if (error)
624 a596b957 2022-07-14 tracey goto err;
626 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
628 a596b957 2022-07-14 tracey if (error)
629 a596b957 2022-07-14 tracey goto err;
631 a596b957 2022-07-14 tracey if (limit == 1)
632 a596b957 2022-07-14 tracey chk_multi = 0;
635 a596b957 2022-07-14 tracey * XXX: again, see previous message about caching
638 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
639 a596b957 2022-07-14 tracey struct repo_tag *new_repo_tag = NULL;
640 a596b957 2022-07-14 tracey error = got_init_repo_tag(&new_repo_tag);
641 a596b957 2022-07-14 tracey if (error)
642 a596b957 2022-07-14 tracey goto err;
644 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
646 a596b957 2022-07-14 tracey new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
647 a596b957 2022-07-14 tracey if (new_repo_tag->tag_name == NULL) {
648 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
649 a596b957 2022-07-14 tracey goto err;
652 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, re->ref);
653 a596b957 2022-07-14 tracey if (error)
654 a596b957 2022-07-14 tracey goto done;
656 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, repo, id);
657 a596b957 2022-07-14 tracey if (error) {
658 a596b957 2022-07-14 tracey if (error->code != GOT_ERR_OBJ_TYPE) {
659 a596b957 2022-07-14 tracey free(id);
660 a596b957 2022-07-14 tracey id = NULL;
661 a596b957 2022-07-14 tracey goto done;
663 a596b957 2022-07-14 tracey /* "lightweight" tag */
664 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
665 a596b957 2022-07-14 tracey if (error) {
666 a596b957 2022-07-14 tracey free(id);
667 a596b957 2022-07-14 tracey id = NULL;
668 a596b957 2022-07-14 tracey goto done;
670 a596b957 2022-07-14 tracey new_repo_tag->tagger =
671 a596b957 2022-07-14 tracey strdup(got_object_commit_get_committer(commit));
672 a596b957 2022-07-14 tracey if (new_repo_tag->tagger == NULL) {
673 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
674 a596b957 2022-07-14 tracey goto err;
676 a596b957 2022-07-14 tracey new_repo_tag->tagger_time =
677 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
678 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str, id);
679 a596b957 2022-07-14 tracey if (error)
680 a596b957 2022-07-14 tracey goto err;
681 a596b957 2022-07-14 tracey free(id);
682 a596b957 2022-07-14 tracey id = NULL;
684 a596b957 2022-07-14 tracey free(id);
685 a596b957 2022-07-14 tracey id = NULL;
686 a596b957 2022-07-14 tracey new_repo_tag->tagger =
687 a596b957 2022-07-14 tracey strdup(got_object_tag_get_tagger(tag));
688 a596b957 2022-07-14 tracey if (new_repo_tag->tagger == NULL) {
689 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
690 a596b957 2022-07-14 tracey goto err;
692 a596b957 2022-07-14 tracey new_repo_tag->tagger_time =
693 a596b957 2022-07-14 tracey got_object_tag_get_tagger_time(tag);
694 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str,
695 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag));
696 a596b957 2022-07-14 tracey if (error)
697 a596b957 2022-07-14 tracey goto err;
700 a596b957 2022-07-14 tracey new_repo_tag->commit_id = strdup(id_str);
701 a596b957 2022-07-14 tracey if (new_repo_tag->commit_id == NULL)
702 a596b957 2022-07-14 tracey goto err;
704 a596b957 2022-07-14 tracey if (commit_found == 0 && qs->commit != NULL &&
705 a596b957 2022-07-14 tracey strncmp(id_str, qs->commit, strlen(id_str)) != 0)
706 a596b957 2022-07-14 tracey continue;
708 a596b957 2022-07-14 tracey commit_found = 1;
710 a596b957 2022-07-14 tracey t->tag_count++;
713 a596b957 2022-07-14 tracey * check for one more commit before breaking,
714 a596b957 2022-07-14 tracey * so we know whether to navigate through briefs
715 a596b957 2022-07-14 tracey * commits and summary
717 a596b957 2022-07-14 tracey if (chk_next) {
718 a596b957 2022-07-14 tracey t->next_id = strdup(new_repo_tag->commit_id);
719 a596b957 2022-07-14 tracey if (t->next_id == NULL) {
720 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
721 a596b957 2022-07-14 tracey goto err;
723 a596b957 2022-07-14 tracey if (commit) {
724 a596b957 2022-07-14 tracey got_object_commit_close(commit);
725 a596b957 2022-07-14 tracey commit = NULL;
727 a596b957 2022-07-14 tracey if (t->next_id == NULL) {
728 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
729 a596b957 2022-07-14 tracey goto err;
731 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
732 a596b957 2022-07-14 tracey gotweb_free_repo_tag(new_repo_tag);
733 a596b957 2022-07-14 tracey goto done;
736 a596b957 2022-07-14 tracey if (commit) {
737 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&new_repo_tag->
738 a596b957 2022-07-14 tracey tag_commit, commit);
739 a596b957 2022-07-14 tracey if (error)
740 a596b957 2022-07-14 tracey goto done;
741 a596b957 2022-07-14 tracey got_object_commit_close(commit);
742 a596b957 2022-07-14 tracey commit = NULL;
744 a596b957 2022-07-14 tracey new_repo_tag->tag_commit =
745 a596b957 2022-07-14 tracey strdup(got_object_tag_get_message(tag));
746 a596b957 2022-07-14 tracey if (new_repo_tag->tag_commit == NULL) {
747 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
748 a596b957 2022-07-14 tracey goto done;
752 a596b957 2022-07-14 tracey while (*new_repo_tag->tag_commit == '\n')
753 a596b957 2022-07-14 tracey new_repo_tag->tag_commit++;
755 a596b957 2022-07-14 tracey if (qs->action != SUMMARY && qs->action != TAGS) {
756 a596b957 2022-07-14 tracey commit_msg = commit_msg0;
757 a596b957 2022-07-14 tracey while (*commit_msg == '\n')
758 a596b957 2022-07-14 tracey commit_msg++;
760 a596b957 2022-07-14 tracey new_repo_tag->commit_msg = strdup(commit_msg);
761 a596b957 2022-07-14 tracey if (new_repo_tag->commit_msg == NULL) {
762 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
763 a596b957 2022-07-14 tracey free(commit_msg0);
764 a596b957 2022-07-14 tracey goto err;
766 a596b957 2022-07-14 tracey free(commit_msg0);
769 a596b957 2022-07-14 tracey if (limit && --limit == 0) {
770 a596b957 2022-07-14 tracey if (chk_multi == 0)
772 a596b957 2022-07-14 tracey chk_next = 1;
774 a596b957 2022-07-14 tracey free(id);
775 a596b957 2022-07-14 tracey id = NULL;
780 a596b957 2022-07-14 tracey * we have tailq populated, so find previous commit id
781 a596b957 2022-07-14 tracey * for navigation through briefs and commits
783 a596b957 2022-07-14 tracey if (t->tag_count == 0) {
784 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
785 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
786 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
789 a596b957 2022-07-14 tracey if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
790 a596b957 2022-07-14 tracey commit_found = 0;
791 a596b957 2022-07-14 tracey TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
793 a596b957 2022-07-14 tracey if (commit_found == 0 && rt->commit_id != NULL &&
794 a596b957 2022-07-14 tracey strcmp(qs->commit, rt->commit_id) != 0) {
795 a596b957 2022-07-14 tracey continue;
797 a596b957 2022-07-14 tracey commit_found = 1;
798 a596b957 2022-07-14 tracey if (c_cnt == srv->max_commits_display ||
799 a596b957 2022-07-14 tracey rt == TAILQ_FIRST(&t->repo_tags)) {
800 a596b957 2022-07-14 tracey t->prev_id = strdup(rt->commit_id);
801 a596b957 2022-07-14 tracey if (t->prev_id == NULL)
802 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
809 a596b957 2022-07-14 tracey if (commit)
810 a596b957 2022-07-14 tracey got_object_commit_close(commit);
811 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
812 a596b957 2022-07-14 tracey free(repo_path);
813 a596b957 2022-07-14 tracey free(id);
814 a596b957 2022-07-14 tracey return error;
817 a596b957 2022-07-14 tracey const struct got_error *
818 a596b957 2022-07-14 tracey got_output_repo_tree(struct request *c)
820 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
821 a596b957 2022-07-14 tracey struct transport *t = c->t;
822 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
823 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
824 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
825 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
826 a596b957 2022-07-14 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
827 a596b957 2022-07-14 tracey struct got_reflist_head refs;
828 a596b957 2022-07-14 tracey struct got_tree_object *tree = NULL;
829 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
830 01498c42 2022-08-19 op const char *name, *index_page_str, *folder;
831 b7efc9b3 2022-08-25 op char *id_str = NULL, *escaped_name = NULL;
832 01498c42 2022-08-19 op char *path = NULL, *in_repo_path = NULL, *modestr = NULL;
833 01498c42 2022-08-19 op int nentries, i, r;
835 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
837 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
839 a596b957 2022-07-14 tracey if (qs->folder != NULL) {
840 a596b957 2022-07-14 tracey path = strdup(qs->folder);
841 a596b957 2022-07-14 tracey if (path == NULL) {
842 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
843 a596b957 2022-07-14 tracey goto done;
846 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, repo_dir->path);
847 a596b957 2022-07-14 tracey if (error)
848 a596b957 2022-07-14 tracey goto done;
849 a596b957 2022-07-14 tracey free(path);
850 a596b957 2022-07-14 tracey path = in_repo_path;
853 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
854 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
855 a596b957 2022-07-14 tracey if (error)
856 a596b957 2022-07-14 tracey goto done;
858 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
859 a596b957 2022-07-14 tracey if (error)
860 a596b957 2022-07-14 tracey goto done;
862 a596b957 2022-07-14 tracey error = got_object_id_by_path(&tree_id, repo, commit, path);
863 a596b957 2022-07-14 tracey if (error)
864 a596b957 2022-07-14 tracey goto done;
866 a596b957 2022-07-14 tracey error = got_object_open_as_tree(&tree, repo, tree_id);
867 a596b957 2022-07-14 tracey if (error)
868 a596b957 2022-07-14 tracey goto done;
870 a596b957 2022-07-14 tracey nentries = got_object_tree_get_nentries(tree);
872 01498c42 2022-08-19 op index_page_str = qs->index_page_str ? qs->index_page_str : "";
873 01498c42 2022-08-19 op folder = qs->folder ? qs->folder : "";
875 a596b957 2022-07-14 tracey for (i = 0; i < nentries; i++) {
876 a596b957 2022-07-14 tracey struct got_tree_entry *te;
877 a596b957 2022-07-14 tracey mode_t mode;
879 a596b957 2022-07-14 tracey te = got_object_tree_get_entry(tree, i);
881 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
882 a596b957 2022-07-14 tracey if (error)
883 a596b957 2022-07-14 tracey goto done;
885 a596b957 2022-07-14 tracey modestr = strdup("");
886 a596b957 2022-07-14 tracey if (modestr == NULL) {
887 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
888 a596b957 2022-07-14 tracey goto done;
890 a596b957 2022-07-14 tracey mode = got_tree_entry_get_mode(te);
891 a596b957 2022-07-14 tracey if (got_object_tree_entry_is_submodule(te)) {
892 a596b957 2022-07-14 tracey free(modestr);
893 a596b957 2022-07-14 tracey modestr = strdup("$");
894 a596b957 2022-07-14 tracey if (modestr == NULL) {
895 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
896 a596b957 2022-07-14 tracey goto done;
898 a596b957 2022-07-14 tracey } else if (S_ISLNK(mode)) {
899 a596b957 2022-07-14 tracey free(modestr);
900 a596b957 2022-07-14 tracey modestr = strdup("@");
901 a596b957 2022-07-14 tracey if (modestr == NULL) {
902 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
903 a596b957 2022-07-14 tracey goto done;
905 a596b957 2022-07-14 tracey } else if (S_ISDIR(mode)) {
906 a596b957 2022-07-14 tracey free(modestr);
907 a596b957 2022-07-14 tracey modestr = strdup("/");
908 a596b957 2022-07-14 tracey if (modestr == NULL) {
909 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
910 a596b957 2022-07-14 tracey goto done;
912 a596b957 2022-07-14 tracey } else if (mode & S_IXUSR) {
913 a596b957 2022-07-14 tracey free(modestr);
914 a596b957 2022-07-14 tracey modestr = strdup("*");
915 a596b957 2022-07-14 tracey if (modestr == NULL) {
916 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
917 a596b957 2022-07-14 tracey goto done;
921 d927f8c8 2022-08-20 op name = got_tree_entry_get_name(te);
922 d927f8c8 2022-08-20 op error = gotweb_escape_html(&escaped_name, name);
926 a596b957 2022-07-14 tracey if (S_ISDIR(mode)) {
927 01498c42 2022-08-19 op r = fcgi_printf(c,
928 01498c42 2022-08-19 op "<div class='tree_wrapper'>\n"
929 01498c42 2022-08-19 op "<div class='tree_line'>"
930 01498c42 2022-08-19 op "<a class='diff_directory' "
931 01498c42 2022-08-19 op "href='?index_page=%s&path=%s&action=tree"
932 01498c42 2022-08-19 op "&commit=%s&folder=%s/%s'>%s%s</a>"
933 01498c42 2022-08-19 op "</div>\n" /* .tree_line */
934 01498c42 2022-08-19 op "<div class='tree_line_blank'> </div>\n"
935 01498c42 2022-08-19 op "</div>\n", /* .tree_wrapper */
936 01498c42 2022-08-19 op index_page_str, qs->path, rc->commit_id,
937 d927f8c8 2022-08-20 op folder, name, escaped_name, modestr);
939 a596b957 2022-07-14 tracey goto done;
941 01498c42 2022-08-19 op r = fcgi_printf(c,
942 01498c42 2022-08-19 op "<div class='tree_wrapper'>\n"
943 01498c42 2022-08-19 op "<div class='tree_line'>"
944 01498c42 2022-08-19 op "<a href='?index_page=%s&path=%s&action=blob"
945 01498c42 2022-08-19 op "&commit=%s&folder=%s&file=%s'>%s%s</a>"
946 01498c42 2022-08-19 op "</div>\n" /* .tree_line */
947 01498c42 2022-08-19 op "<div class='tree_line_blank'>"
948 01498c42 2022-08-19 op "<a href='?index_page=%s&path=%s&action=commits"
949 01498c42 2022-08-19 op "&commit=%s&folder=%s&file=%s'>commits</a>\n"
951 01498c42 2022-08-19 op "<a href='?index_page=%s&path=%s&action=blame"
952 01498c42 2022-08-19 op "&commit=%s&folder=%s&file=%s'>blame</a>\n"
953 01498c42 2022-08-19 op "</div>\n" /* .tree_line_blank */
954 01498c42 2022-08-19 op "</div>\n", /* .tree_wrapper */
955 01498c42 2022-08-19 op index_page_str, qs->path, rc->commit_id,
956 d927f8c8 2022-08-20 op folder, name, escaped_name, modestr,
957 01498c42 2022-08-19 op index_page_str, qs->path, rc->commit_id,
958 01498c42 2022-08-19 op folder, name,
959 01498c42 2022-08-19 op index_page_str, qs->path, rc->commit_id,
960 01498c42 2022-08-19 op folder, name);
962 a596b957 2022-07-14 tracey goto done;
964 a596b957 2022-07-14 tracey free(id_str);
965 a596b957 2022-07-14 tracey id_str = NULL;
966 a596b957 2022-07-14 tracey free(modestr);
967 a596b957 2022-07-14 tracey modestr = NULL;
968 b7efc9b3 2022-08-25 op free(escaped_name);
969 b7efc9b3 2022-08-25 op escaped_name = NULL;
972 b7efc9b3 2022-08-25 op free(escaped_name);
973 a596b957 2022-07-14 tracey free(id_str);
974 a596b957 2022-07-14 tracey free(modestr);
975 a596b957 2022-07-14 tracey free(path);
976 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
977 a596b957 2022-07-14 tracey if (commit)
978 a596b957 2022-07-14 tracey got_object_commit_close(commit);
979 a596b957 2022-07-14 tracey free(commit_id);
980 a596b957 2022-07-14 tracey free(tree_id);
981 a596b957 2022-07-14 tracey return error;
984 a596b957 2022-07-14 tracey const struct got_error *
985 a596b957 2022-07-14 tracey got_output_file_blob(struct request *c)
987 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
988 a596b957 2022-07-14 tracey struct transport *t = c->t;
989 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
990 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
991 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
992 a596b957 2022-07-14 tracey struct got_object_id *commit_id = NULL;
993 a596b957 2022-07-14 tracey struct got_reflist_head refs;
994 a596b957 2022-07-14 tracey struct got_blob_object *blob = NULL;
995 a596b957 2022-07-14 tracey char *path = NULL, *in_repo_path = NULL;
996 0d8d489a 2022-07-28 op int obj_type, set_mime = 0, fd = -1;
997 a596b957 2022-07-14 tracey size_t len, hdrlen;
998 a596b957 2022-07-14 tracey const uint8_t *buf;
1000 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1002 a596b957 2022-07-14 tracey if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1003 a596b957 2022-07-14 tracey qs->folder ? "/" : "", qs->file) == -1) {
1004 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1005 a596b957 2022-07-14 tracey goto done;
1008 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, path);
1009 a596b957 2022-07-14 tracey if (error)
1010 a596b957 2022-07-14 tracey goto done;
1012 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1013 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
1014 a596b957 2022-07-14 tracey if (error)
1015 a596b957 2022-07-14 tracey goto done;
1017 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
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_id_by_path(&commit_id, repo, commit, in_repo_path);
1022 a596b957 2022-07-14 tracey if (error)
1023 a596b957 2022-07-14 tracey goto done;
1025 a596b957 2022-07-14 tracey if (commit_id == NULL) {
1026 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_NO_OBJ);
1027 a596b957 2022-07-14 tracey goto done;
1030 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, commit_id);
1031 a596b957 2022-07-14 tracey if (error)
1032 a596b957 2022-07-14 tracey goto done;
1034 a596b957 2022-07-14 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
1035 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1036 a596b957 2022-07-14 tracey goto done;
1039 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1040 a596b957 2022-07-14 tracey if (error)
1041 a596b957 2022-07-14 tracey goto done;
1043 a596b957 2022-07-14 tracey error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1044 a596b957 2022-07-14 tracey if (error)
1045 a596b957 2022-07-14 tracey goto done;
1046 a596b957 2022-07-14 tracey hdrlen = got_object_blob_get_hdrlen(blob);
1048 a596b957 2022-07-14 tracey error = got_object_blob_read_block(&len, blob);
1049 a596b957 2022-07-14 tracey if (error)
1050 a596b957 2022-07-14 tracey goto done;
1051 a596b957 2022-07-14 tracey buf = got_object_blob_get_read_buf(blob);
1054 a596b957 2022-07-14 tracey * Skip blob object header first time around,
1055 a596b957 2022-07-14 tracey * which also contains a zero byte.
1057 a596b957 2022-07-14 tracey buf += hdrlen;
1058 a596b957 2022-07-14 tracey if (set_mime == 0) {
1059 a596b957 2022-07-14 tracey if (isbinary(buf, len - hdrlen)) {
1060 a596b957 2022-07-14 tracey error = gotweb_render_content_type_file(c,
1061 a596b957 2022-07-14 tracey "application/octet-stream",
1062 a596b957 2022-07-14 tracey qs->file);
1063 a596b957 2022-07-14 tracey if (error) {
1064 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__,
1065 a596b957 2022-07-14 tracey error->msg);
1066 a596b957 2022-07-14 tracey goto done;
1068 a596b957 2022-07-14 tracey } else {
1069 a596b957 2022-07-14 tracey error = gotweb_render_content_type(c,
1070 d2716103 2022-07-28 op "text/plain");
1071 a596b957 2022-07-14 tracey if (error) {
1072 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__,
1073 a596b957 2022-07-14 tracey error->msg);
1074 a596b957 2022-07-14 tracey goto done;
1078 a596b957 2022-07-14 tracey set_mime = 1;
1079 0d8d489a 2022-07-28 op fcgi_gen_binary_response(c, buf, len - hdrlen);
1081 a596b957 2022-07-14 tracey hdrlen = 0;
1082 a596b957 2022-07-14 tracey } while (len != 0);
1084 a596b957 2022-07-14 tracey if (commit)
1085 a596b957 2022-07-14 tracey got_object_commit_close(commit);
1086 a596b957 2022-07-14 tracey if (fd != -1 && close(fd) == -1 && error == NULL)
1087 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1088 a596b957 2022-07-14 tracey if (blob)
1089 a596b957 2022-07-14 tracey got_object_blob_close(blob);
1090 a596b957 2022-07-14 tracey free(in_repo_path);
1091 a596b957 2022-07-14 tracey free(commit_id);
1092 a596b957 2022-07-14 tracey free(path);
1093 a596b957 2022-07-14 tracey return error;
1096 a596b957 2022-07-14 tracey struct blame_line {
1097 a596b957 2022-07-14 tracey int annotated;
1098 a596b957 2022-07-14 tracey char *id_str;
1099 a596b957 2022-07-14 tracey char *committer;
1100 a596b957 2022-07-14 tracey char datebuf[11]; /* YYYY-MM-DD + NUL */
1103 a596b957 2022-07-14 tracey struct blame_cb_args {
1104 a596b957 2022-07-14 tracey struct blame_line *lines;
1105 a596b957 2022-07-14 tracey int nlines;
1106 a596b957 2022-07-14 tracey int nlines_prec;
1107 a596b957 2022-07-14 tracey int lineno_cur;
1108 a596b957 2022-07-14 tracey off_t *line_offsets;
1109 a596b957 2022-07-14 tracey FILE *f;
1110 a596b957 2022-07-14 tracey struct got_repository *repo;
1111 a596b957 2022-07-14 tracey struct request *c;
1114 a596b957 2022-07-14 tracey static const struct got_error *
1115 a596b957 2022-07-14 tracey got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1116 a596b957 2022-07-14 tracey struct got_commit_object *commit, struct got_object_id *id)
1118 a596b957 2022-07-14 tracey const struct got_error *err = NULL;
1119 a596b957 2022-07-14 tracey struct blame_cb_args *a = arg;
1120 a596b957 2022-07-14 tracey struct blame_line *bline;
1121 a596b957 2022-07-14 tracey struct request *c = a->c;
1122 a596b957 2022-07-14 tracey struct transport *t = c->t;
1123 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
1124 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
1125 01498c42 2022-08-19 op const char *index_page_str;
1126 a596b957 2022-07-14 tracey char *line = NULL, *eline = NULL;
1127 a596b957 2022-07-14 tracey size_t linesize = 0;
1128 a596b957 2022-07-14 tracey off_t offset;
1129 a596b957 2022-07-14 tracey struct tm tm;
1130 a596b957 2022-07-14 tracey time_t committer_time;
1133 a596b957 2022-07-14 tracey if (nlines != a->nlines ||
1134 a596b957 2022-07-14 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
1135 a596b957 2022-07-14 tracey return got_error(GOT_ERR_RANGE);
1137 a596b957 2022-07-14 tracey if (lineno == -1)
1138 a596b957 2022-07-14 tracey return NULL; /* no change in this commit */
1140 a596b957 2022-07-14 tracey /* Annotate this line. */
1141 a596b957 2022-07-14 tracey bline = &a->lines[lineno - 1];
1142 a596b957 2022-07-14 tracey if (bline->annotated)
1143 a596b957 2022-07-14 tracey return NULL;
1144 a596b957 2022-07-14 tracey err = got_object_id_str(&bline->id_str, id);
1145 a596b957 2022-07-14 tracey if (err)
1146 a596b957 2022-07-14 tracey return err;
1148 a596b957 2022-07-14 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
1149 a596b957 2022-07-14 tracey if (bline->committer == NULL) {
1150 a596b957 2022-07-14 tracey err = got_error_from_errno("strdup");
1151 a596b957 2022-07-14 tracey goto done;
1154 a596b957 2022-07-14 tracey committer_time = got_object_commit_get_committer_time(commit);
1155 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1156 a596b957 2022-07-14 tracey return got_error_from_errno("gmtime_r");
1157 a596b957 2022-07-14 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1158 a596b957 2022-07-14 tracey &tm) == 0) {
1159 a596b957 2022-07-14 tracey err = got_error(GOT_ERR_NO_SPACE);
1160 a596b957 2022-07-14 tracey goto done;
1162 a596b957 2022-07-14 tracey bline->annotated = 1;
1164 a596b957 2022-07-14 tracey /* Print lines annotated so far. */
1165 a596b957 2022-07-14 tracey bline = &a->lines[a->lineno_cur - 1];
1166 a596b957 2022-07-14 tracey if (!bline->annotated)
1167 a596b957 2022-07-14 tracey goto done;
1169 a596b957 2022-07-14 tracey offset = a->line_offsets[a->lineno_cur - 1];
1170 a596b957 2022-07-14 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
1171 a596b957 2022-07-14 tracey err = got_error_from_errno("fseeko");
1172 a596b957 2022-07-14 tracey goto done;
1175 01498c42 2022-08-19 op index_page_str = qs->index_page_str ? qs->index_page_str : "";
1177 85f2c2e0 2022-08-18 op while (a->lineno_cur <= a->nlines && bline->annotated) {
1178 a596b957 2022-07-14 tracey char *smallerthan, *at, *nl, *committer;
1179 a596b957 2022-07-14 tracey size_t len;
1181 a596b957 2022-07-14 tracey if (getline(&line, &linesize, a->f) == -1) {
1182 a596b957 2022-07-14 tracey if (ferror(a->f))
1183 a596b957 2022-07-14 tracey err = got_error_from_errno("getline");
1187 a596b957 2022-07-14 tracey committer = bline->committer;
1188 a596b957 2022-07-14 tracey smallerthan = strchr(committer, '<');
1189 a596b957 2022-07-14 tracey if (smallerthan && smallerthan[1] != '\0')
1190 a596b957 2022-07-14 tracey committer = smallerthan + 1;
1191 a596b957 2022-07-14 tracey at = strchr(committer, '@');
1193 a596b957 2022-07-14 tracey *at = '\0';
1194 a596b957 2022-07-14 tracey len = strlen(committer);
1195 a596b957 2022-07-14 tracey if (len >= 9)
1196 a596b957 2022-07-14 tracey committer[8] = '\0';
1198 a596b957 2022-07-14 tracey nl = strchr(line, '\n');
1200 a596b957 2022-07-14 tracey *nl = '\0';
1202 a596b957 2022-07-14 tracey err = gotweb_escape_html(&eline, line);
1203 a596b957 2022-07-14 tracey if (err)
1204 a596b957 2022-07-14 tracey goto done;
1206 01498c42 2022-08-19 op r = fcgi_printf(c, "<div class='blame_wrapper'>"
1207 01498c42 2022-08-19 op "<div class='blame_number'>%.*d</div>"
1208 01498c42 2022-08-19 op "<div class='blame_hash'>"
1209 01498c42 2022-08-19 op "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1212 01498c42 2022-08-19 op "<div class='blame_date'>%s</div>"
1213 01498c42 2022-08-19 op "<div class='blame_author'>%s</div>"
1214 01498c42 2022-08-19 op "<div class='blame_code'>%s</div>"
1215 01498c42 2022-08-19 op "</div>", /* .blame_wrapper */
1216 01498c42 2022-08-19 op a->nlines_prec, a->lineno_cur,
1217 01498c42 2022-08-19 op index_page_str, repo_dir->name, bline->id_str,
1218 01498c42 2022-08-19 op bline->id_str,
1219 01498c42 2022-08-19 op bline->datebuf,
1222 01498c42 2022-08-19 op if (r == -1)
1223 a596b957 2022-07-14 tracey goto done;
1225 a596b957 2022-07-14 tracey a->lineno_cur++;
1226 a596b957 2022-07-14 tracey bline = &a->lines[a->lineno_cur - 1];
1229 a596b957 2022-07-14 tracey free(line);
1230 a596b957 2022-07-14 tracey free(eline);
1231 a596b957 2022-07-14 tracey return err;
1234 a596b957 2022-07-14 tracey const struct got_error *
1235 a596b957 2022-07-14 tracey got_output_file_blame(struct request *c)
1237 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1238 a596b957 2022-07-14 tracey struct transport *t = c->t;
1239 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1240 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
1241 a596b957 2022-07-14 tracey struct got_object_id *obj_id = NULL, *commit_id = NULL;
1242 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
1243 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1244 a596b957 2022-07-14 tracey struct got_blob_object *blob = NULL;
1245 a596b957 2022-07-14 tracey char *path = NULL, *in_repo_path = NULL;
1246 a596b957 2022-07-14 tracey struct blame_cb_args bca;
1247 a596b957 2022-07-14 tracey int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1248 a596b957 2022-07-14 tracey int fd6 = -1;
1249 a596b957 2022-07-14 tracey off_t filesize;
1250 a596b957 2022-07-14 tracey FILE *f1 = NULL, *f2 = NULL;
1252 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1253 a596b957 2022-07-14 tracey bca.f = NULL;
1254 a596b957 2022-07-14 tracey bca.lines = NULL;
1256 a596b957 2022-07-14 tracey if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1257 a596b957 2022-07-14 tracey qs->folder ? "/" : "", qs->file) == -1) {
1258 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1259 a596b957 2022-07-14 tracey goto done;
1262 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, path);
1263 a596b957 2022-07-14 tracey if (error)
1264 a596b957 2022-07-14 tracey goto done;
1266 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1267 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
1268 a596b957 2022-07-14 tracey if (error)
1269 a596b957 2022-07-14 tracey goto done;
1271 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
1272 a596b957 2022-07-14 tracey if (error)
1273 a596b957 2022-07-14 tracey goto done;
1275 a596b957 2022-07-14 tracey error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1276 a596b957 2022-07-14 tracey if (error)
1277 a596b957 2022-07-14 tracey goto done;
1279 a596b957 2022-07-14 tracey if (commit_id == NULL) {
1280 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_NO_OBJ);
1281 a596b957 2022-07-14 tracey goto done;
1284 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, obj_id);
1285 a596b957 2022-07-14 tracey if (error)
1286 a596b957 2022-07-14 tracey goto done;
1288 a596b957 2022-07-14 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
1289 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1290 a596b957 2022-07-14 tracey goto done;
1293 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1294 a596b957 2022-07-14 tracey if (error)
1295 a596b957 2022-07-14 tracey goto done;
1297 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1298 a596b957 2022-07-14 tracey if (error)
1299 a596b957 2022-07-14 tracey goto done;
1301 a596b957 2022-07-14 tracey error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1302 a596b957 2022-07-14 tracey if (error)
1303 a596b957 2022-07-14 tracey goto done;
1305 a596b957 2022-07-14 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1306 a596b957 2022-07-14 tracey &bca.line_offsets, bca.f, blob);
1307 a596b957 2022-07-14 tracey if (error || bca.nlines == 0)
1308 a596b957 2022-07-14 tracey goto done;
1310 a596b957 2022-07-14 tracey /* Don't include \n at EOF in the blame line count. */
1311 a596b957 2022-07-14 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
1312 a596b957 2022-07-14 tracey bca.nlines--;
1314 a596b957 2022-07-14 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1315 a596b957 2022-07-14 tracey if (bca.lines == NULL) {
1316 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1317 a596b957 2022-07-14 tracey goto done;
1319 a596b957 2022-07-14 tracey bca.lineno_cur = 1;
1320 a596b957 2022-07-14 tracey bca.nlines_prec = 0;
1321 a596b957 2022-07-14 tracey i = bca.nlines;
1322 a596b957 2022-07-14 tracey while (i > 0) {
1323 a596b957 2022-07-14 tracey i /= 10;
1324 a596b957 2022-07-14 tracey bca.nlines_prec++;
1326 a596b957 2022-07-14 tracey bca.repo = repo;
1327 a596b957 2022-07-14 tracey bca.c = c;
1329 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1330 a596b957 2022-07-14 tracey if (error)
1331 a596b957 2022-07-14 tracey goto done;
1333 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1334 a596b957 2022-07-14 tracey if (error)
1335 a596b957 2022-07-14 tracey goto done;
1337 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1338 a596b957 2022-07-14 tracey if (error)
1339 a596b957 2022-07-14 tracey goto done;
1341 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1342 a596b957 2022-07-14 tracey if (error)
1343 a596b957 2022-07-14 tracey goto done;
1345 a596b957 2022-07-14 tracey error = got_blame(in_repo_path, commit_id, repo,
1346 a596b957 2022-07-14 tracey GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1347 a596b957 2022-07-14 tracey fd3, fd4, f1, f2);
1349 a596b957 2022-07-14 tracey if (blob) {
1350 a596b957 2022-07-14 tracey free(bca.line_offsets);
1351 a596b957 2022-07-14 tracey for (i = 0; i < bca.nlines; i++) {
1352 a596b957 2022-07-14 tracey struct blame_line *bline = &bca.lines[i];
1353 a596b957 2022-07-14 tracey free(bline->id_str);
1354 a596b957 2022-07-14 tracey free(bline->committer);
1358 a596b957 2022-07-14 tracey free(bca.lines);
1359 a596b957 2022-07-14 tracey if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1360 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1361 a596b957 2022-07-14 tracey if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1362 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1363 a596b957 2022-07-14 tracey if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1364 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1365 a596b957 2022-07-14 tracey if (bca.f) {
1366 a596b957 2022-07-14 tracey const struct got_error *bca_err =
1367 a596b957 2022-07-14 tracey got_gotweb_flushfile(bca.f, fd1);
1368 a596b957 2022-07-14 tracey if (error == NULL)
1369 a596b957 2022-07-14 tracey error = bca_err;
1371 a596b957 2022-07-14 tracey if (f1) {
1372 a596b957 2022-07-14 tracey const struct got_error *f1_err =
1373 a596b957 2022-07-14 tracey got_gotweb_flushfile(f1, fd5);
1374 a596b957 2022-07-14 tracey if (error == NULL)
1375 a596b957 2022-07-14 tracey error = f1_err;
1377 a596b957 2022-07-14 tracey if (f2) {
1378 a596b957 2022-07-14 tracey const struct got_error *f2_err =
1379 a596b957 2022-07-14 tracey got_gotweb_flushfile(f2, fd6);
1380 a596b957 2022-07-14 tracey if (error == NULL)
1381 a596b957 2022-07-14 tracey error = f2_err;
1383 a596b957 2022-07-14 tracey if (commit)
1384 a596b957 2022-07-14 tracey got_object_commit_close(commit);
1385 a596b957 2022-07-14 tracey if (blob)
1386 a596b957 2022-07-14 tracey got_object_blob_close(blob);
1387 a596b957 2022-07-14 tracey free(in_repo_path);
1388 a596b957 2022-07-14 tracey free(commit_id);
1389 a596b957 2022-07-14 tracey free(path);
1390 a596b957 2022-07-14 tracey return error;
1393 a596b957 2022-07-14 tracey const struct got_error *
1394 a596b957 2022-07-14 tracey got_output_repo_diff(struct request *c)
1396 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1397 a596b957 2022-07-14 tracey struct transport *t = c->t;
1398 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1399 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
1400 a596b957 2022-07-14 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1401 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1402 a596b957 2022-07-14 tracey FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1403 a596b957 2022-07-14 tracey char *label1 = NULL, *label2 = NULL, *line = NULL;
1404 a596b957 2022-07-14 tracey char *newline, *eline = NULL, *color = NULL;
1405 a596b957 2022-07-14 tracey int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1406 a596b957 2022-07-14 tracey size_t linesize = 0;
1407 a596b957 2022-07-14 tracey ssize_t linelen;
1408 a596b957 2022-07-14 tracey int wrlen = 0;
1410 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1412 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1413 a596b957 2022-07-14 tracey if (error)
1414 a596b957 2022-07-14 tracey return error;
1416 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1417 a596b957 2022-07-14 tracey if (error)
1418 a596b957 2022-07-14 tracey return error;
1420 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1421 a596b957 2022-07-14 tracey if (error)
1422 a596b957 2022-07-14 tracey return error;
1424 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
1426 a596b957 2022-07-14 tracey if (rc->parent_id != NULL &&
1427 a596b957 2022-07-14 tracey strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1428 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&id1, &label1,
1429 a596b957 2022-07-14 tracey rc->parent_id, GOT_OBJ_TYPE_ANY,
1430 a596b957 2022-07-14 tracey &refs, repo);
1431 a596b957 2022-07-14 tracey if (error)
1432 a596b957 2022-07-14 tracey goto done;
1435 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1436 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_ANY, &refs, repo);
1437 a596b957 2022-07-14 tracey if (error)
1438 a596b957 2022-07-14 tracey goto done;
1440 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, id2);
1441 a596b957 2022-07-14 tracey if (error)
1442 a596b957 2022-07-14 tracey goto done;
1444 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1445 a596b957 2022-07-14 tracey if (error)
1446 a596b957 2022-07-14 tracey goto done;
1448 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1449 a596b957 2022-07-14 tracey if (error)
1450 a596b957 2022-07-14 tracey goto done;
1452 a596b957 2022-07-14 tracey switch (obj_type) {
1453 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_BLOB:
1454 a596b957 2022-07-14 tracey error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1455 a596b957 2022-07-14 tracey id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1456 a596b957 2022-07-14 tracey repo, f3);
1458 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_TREE:
1459 a596b957 2022-07-14 tracey error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1460 a596b957 2022-07-14 tracey id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1461 a596b957 2022-07-14 tracey repo, f3);
1463 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_COMMIT:
1464 a596b957 2022-07-14 tracey error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1465 a596b957 2022-07-14 tracey fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1466 a596b957 2022-07-14 tracey repo, f3);
1468 a596b957 2022-07-14 tracey default:
1469 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1471 a596b957 2022-07-14 tracey if (error)
1472 a596b957 2022-07-14 tracey goto done;
1474 a596b957 2022-07-14 tracey if (fseek(f1, 0, SEEK_SET) == -1) {
1475 a596b957 2022-07-14 tracey error = got_ferror(f1, GOT_ERR_IO);
1476 a596b957 2022-07-14 tracey goto done;
1479 a596b957 2022-07-14 tracey if (fseek(f2, 0, SEEK_SET) == -1) {
1480 a596b957 2022-07-14 tracey error = got_ferror(f2, GOT_ERR_IO);
1481 a596b957 2022-07-14 tracey goto done;
1484 a596b957 2022-07-14 tracey if (fseek(f3, 0, SEEK_SET) == -1) {
1485 a596b957 2022-07-14 tracey error = got_ferror(f3, GOT_ERR_IO);
1486 a596b957 2022-07-14 tracey goto done;
1489 a596b957 2022-07-14 tracey while ((linelen = getline(&line, &linesize, f3)) != -1) {
1490 a596b957 2022-07-14 tracey if (strncmp(line, "-", 1) == 0) {
1491 a596b957 2022-07-14 tracey color = strdup("diff_minus");
1492 a596b957 2022-07-14 tracey if (color == NULL) {
1493 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1494 a596b957 2022-07-14 tracey goto done;
1496 a596b957 2022-07-14 tracey } else if (strncmp(line, "+", 1) == 0) {
1497 a596b957 2022-07-14 tracey color = strdup("diff_plus");
1498 a596b957 2022-07-14 tracey if (color == NULL) {
1499 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1500 a596b957 2022-07-14 tracey goto done;
1502 a596b957 2022-07-14 tracey } else if (strncmp(line, "@@", 2) == 0) {
1503 a596b957 2022-07-14 tracey color = strdup("diff_chunk_header");
1504 a596b957 2022-07-14 tracey if (color == NULL) {
1505 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1506 a596b957 2022-07-14 tracey goto done;
1508 a596b957 2022-07-14 tracey } else if (strncmp(line, "@@", 2) == 0) {
1509 a596b957 2022-07-14 tracey color = strdup("diff_chunk_header");
1510 a596b957 2022-07-14 tracey if (color == NULL) {
1511 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1512 a596b957 2022-07-14 tracey goto done;
1514 a596b957 2022-07-14 tracey } else if (strncmp(line, "commit +", 8) == 0) {
1515 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1516 a596b957 2022-07-14 tracey if (color == NULL) {
1517 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1518 a596b957 2022-07-14 tracey goto done;
1520 a596b957 2022-07-14 tracey } else if (strncmp(line, "commit -", 8) == 0) {
1521 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1522 a596b957 2022-07-14 tracey if (color == NULL) {
1523 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1524 a596b957 2022-07-14 tracey goto done;
1526 a596b957 2022-07-14 tracey } else if (strncmp(line, "blob +", 6) == 0) {
1527 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1528 a596b957 2022-07-14 tracey if (color == NULL) {
1529 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1530 a596b957 2022-07-14 tracey goto done;
1532 a596b957 2022-07-14 tracey } else if (strncmp(line, "blob -", 6) == 0) {
1533 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1534 a596b957 2022-07-14 tracey if (color == NULL) {
1535 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1536 a596b957 2022-07-14 tracey goto done;
1538 a596b957 2022-07-14 tracey } else if (strncmp(line, "file +", 6) == 0) {
1539 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1540 a596b957 2022-07-14 tracey if (color == NULL) {
1541 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1542 a596b957 2022-07-14 tracey goto done;
1544 a596b957 2022-07-14 tracey } else if (strncmp(line, "file -", 6) == 0) {
1545 a596b957 2022-07-14 tracey color = strdup("diff_meta");
1546 a596b957 2022-07-14 tracey if (color == NULL) {
1547 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1548 a596b957 2022-07-14 tracey goto done;
1550 a596b957 2022-07-14 tracey } else if (strncmp(line, "from:", 5) == 0) {
1551 a596b957 2022-07-14 tracey color = strdup("diff_author");
1552 a596b957 2022-07-14 tracey if (color == NULL) {
1553 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1554 a596b957 2022-07-14 tracey goto done;
1556 a596b957 2022-07-14 tracey } else if (strncmp(line, "via:", 4) == 0) {
1557 a596b957 2022-07-14 tracey color = strdup("diff_author");
1558 a596b957 2022-07-14 tracey if (color == NULL) {
1559 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1560 a596b957 2022-07-14 tracey goto done;
1562 a596b957 2022-07-14 tracey } else if (strncmp(line, "date:", 5) == 0) {
1563 a596b957 2022-07-14 tracey color = strdup("diff_date");
1564 a596b957 2022-07-14 tracey if (color == NULL) {
1565 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1566 a596b957 2022-07-14 tracey goto done;
1570 a596b957 2022-07-14 tracey newline = strchr(line, '\n');
1571 a596b957 2022-07-14 tracey if (newline)
1572 a596b957 2022-07-14 tracey *newline = '\0';
1574 a596b957 2022-07-14 tracey error = gotweb_escape_html(&eline, line);
1575 a596b957 2022-07-14 tracey if (error)
1576 a596b957 2022-07-14 tracey goto done;
1578 01498c42 2022-08-19 op fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1579 01498c42 2022-08-19 op color ? color : "", eline);
1580 a596b957 2022-07-14 tracey free(eline);
1581 a596b957 2022-07-14 tracey eline = NULL;
1583 a596b957 2022-07-14 tracey if (linelen > 0)
1584 a596b957 2022-07-14 tracey wrlen = wrlen + linelen;
1585 a596b957 2022-07-14 tracey free(color);
1586 a596b957 2022-07-14 tracey color = NULL;
1588 a596b957 2022-07-14 tracey if (linelen == -1 && ferror(f3))
1589 a596b957 2022-07-14 tracey error = got_error_from_errno("getline");
1591 a596b957 2022-07-14 tracey free(color);
1592 a596b957 2022-07-14 tracey if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1593 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1594 a596b957 2022-07-14 tracey if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1595 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1596 a596b957 2022-07-14 tracey if (f1) {
1597 a596b957 2022-07-14 tracey const struct got_error *f1_err =
1598 a596b957 2022-07-14 tracey got_gotweb_flushfile(f1, fd1);
1599 a596b957 2022-07-14 tracey if (error == NULL)
1600 a596b957 2022-07-14 tracey error = f1_err;
1602 a596b957 2022-07-14 tracey if (f2) {
1603 a596b957 2022-07-14 tracey const struct got_error *f2_err =
1604 a596b957 2022-07-14 tracey got_gotweb_flushfile(f2, fd2);
1605 a596b957 2022-07-14 tracey if (error == NULL)
1606 a596b957 2022-07-14 tracey error = f2_err;
1608 a596b957 2022-07-14 tracey if (f3) {
1609 a596b957 2022-07-14 tracey const struct got_error *f3_err =
1610 a596b957 2022-07-14 tracey got_gotweb_flushfile(f3, fd3);
1611 a596b957 2022-07-14 tracey if (error == NULL)
1612 a596b957 2022-07-14 tracey error = f3_err;
1614 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
1615 a596b957 2022-07-14 tracey free(line);
1616 a596b957 2022-07-14 tracey free(eline);
1617 a596b957 2022-07-14 tracey free(label1);
1618 a596b957 2022-07-14 tracey free(label2);
1619 a596b957 2022-07-14 tracey free(id1);
1620 a596b957 2022-07-14 tracey free(id2);
1621 a596b957 2022-07-14 tracey return error;
1624 a596b957 2022-07-14 tracey static const struct got_error *
1625 a596b957 2022-07-14 tracey got_init_repo_commit(struct repo_commit **rc)
1627 a596b957 2022-07-14 tracey *rc = calloc(1, sizeof(**rc));
1628 a596b957 2022-07-14 tracey if (*rc == NULL)
1629 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
1631 a596b957 2022-07-14 tracey (*rc)->path = NULL;
1632 a596b957 2022-07-14 tracey (*rc)->refs_str = NULL;
1633 a596b957 2022-07-14 tracey (*rc)->commit_id = NULL;
1634 a596b957 2022-07-14 tracey (*rc)->committer = NULL;
1635 a596b957 2022-07-14 tracey (*rc)->author = NULL;
1636 a596b957 2022-07-14 tracey (*rc)->parent_id = NULL;
1637 a596b957 2022-07-14 tracey (*rc)->tree_id = NULL;
1638 a596b957 2022-07-14 tracey (*rc)->commit_msg = NULL;
1640 dd84f505 2022-08-31 stsp return NULL;
1643 a596b957 2022-07-14 tracey static const struct got_error *
1644 a596b957 2022-07-14 tracey got_init_repo_tag(struct repo_tag **rt)
1646 a596b957 2022-07-14 tracey *rt = calloc(1, sizeof(**rt));
1647 a596b957 2022-07-14 tracey if (*rt == NULL)
1648 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
1650 a596b957 2022-07-14 tracey (*rt)->commit_id = NULL;
1651 a596b957 2022-07-14 tracey (*rt)->tag_name = NULL;
1652 a596b957 2022-07-14 tracey (*rt)->tag_commit = NULL;
1653 a596b957 2022-07-14 tracey (*rt)->commit_msg = NULL;
1654 a596b957 2022-07-14 tracey (*rt)->tagger = NULL;
1656 dd84f505 2022-08-31 stsp return NULL;