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 5822e79e 2023-02-23 op #include <sha2.h>
26 a596b957 2022-07-14 tracey #include <stdlib.h>
27 a596b957 2022-07-14 tracey #include <stdio.h>
28 a596b957 2022-07-14 tracey #include <string.h>
29 a596b957 2022-07-14 tracey #include <unistd.h>
31 a596b957 2022-07-14 tracey #include "got_error.h"
32 a596b957 2022-07-14 tracey #include "got_object.h"
33 a596b957 2022-07-14 tracey #include "got_reference.h"
34 a596b957 2022-07-14 tracey #include "got_repository.h"
35 a596b957 2022-07-14 tracey #include "got_path.h"
36 a596b957 2022-07-14 tracey #include "got_cancel.h"
37 a596b957 2022-07-14 tracey #include "got_diff.h"
38 a596b957 2022-07-14 tracey #include "got_commit_graph.h"
39 a596b957 2022-07-14 tracey #include "got_blame.h"
40 a596b957 2022-07-14 tracey #include "got_privsep.h"
42 a596b957 2022-07-14 tracey #include "proc.h"
43 a596b957 2022-07-14 tracey #include "gotwebd.h"
45 a596b957 2022-07-14 tracey static const struct got_error *got_init_repo_commit(struct repo_commit **);
46 a596b957 2022-07-14 tracey static const struct got_error *got_init_repo_tag(struct repo_tag **);
47 a596b957 2022-07-14 tracey static const struct got_error *got_get_repo_commit(struct request *,
48 a596b957 2022-07-14 tracey struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
49 a596b957 2022-07-14 tracey struct got_object_id *);
50 a596b957 2022-07-14 tracey static const struct got_error *got_gotweb_dupfd(int *, int *);
51 a596b957 2022-07-14 tracey static const struct got_error *got_gotweb_openfile(FILE **, int *, 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 587550a5 2023-01-10 op 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 c33c3763 2023-02-03 mark if (*fd == -1)
98 c33c3763 2023-02-03 mark return got_error_from_errno("dup");
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 cb93ab40 2023-01-22 op got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
132 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
133 a596b957 2022-07-14 tracey struct server *srv = c->srv;
134 a596b957 2022-07-14 tracey struct transport *t = c->t;
135 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
136 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
137 a596b957 2022-07-14 tracey struct got_reflist_head refs;
138 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
139 a596b957 2022-07-14 tracey time_t committer_time = 0, cmp_time = 0;
141 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
143 a596b957 2022-07-14 tracey if (srv->show_repo_age == 0)
144 a596b957 2022-07-14 tracey return NULL;
146 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/heads",
147 a596b957 2022-07-14 tracey got_ref_cmp_by_name, NULL);
148 a596b957 2022-07-14 tracey if (error)
149 a596b957 2022-07-14 tracey goto done;
152 a596b957 2022-07-14 tracey * Find the youngest branch tip in the repository, or the age of
153 a596b957 2022-07-14 tracey * the a specific branch tip if a name was provided by the caller.
155 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
156 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
158 a596b957 2022-07-14 tracey if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
159 a596b957 2022-07-14 tracey continue;
161 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, re->ref);
162 a596b957 2022-07-14 tracey if (error)
163 a596b957 2022-07-14 tracey goto done;
165 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
166 a596b957 2022-07-14 tracey free(id);
167 a596b957 2022-07-14 tracey if (error)
168 a596b957 2022-07-14 tracey goto done;
170 a596b957 2022-07-14 tracey committer_time =
171 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
172 a596b957 2022-07-14 tracey got_object_commit_close(commit);
173 a596b957 2022-07-14 tracey if (cmp_time < committer_time)
174 a596b957 2022-07-14 tracey cmp_time = committer_time;
176 a596b957 2022-07-14 tracey if (refname)
180 cb93ab40 2023-01-22 op if (cmp_time != 0)
181 cb93ab40 2023-01-22 op *repo_age = cmp_time;
183 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
184 a596b957 2022-07-14 tracey return error;
187 a596b957 2022-07-14 tracey static const struct got_error *
188 a596b957 2022-07-14 tracey got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
189 a596b957 2022-07-14 tracey struct got_commit_object *commit, struct got_reflist_head *refs,
190 a596b957 2022-07-14 tracey struct got_object_id *id)
192 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
193 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
194 a596b957 2022-07-14 tracey struct got_object_id *id2 = NULL;
195 a596b957 2022-07-14 tracey struct got_object_qid *parent_id;
196 a596b957 2022-07-14 tracey struct transport *t = c->t;
197 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
198 a596b957 2022-07-14 tracey char *commit_msg = NULL, *commit_msg0;
200 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, refs, entry) {
202 a596b957 2022-07-14 tracey const char *name;
203 a596b957 2022-07-14 tracey struct got_tag_object *tag = NULL;
204 a596b957 2022-07-14 tracey struct got_object_id *ref_id;
207 a596b957 2022-07-14 tracey if (got_ref_is_symbolic(re->ref))
208 a596b957 2022-07-14 tracey continue;
210 a596b957 2022-07-14 tracey name = got_ref_get_name(re->ref);
211 a596b957 2022-07-14 tracey if (strncmp(name, "refs/", 5) == 0)
212 a596b957 2022-07-14 tracey name += 5;
213 a596b957 2022-07-14 tracey if (strncmp(name, "got/", 4) == 0)
214 a596b957 2022-07-14 tracey continue;
215 a596b957 2022-07-14 tracey if (strncmp(name, "heads/", 6) == 0)
216 a596b957 2022-07-14 tracey name += 6;
217 a596b957 2022-07-14 tracey if (strncmp(name, "remotes/", 8) == 0) {
218 a596b957 2022-07-14 tracey name += 8;
219 341fa7ca 2022-09-01 op if (strstr(name, "/" GOT_REF_HEAD) != NULL)
220 a596b957 2022-07-14 tracey continue;
222 a596b957 2022-07-14 tracey error = got_ref_resolve(&ref_id, t->repo, re->ref);
223 a596b957 2022-07-14 tracey if (error)
224 a596b957 2022-07-14 tracey return error;
225 a596b957 2022-07-14 tracey if (strncmp(name, "tags/", 5) == 0) {
226 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, t->repo, ref_id);
227 a596b957 2022-07-14 tracey if (error) {
228 a596b957 2022-07-14 tracey if (error->code != GOT_ERR_OBJ_TYPE) {
229 a596b957 2022-07-14 tracey free(ref_id);
230 a596b957 2022-07-14 tracey continue;
233 a596b957 2022-07-14 tracey * Ref points at something other
234 a596b957 2022-07-14 tracey * than a tag.
236 a596b957 2022-07-14 tracey error = NULL;
237 a596b957 2022-07-14 tracey tag = NULL;
240 a596b957 2022-07-14 tracey cmp = got_object_id_cmp(tag ?
241 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag) : ref_id, id);
242 a596b957 2022-07-14 tracey free(ref_id);
244 a596b957 2022-07-14 tracey got_object_tag_close(tag);
245 a596b957 2022-07-14 tracey if (cmp != 0)
246 a596b957 2022-07-14 tracey continue;
247 a596b957 2022-07-14 tracey s = repo_commit->refs_str;
248 a596b957 2022-07-14 tracey if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
249 a596b957 2022-07-14 tracey s ? ", " : "", name) == -1) {
250 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
252 a596b957 2022-07-14 tracey repo_commit->refs_str = NULL;
253 a596b957 2022-07-14 tracey return error;
258 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->commit_id, id);
259 a596b957 2022-07-14 tracey if (error)
260 a596b957 2022-07-14 tracey return error;
262 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->tree_id,
263 a596b957 2022-07-14 tracey got_object_commit_get_tree_id(commit));
264 a596b957 2022-07-14 tracey if (error)
265 a596b957 2022-07-14 tracey return error;
267 a596b957 2022-07-14 tracey if (qs->action == DIFF) {
268 a596b957 2022-07-14 tracey parent_id = STAILQ_FIRST(
269 a596b957 2022-07-14 tracey got_object_commit_get_parent_ids(commit));
270 a596b957 2022-07-14 tracey if (parent_id != NULL) {
271 a596b957 2022-07-14 tracey id2 = got_object_id_dup(&parent_id->id);
272 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->parent_id, id2);
273 a596b957 2022-07-14 tracey if (error)
274 a596b957 2022-07-14 tracey return error;
275 a596b957 2022-07-14 tracey free(id2);
277 a596b957 2022-07-14 tracey repo_commit->parent_id = strdup("/dev/null");
278 a596b957 2022-07-14 tracey if (repo_commit->parent_id == NULL) {
279 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
280 a596b957 2022-07-14 tracey return error;
285 a596b957 2022-07-14 tracey repo_commit->committer_time =
286 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
288 a596b957 2022-07-14 tracey repo_commit->author =
289 a596b957 2022-07-14 tracey strdup(got_object_commit_get_author(commit));
290 a596b957 2022-07-14 tracey if (repo_commit->author == NULL) {
291 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
292 a596b957 2022-07-14 tracey return error;
294 a596b957 2022-07-14 tracey repo_commit->committer =
295 a596b957 2022-07-14 tracey strdup(got_object_commit_get_committer(commit));
296 a596b957 2022-07-14 tracey if (repo_commit->committer == NULL) {
297 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
298 a596b957 2022-07-14 tracey return error;
300 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
301 a596b957 2022-07-14 tracey if (error)
302 a596b957 2022-07-14 tracey return error;
304 a596b957 2022-07-14 tracey commit_msg = commit_msg0;
305 a596b957 2022-07-14 tracey while (*commit_msg == '\n')
306 a596b957 2022-07-14 tracey commit_msg++;
308 a596b957 2022-07-14 tracey repo_commit->commit_msg = strdup(commit_msg);
309 a596b957 2022-07-14 tracey if (repo_commit->commit_msg == NULL)
310 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
311 a596b957 2022-07-14 tracey free(commit_msg0);
312 a596b957 2022-07-14 tracey return error;
315 a596b957 2022-07-14 tracey const struct got_error *
316 a596b957 2022-07-14 tracey got_get_repo_commits(struct request *c, int limit)
318 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
319 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
320 a596b957 2022-07-14 tracey struct got_commit_graph *graph = NULL;
321 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
322 a596b957 2022-07-14 tracey struct got_reflist_head refs;
323 bce44e0b 2022-09-02 op struct got_reference *ref = NULL;
324 a596b957 2022-07-14 tracey struct repo_commit *repo_commit = NULL;
325 a596b957 2022-07-14 tracey struct server *srv = c->srv;
326 a596b957 2022-07-14 tracey struct transport *t = c->t;
327 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
328 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
329 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
330 a596b957 2022-07-14 tracey char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
331 65c2e810 2023-01-22 op int chk_next = 0, chk_multi = 0;
333 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
335 a596b957 2022-07-14 tracey if (qs->file != NULL && strlen(qs->file) > 0)
336 a596b957 2022-07-14 tracey if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
337 a596b957 2022-07-14 tracey qs->file) == -1)
338 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
340 a596b957 2022-07-14 tracey if (asprintf(&repo_path, "%s/%s", srv->repos_path,
341 89ae185c 2022-09-01 op repo_dir->name) == -1) {
342 89ae185c 2022-09-01 op error = got_error_from_errno("asprintf");
346 65c2e810 2023-01-22 op if (qs->commit) {
347 65c2e810 2023-01-22 op error = got_repo_match_object_id_prefix(&id, qs->commit,
348 65c2e810 2023-01-22 op GOT_OBJ_TYPE_COMMIT, repo);
352 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->headref, 0);
353 a596b957 2022-07-14 tracey if (error)
354 a596b957 2022-07-14 tracey goto done;
356 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
357 a596b957 2022-07-14 tracey if (error)
358 a596b957 2022-07-14 tracey goto done;
361 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, repo_path);
362 a596b957 2022-07-14 tracey if (error)
363 a596b957 2022-07-14 tracey goto done;
365 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
366 a596b957 2022-07-14 tracey if (error)
367 a596b957 2022-07-14 tracey goto done;
369 a596b957 2022-07-14 tracey if (qs->file != NULL && strlen(qs->file) > 0) {
370 a596b957 2022-07-14 tracey error = got_commit_graph_open(&graph, file_path, 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_commit_graph_open(&graph, in_repo_path, 0);
375 a596b957 2022-07-14 tracey if (error)
376 a596b957 2022-07-14 tracey goto done;
379 a596b957 2022-07-14 tracey error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
380 a596b957 2022-07-14 tracey if (error)
381 a596b957 2022-07-14 tracey goto done;
383 a596b957 2022-07-14 tracey for (;;) {
384 d9787ed8 2022-09-10 op struct got_object_id next_id;
386 6227cf0e 2022-09-05 op error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
388 a596b957 2022-07-14 tracey if (error) {
389 a596b957 2022-07-14 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
390 a596b957 2022-07-14 tracey error = NULL;
391 a596b957 2022-07-14 tracey goto done;
394 d9787ed8 2022-09-10 op error = got_object_open_as_commit(&commit, repo, &next_id);
395 a596b957 2022-07-14 tracey if (error)
396 a596b957 2022-07-14 tracey goto done;
398 4a962942 2022-09-02 op error = got_init_repo_commit(&repo_commit);
402 a596b957 2022-07-14 tracey error = got_get_repo_commit(c, repo_commit, commit,
403 d9787ed8 2022-09-10 op &refs, &next_id);
405 4f0a80ed 2022-09-07 op gotweb_free_repo_commit(repo_commit);
406 a596b957 2022-07-14 tracey goto done;
409 4f0a80ed 2022-09-07 op TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
411 65c2e810 2023-01-22 op if (!chk_multi || limit != 1 ||
412 65c2e810 2023-01-22 op srv->max_commits_display == 1) {
413 a596b957 2022-07-14 tracey chk_multi = 1;
416 a596b957 2022-07-14 tracey * check for one more commit before breaking,
417 a596b957 2022-07-14 tracey * so we know whether to navigate through briefs
418 a596b957 2022-07-14 tracey * commits and summary
420 a596b957 2022-07-14 tracey if (chk_next && (qs->action == BRIEFS ||
421 a596b957 2022-07-14 tracey qs->action == COMMITS || qs->action == SUMMARY)) {
422 e3662697 2023-02-03 op t->more_id = strdup(repo_commit->commit_id);
423 e3662697 2023-02-03 op if (t->more_id == NULL) {
424 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
425 a596b957 2022-07-14 tracey goto done;
427 2aea6fc2 2023-01-31 op got_object_commit_close(commit);
428 2aea6fc2 2023-01-31 op commit = NULL;
429 4a962942 2022-09-02 op TAILQ_REMOVE(&t->repo_commits, repo_commit,
431 4a962942 2022-09-02 op gotweb_free_repo_commit(repo_commit);
432 a596b957 2022-07-14 tracey goto done;
435 a596b957 2022-07-14 tracey if (error || (limit && --limit == 0)) {
436 65c2e810 2023-01-22 op if (qs->file != NULL && *qs->file != '\0')
437 a596b957 2022-07-14 tracey if (chk_multi == 0)
439 a596b957 2022-07-14 tracey chk_next = 1;
441 a596b957 2022-07-14 tracey if (commit) {
442 a596b957 2022-07-14 tracey got_object_commit_close(commit);
443 a596b957 2022-07-14 tracey commit = NULL;
448 bce44e0b 2022-09-02 op got_ref_close(ref);
449 a596b957 2022-07-14 tracey if (commit)
450 a596b957 2022-07-14 tracey got_object_commit_close(commit);
451 a596b957 2022-07-14 tracey if (graph)
452 a596b957 2022-07-14 tracey got_commit_graph_close(graph);
453 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
454 dfa5768d 2022-09-02 op free(in_repo_path);
455 a596b957 2022-07-14 tracey free(file_path);
456 a596b957 2022-07-14 tracey free(repo_path);
457 a596b957 2022-07-14 tracey free(id);
458 a596b957 2022-07-14 tracey return error;
461 a596b957 2022-07-14 tracey const struct got_error *
462 a596b957 2022-07-14 tracey got_get_repo_tags(struct request *c, int limit)
464 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
465 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
466 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
467 a596b957 2022-07-14 tracey struct got_reflist_head refs;
468 a596b957 2022-07-14 tracey struct got_reference *ref;
469 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
470 a596b957 2022-07-14 tracey struct server *srv = c->srv;
471 a596b957 2022-07-14 tracey struct transport *t = c->t;
472 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
473 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
474 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
475 a596b957 2022-07-14 tracey struct got_tag_object *tag = NULL;
476 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
477 a596b957 2022-07-14 tracey char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
478 625e5896 2022-09-01 op char *tag_commit = NULL, *tag_commit0 = NULL;
479 a596b957 2022-07-14 tracey char *commit_msg = NULL, *commit_msg0 = NULL;
480 a596b957 2022-07-14 tracey int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
482 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
484 a596b957 2022-07-14 tracey if (asprintf(&repo_path, "%s/%s", srv->repos_path,
485 a596b957 2022-07-14 tracey repo_dir->name) == -1)
486 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
488 1abb18e1 2022-12-20 op if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
489 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->headref, 0);
490 a596b957 2022-07-14 tracey if (error)
491 a596b957 2022-07-14 tracey goto err;
492 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
493 a596b957 2022-07-14 tracey got_ref_close(ref);
494 a596b957 2022-07-14 tracey if (error)
495 a596b957 2022-07-14 tracey goto err;
496 a596b957 2022-07-14 tracey } else if (qs->commit == NULL && qs->action == TAG) {
497 a596b957 2022-07-14 tracey error = got_error_msg(GOT_ERR_EOF, "commit id missing");
498 a596b957 2022-07-14 tracey goto err;
500 a596b957 2022-07-14 tracey error = got_repo_match_object_id_prefix(&id, qs->commit,
501 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, repo);
502 a596b957 2022-07-14 tracey if (error)
503 a596b957 2022-07-14 tracey goto err;
506 a596b957 2022-07-14 tracey if (qs->action != SUMMARY && qs->action != TAGS) {
507 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
508 a596b957 2022-07-14 tracey if (error)
509 a596b957 2022-07-14 tracey goto err;
510 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
511 a596b957 2022-07-14 tracey if (error)
512 a596b957 2022-07-14 tracey goto err;
513 a596b957 2022-07-14 tracey if (commit) {
514 a596b957 2022-07-14 tracey got_object_commit_close(commit);
515 a596b957 2022-07-14 tracey commit = NULL;
519 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, repo_path);
520 a596b957 2022-07-14 tracey if (error)
521 a596b957 2022-07-14 tracey goto err;
523 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
525 a596b957 2022-07-14 tracey if (error)
526 a596b957 2022-07-14 tracey goto err;
528 a596b957 2022-07-14 tracey if (limit == 1)
529 a596b957 2022-07-14 tracey chk_multi = 0;
532 a596b957 2022-07-14 tracey * XXX: again, see previous message about caching
535 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
536 a596b957 2022-07-14 tracey struct repo_tag *new_repo_tag = NULL;
537 a596b957 2022-07-14 tracey error = got_init_repo_tag(&new_repo_tag);
538 a596b957 2022-07-14 tracey if (error)
539 a596b957 2022-07-14 tracey goto err;
541 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
543 a596b957 2022-07-14 tracey new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
544 a596b957 2022-07-14 tracey if (new_repo_tag->tag_name == NULL) {
545 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
546 a596b957 2022-07-14 tracey goto err;
552 b163541d 2022-09-02 op free(id_str);
553 b163541d 2022-09-02 op id_str = NULL;
555 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, re->ref);
556 a596b957 2022-07-14 tracey if (error)
557 a596b957 2022-07-14 tracey goto done;
560 bc95141c 2022-09-02 op got_object_tag_close(tag);
561 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, repo, id);
562 a596b957 2022-07-14 tracey if (error) {
563 b163541d 2022-09-02 op if (error->code != GOT_ERR_OBJ_TYPE)
564 a596b957 2022-07-14 tracey goto done;
565 a596b957 2022-07-14 tracey /* "lightweight" tag */
566 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
568 a596b957 2022-07-14 tracey goto done;
569 a596b957 2022-07-14 tracey new_repo_tag->tagger =
570 a596b957 2022-07-14 tracey strdup(got_object_commit_get_committer(commit));
571 a596b957 2022-07-14 tracey if (new_repo_tag->tagger == NULL) {
572 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
573 a596b957 2022-07-14 tracey goto err;
575 a596b957 2022-07-14 tracey new_repo_tag->tagger_time =
576 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
577 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str, id);
578 a596b957 2022-07-14 tracey if (error)
579 a596b957 2022-07-14 tracey goto err;
581 a596b957 2022-07-14 tracey new_repo_tag->tagger =
582 a596b957 2022-07-14 tracey strdup(got_object_tag_get_tagger(tag));
583 a596b957 2022-07-14 tracey if (new_repo_tag->tagger == NULL) {
584 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
585 a596b957 2022-07-14 tracey goto err;
587 a596b957 2022-07-14 tracey new_repo_tag->tagger_time =
588 a596b957 2022-07-14 tracey got_object_tag_get_tagger_time(tag);
589 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str,
590 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag));
591 a596b957 2022-07-14 tracey if (error)
592 a596b957 2022-07-14 tracey goto err;
595 a596b957 2022-07-14 tracey new_repo_tag->commit_id = strdup(id_str);
596 a596b957 2022-07-14 tracey if (new_repo_tag->commit_id == NULL)
597 a596b957 2022-07-14 tracey goto err;
599 a596b957 2022-07-14 tracey if (commit_found == 0 && qs->commit != NULL &&
600 a596b957 2022-07-14 tracey strncmp(id_str, qs->commit, strlen(id_str)) != 0)
601 a596b957 2022-07-14 tracey continue;
603 a596b957 2022-07-14 tracey commit_found = 1;
605 a596b957 2022-07-14 tracey t->tag_count++;
608 a596b957 2022-07-14 tracey * check for one more commit before breaking,
609 a596b957 2022-07-14 tracey * so we know whether to navigate through briefs
610 a596b957 2022-07-14 tracey * commits and summary
612 a596b957 2022-07-14 tracey if (chk_next) {
613 a596b957 2022-07-14 tracey t->next_id = strdup(new_repo_tag->commit_id);
614 a596b957 2022-07-14 tracey if (t->next_id == NULL) {
615 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
616 a596b957 2022-07-14 tracey goto err;
618 a596b957 2022-07-14 tracey if (commit) {
619 a596b957 2022-07-14 tracey got_object_commit_close(commit);
620 a596b957 2022-07-14 tracey commit = NULL;
622 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
623 a596b957 2022-07-14 tracey gotweb_free_repo_tag(new_repo_tag);
624 a596b957 2022-07-14 tracey goto done;
627 a596b957 2022-07-14 tracey if (commit) {
628 625e5896 2022-09-01 op error = got_object_commit_get_logmsg(&tag_commit0,
630 a596b957 2022-07-14 tracey if (error)
632 a596b957 2022-07-14 tracey got_object_commit_close(commit);
633 a596b957 2022-07-14 tracey commit = NULL;
635 625e5896 2022-09-01 op tag_commit0 = strdup(got_object_tag_get_message(tag));
636 625e5896 2022-09-01 op if (tag_commit0 == NULL) {
637 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
642 625e5896 2022-09-01 op tag_commit = tag_commit0;
643 625e5896 2022-09-01 op while (*tag_commit == '\n')
644 625e5896 2022-09-01 op tag_commit++;
645 625e5896 2022-09-01 op new_repo_tag->tag_commit = strdup(tag_commit);
646 625e5896 2022-09-01 op if (new_repo_tag->tag_commit == NULL) {
647 625e5896 2022-09-01 op error = got_error_from_errno("strdup");
648 625e5896 2022-09-01 op free(tag_commit0);
651 625e5896 2022-09-01 op free(tag_commit0);
653 a596b957 2022-07-14 tracey if (qs->action != SUMMARY && qs->action != TAGS) {
654 a596b957 2022-07-14 tracey commit_msg = commit_msg0;
655 a596b957 2022-07-14 tracey while (*commit_msg == '\n')
656 a596b957 2022-07-14 tracey commit_msg++;
658 a596b957 2022-07-14 tracey new_repo_tag->commit_msg = strdup(commit_msg);
659 a596b957 2022-07-14 tracey if (new_repo_tag->commit_msg == NULL) {
660 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
661 a596b957 2022-07-14 tracey goto err;
665 a596b957 2022-07-14 tracey if (limit && --limit == 0) {
666 a596b957 2022-07-14 tracey if (chk_multi == 0)
668 a596b957 2022-07-14 tracey chk_next = 1;
674 a596b957 2022-07-14 tracey * we have tailq populated, so find previous commit id
675 a596b957 2022-07-14 tracey * for navigation through briefs and commits
677 a596b957 2022-07-14 tracey if (t->tag_count == 0) {
678 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
679 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
680 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
683 a596b957 2022-07-14 tracey if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
684 a596b957 2022-07-14 tracey commit_found = 0;
685 a596b957 2022-07-14 tracey TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
687 a596b957 2022-07-14 tracey if (commit_found == 0 && rt->commit_id != NULL &&
688 a596b957 2022-07-14 tracey strcmp(qs->commit, rt->commit_id) != 0) {
689 a596b957 2022-07-14 tracey continue;
691 a596b957 2022-07-14 tracey commit_found = 1;
692 a596b957 2022-07-14 tracey if (c_cnt == srv->max_commits_display ||
693 a596b957 2022-07-14 tracey rt == TAILQ_FIRST(&t->repo_tags)) {
694 a596b957 2022-07-14 tracey t->prev_id = strdup(rt->commit_id);
695 a596b957 2022-07-14 tracey if (t->prev_id == NULL)
696 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
703 a596b957 2022-07-14 tracey if (commit)
704 a596b957 2022-07-14 tracey got_object_commit_close(commit);
706 b163541d 2022-09-02 op got_object_tag_close(tag);
707 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
708 5a57034b 2022-09-02 op free(commit_msg0);
709 b163541d 2022-09-02 op free(in_repo_path);
710 a596b957 2022-07-14 tracey free(repo_path);
711 a596b957 2022-07-14 tracey free(id);
712 b163541d 2022-09-02 op free(id_str);
713 a596b957 2022-07-14 tracey return error;
717 43d421de 2023-01-05 op got_output_repo_tree(struct request *c,
718 43d421de 2023-01-05 op int (*cb)(struct template *, struct got_tree_entry *))
720 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
721 a596b957 2022-07-14 tracey struct transport *t = c->t;
722 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
723 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
724 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
725 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
726 a596b957 2022-07-14 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
727 a596b957 2022-07-14 tracey struct got_reflist_head refs;
728 a596b957 2022-07-14 tracey struct got_tree_object *tree = NULL;
729 43d421de 2023-01-05 op struct got_tree_entry *te;
730 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
731 e5e662e4 2022-09-02 op char *escaped_name = NULL, *path = NULL;
732 43d421de 2023-01-05 op int nentries, i;
734 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
736 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
738 a596b957 2022-07-14 tracey if (qs->folder != NULL) {
739 a596b957 2022-07-14 tracey path = strdup(qs->folder);
740 a596b957 2022-07-14 tracey if (path == NULL) {
741 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
742 a596b957 2022-07-14 tracey goto done;
745 6977f45a 2022-09-02 op error = got_repo_map_path(&path, repo, repo_dir->path);
746 a596b957 2022-07-14 tracey if (error)
747 a596b957 2022-07-14 tracey goto done;
750 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
751 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
752 a596b957 2022-07-14 tracey if (error)
753 a596b957 2022-07-14 tracey goto done;
755 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
756 a596b957 2022-07-14 tracey if (error)
757 a596b957 2022-07-14 tracey goto done;
759 a596b957 2022-07-14 tracey error = got_object_id_by_path(&tree_id, repo, commit, path);
760 a596b957 2022-07-14 tracey if (error)
761 a596b957 2022-07-14 tracey goto done;
763 a596b957 2022-07-14 tracey error = got_object_open_as_tree(&tree, repo, tree_id);
764 a596b957 2022-07-14 tracey if (error)
765 a596b957 2022-07-14 tracey goto done;
767 a596b957 2022-07-14 tracey nentries = got_object_tree_get_nentries(tree);
769 a596b957 2022-07-14 tracey for (i = 0; i < nentries; i++) {
770 a596b957 2022-07-14 tracey te = got_object_tree_get_entry(tree, i);
771 01392bd6 2023-02-10 tracey if (cb(c->tp, te) == -1) {
772 01392bd6 2023-02-10 tracey error = got_error(GOT_ERR_CANCELLED);
777 b7efc9b3 2022-08-25 op free(escaped_name);
778 a596b957 2022-07-14 tracey free(path);
779 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
780 a596b957 2022-07-14 tracey if (commit)
781 a596b957 2022-07-14 tracey got_object_commit_close(commit);
783 58354f54 2022-09-06 op got_object_tree_close(tree);
784 a596b957 2022-07-14 tracey free(commit_id);
785 a596b957 2022-07-14 tracey free(tree_id);
787 43d421de 2023-01-05 op log_warnx("%s: %s", __func__, error->msg);
793 a596b957 2022-07-14 tracey const struct got_error *
794 298f95fb 2023-01-05 op got_open_blob_for_output(struct got_blob_object **blob, int *fd,
795 298f95fb 2023-01-05 op int *binary, struct request *c)
797 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
798 a596b957 2022-07-14 tracey struct transport *t = c->t;
799 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
800 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
801 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
802 a596b957 2022-07-14 tracey struct got_object_id *commit_id = NULL;
803 a596b957 2022-07-14 tracey struct got_reflist_head refs;
804 a596b957 2022-07-14 tracey char *path = NULL, *in_repo_path = NULL;
805 298f95fb 2023-01-05 op int obj_type;
807 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
809 298f95fb 2023-01-05 op *blob = NULL;
813 a596b957 2022-07-14 tracey if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
814 a596b957 2022-07-14 tracey qs->folder ? "/" : "", qs->file) == -1) {
815 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
816 a596b957 2022-07-14 tracey goto done;
819 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, path);
820 a596b957 2022-07-14 tracey if (error)
821 a596b957 2022-07-14 tracey goto done;
823 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
824 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
825 a596b957 2022-07-14 tracey if (error)
826 a596b957 2022-07-14 tracey goto done;
828 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
829 a596b957 2022-07-14 tracey if (error)
830 a596b957 2022-07-14 tracey goto done;
832 a596b957 2022-07-14 tracey error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
833 a596b957 2022-07-14 tracey if (error)
834 a596b957 2022-07-14 tracey goto done;
836 a596b957 2022-07-14 tracey if (commit_id == NULL) {
837 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_NO_OBJ);
838 a596b957 2022-07-14 tracey goto done;
841 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, commit_id);
842 a596b957 2022-07-14 tracey if (error)
843 a596b957 2022-07-14 tracey goto done;
845 a596b957 2022-07-14 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
846 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
847 a596b957 2022-07-14 tracey goto done;
850 298f95fb 2023-01-05 op error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
851 a596b957 2022-07-14 tracey if (error)
852 a596b957 2022-07-14 tracey goto done;
854 298f95fb 2023-01-05 op error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
855 a596b957 2022-07-14 tracey if (error)
856 a596b957 2022-07-14 tracey goto done;
858 298f95fb 2023-01-05 op error = got_object_blob_is_binary(binary, *blob);
864 298f95fb 2023-01-05 op got_object_commit_close(commit);
867 298f95fb 2023-01-05 op if (*fd != -1)
870 298f95fb 2023-01-05 op got_object_blob_close(*blob);
872 298f95fb 2023-01-05 op *blob = NULL;
875 298f95fb 2023-01-05 op free(in_repo_path);
876 298f95fb 2023-01-05 op free(commit_id);
878 a596b957 2022-07-14 tracey return error;
882 298f95fb 2023-01-05 op got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
883 298f95fb 2023-01-05 op int (*cb)(struct template *, const char *, size_t))
885 298f95fb 2023-01-05 op const struct got_error *err;
886 298f95fb 2023-01-05 op char *line = NULL;
887 298f95fb 2023-01-05 op size_t linesize = 0;
888 298f95fb 2023-01-05 op size_t lineno = 0;
889 298f95fb 2023-01-05 op ssize_t linelen = 0;
892 298f95fb 2023-01-05 op err = got_object_blob_getline(&line, &linelen, &linesize,
894 298f95fb 2023-01-05 op if (err || linelen == -1)
897 01392bd6 2023-02-10 tracey if (cb(tp, line, lineno) == -1) {
898 01392bd6 2023-02-10 tracey err = got_error(GOT_ERR_CANCELLED);
906 298f95fb 2023-01-05 op log_warnx("%s: got_object_blob_getline failed: %s",
907 298f95fb 2023-01-05 op __func__, err->msg);
913 a596b957 2022-07-14 tracey struct blame_cb_args {
914 a596b957 2022-07-14 tracey struct blame_line *lines;
915 a596b957 2022-07-14 tracey int nlines;
916 a596b957 2022-07-14 tracey int nlines_prec;
917 a596b957 2022-07-14 tracey int lineno_cur;
918 a596b957 2022-07-14 tracey off_t *line_offsets;
920 a596b957 2022-07-14 tracey struct got_repository *repo;
921 a596b957 2022-07-14 tracey struct request *c;
922 8319855f 2023-01-15 op got_render_blame_line_cb cb;
925 a596b957 2022-07-14 tracey static const struct got_error *
926 a596b957 2022-07-14 tracey got_gotweb_blame_cb(void *arg, int nlines, int lineno,
927 a596b957 2022-07-14 tracey struct got_commit_object *commit, struct got_object_id *id)
929 a596b957 2022-07-14 tracey const struct got_error *err = NULL;
930 a596b957 2022-07-14 tracey struct blame_cb_args *a = arg;
931 a596b957 2022-07-14 tracey struct blame_line *bline;
932 a596b957 2022-07-14 tracey struct request *c = a->c;
933 8319855f 2023-01-15 op char *line = NULL;
934 a596b957 2022-07-14 tracey size_t linesize = 0;
935 a596b957 2022-07-14 tracey off_t offset;
936 a596b957 2022-07-14 tracey struct tm tm;
937 a596b957 2022-07-14 tracey time_t committer_time;
939 a596b957 2022-07-14 tracey if (nlines != a->nlines ||
940 a596b957 2022-07-14 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
941 a596b957 2022-07-14 tracey return got_error(GOT_ERR_RANGE);
943 a596b957 2022-07-14 tracey if (lineno == -1)
944 a596b957 2022-07-14 tracey return NULL; /* no change in this commit */
946 a596b957 2022-07-14 tracey /* Annotate this line. */
947 a596b957 2022-07-14 tracey bline = &a->lines[lineno - 1];
948 a596b957 2022-07-14 tracey if (bline->annotated)
949 a596b957 2022-07-14 tracey return NULL;
950 a596b957 2022-07-14 tracey err = got_object_id_str(&bline->id_str, id);
952 a596b957 2022-07-14 tracey return err;
954 a596b957 2022-07-14 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
955 a596b957 2022-07-14 tracey if (bline->committer == NULL) {
956 a596b957 2022-07-14 tracey err = got_error_from_errno("strdup");
957 a596b957 2022-07-14 tracey goto done;
960 a596b957 2022-07-14 tracey committer_time = got_object_commit_get_committer_time(commit);
961 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
962 a596b957 2022-07-14 tracey return got_error_from_errno("gmtime_r");
963 a596b957 2022-07-14 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
964 a596b957 2022-07-14 tracey &tm) == 0) {
965 a596b957 2022-07-14 tracey err = got_error(GOT_ERR_NO_SPACE);
966 a596b957 2022-07-14 tracey goto done;
968 a596b957 2022-07-14 tracey bline->annotated = 1;
970 a596b957 2022-07-14 tracey /* Print lines annotated so far. */
971 a596b957 2022-07-14 tracey bline = &a->lines[a->lineno_cur - 1];
972 a596b957 2022-07-14 tracey if (!bline->annotated)
973 a596b957 2022-07-14 tracey goto done;
975 a596b957 2022-07-14 tracey offset = a->line_offsets[a->lineno_cur - 1];
976 a596b957 2022-07-14 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
977 a596b957 2022-07-14 tracey err = got_error_from_errno("fseeko");
978 a596b957 2022-07-14 tracey goto done;
981 85f2c2e0 2022-08-18 op while (a->lineno_cur <= a->nlines && bline->annotated) {
982 a596b957 2022-07-14 tracey if (getline(&line, &linesize, a->f) == -1) {
983 a596b957 2022-07-14 tracey if (ferror(a->f))
984 a596b957 2022-07-14 tracey err = got_error_from_errno("getline");
988 8319855f 2023-01-15 op if (a->cb(c->tp, line, bline, a->nlines_prec,
989 4f152e84 2023-02-10 op a->lineno_cur) == -1) {
990 4f152e84 2023-02-10 op err = got_error(GOT_ERR_CANCELLED);
994 a596b957 2022-07-14 tracey a->lineno_cur++;
995 a596b957 2022-07-14 tracey bline = &a->lines[a->lineno_cur - 1];
998 a596b957 2022-07-14 tracey free(line);
999 a596b957 2022-07-14 tracey return err;
1002 a596b957 2022-07-14 tracey const struct got_error *
1003 8319855f 2023-01-15 op got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1005 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1006 a596b957 2022-07-14 tracey struct transport *t = c->t;
1007 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1008 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
1009 a596b957 2022-07-14 tracey struct got_object_id *obj_id = NULL, *commit_id = NULL;
1010 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
1011 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1012 a596b957 2022-07-14 tracey struct got_blob_object *blob = NULL;
1013 a596b957 2022-07-14 tracey char *path = NULL, *in_repo_path = NULL;
1014 a596b957 2022-07-14 tracey struct blame_cb_args bca;
1015 a596b957 2022-07-14 tracey int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1016 a596b957 2022-07-14 tracey int fd6 = -1;
1017 a596b957 2022-07-14 tracey off_t filesize;
1018 a596b957 2022-07-14 tracey FILE *f1 = NULL, *f2 = NULL;
1020 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1021 a596b957 2022-07-14 tracey bca.f = NULL;
1022 a596b957 2022-07-14 tracey bca.lines = NULL;
1023 8319855f 2023-01-15 op bca.cb = cb;
1025 a596b957 2022-07-14 tracey if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1026 a596b957 2022-07-14 tracey qs->folder ? "/" : "", qs->file) == -1) {
1027 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1028 a596b957 2022-07-14 tracey goto done;
1031 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, path);
1032 a596b957 2022-07-14 tracey if (error)
1033 a596b957 2022-07-14 tracey goto done;
1035 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1036 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
1037 a596b957 2022-07-14 tracey if (error)
1038 a596b957 2022-07-14 tracey goto done;
1040 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
1041 a596b957 2022-07-14 tracey if (error)
1042 a596b957 2022-07-14 tracey goto done;
1044 a596b957 2022-07-14 tracey error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1045 a596b957 2022-07-14 tracey if (error)
1046 a596b957 2022-07-14 tracey goto done;
1048 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, obj_id);
1049 a596b957 2022-07-14 tracey if (error)
1050 a596b957 2022-07-14 tracey goto done;
1052 a596b957 2022-07-14 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
1053 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1054 a596b957 2022-07-14 tracey goto done;
1057 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1058 a596b957 2022-07-14 tracey if (error)
1059 a596b957 2022-07-14 tracey goto done;
1061 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1062 a596b957 2022-07-14 tracey if (error)
1063 a596b957 2022-07-14 tracey goto done;
1065 a596b957 2022-07-14 tracey error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1066 a596b957 2022-07-14 tracey if (error)
1067 a596b957 2022-07-14 tracey goto done;
1069 a596b957 2022-07-14 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1070 a596b957 2022-07-14 tracey &bca.line_offsets, bca.f, blob);
1071 a596b957 2022-07-14 tracey if (error || bca.nlines == 0)
1072 a596b957 2022-07-14 tracey goto done;
1074 a596b957 2022-07-14 tracey /* Don't include \n at EOF in the blame line count. */
1075 a596b957 2022-07-14 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
1076 a596b957 2022-07-14 tracey bca.nlines--;
1078 a596b957 2022-07-14 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1079 a596b957 2022-07-14 tracey if (bca.lines == NULL) {
1080 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1081 a596b957 2022-07-14 tracey goto done;
1083 a596b957 2022-07-14 tracey bca.lineno_cur = 1;
1084 a596b957 2022-07-14 tracey bca.nlines_prec = 0;
1085 a596b957 2022-07-14 tracey i = bca.nlines;
1086 a596b957 2022-07-14 tracey while (i > 0) {
1087 a596b957 2022-07-14 tracey i /= 10;
1088 a596b957 2022-07-14 tracey bca.nlines_prec++;
1090 a596b957 2022-07-14 tracey bca.repo = repo;
1091 a596b957 2022-07-14 tracey bca.c = c;
1093 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1094 a596b957 2022-07-14 tracey if (error)
1095 a596b957 2022-07-14 tracey goto done;
1097 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1098 a596b957 2022-07-14 tracey if (error)
1099 a596b957 2022-07-14 tracey goto done;
1101 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1102 a596b957 2022-07-14 tracey if (error)
1103 a596b957 2022-07-14 tracey goto done;
1105 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1106 a596b957 2022-07-14 tracey if (error)
1107 a596b957 2022-07-14 tracey goto done;
1109 a596b957 2022-07-14 tracey error = got_blame(in_repo_path, commit_id, repo,
1110 a596b957 2022-07-14 tracey GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1111 a596b957 2022-07-14 tracey fd3, fd4, f1, f2);
1114 a33a44db 2022-09-03 op if (bca.lines) {
1115 a596b957 2022-07-14 tracey free(bca.line_offsets);
1116 a596b957 2022-07-14 tracey for (i = 0; i < bca.nlines; i++) {
1117 a596b957 2022-07-14 tracey struct blame_line *bline = &bca.lines[i];
1118 a596b957 2022-07-14 tracey free(bline->id_str);
1119 a596b957 2022-07-14 tracey free(bline->committer);
1122 a596b957 2022-07-14 tracey free(bca.lines);
1123 a596b957 2022-07-14 tracey if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1124 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1125 a596b957 2022-07-14 tracey if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1126 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1127 a596b957 2022-07-14 tracey if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1128 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1129 a596b957 2022-07-14 tracey if (bca.f) {
1130 a596b957 2022-07-14 tracey const struct got_error *bca_err =
1131 a596b957 2022-07-14 tracey got_gotweb_flushfile(bca.f, fd1);
1132 a596b957 2022-07-14 tracey if (error == NULL)
1133 a596b957 2022-07-14 tracey error = bca_err;
1135 a596b957 2022-07-14 tracey if (f1) {
1136 a596b957 2022-07-14 tracey const struct got_error *f1_err =
1137 a596b957 2022-07-14 tracey got_gotweb_flushfile(f1, fd5);
1138 a596b957 2022-07-14 tracey if (error == NULL)
1139 a596b957 2022-07-14 tracey error = f1_err;
1141 a596b957 2022-07-14 tracey if (f2) {
1142 a596b957 2022-07-14 tracey const struct got_error *f2_err =
1143 a596b957 2022-07-14 tracey got_gotweb_flushfile(f2, fd6);
1144 a596b957 2022-07-14 tracey if (error == NULL)
1145 a596b957 2022-07-14 tracey error = f2_err;
1147 a596b957 2022-07-14 tracey if (commit)
1148 a596b957 2022-07-14 tracey got_object_commit_close(commit);
1149 a596b957 2022-07-14 tracey if (blob)
1150 a596b957 2022-07-14 tracey got_object_blob_close(blob);
1151 a596b957 2022-07-14 tracey free(in_repo_path);
1152 a596b957 2022-07-14 tracey free(commit_id);
1153 b94206d0 2022-09-03 op free(obj_id);
1154 a596b957 2022-07-14 tracey free(path);
1155 b94206d0 2022-09-03 op got_ref_list_free(&refs);
1156 a596b957 2022-07-14 tracey return error;
1159 a596b957 2022-07-14 tracey const struct got_error *
1160 587550a5 2023-01-10 op got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1162 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1163 a596b957 2022-07-14 tracey struct transport *t = c->t;
1164 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1165 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
1166 a596b957 2022-07-14 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1167 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1168 a596b957 2022-07-14 tracey FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1169 a596b957 2022-07-14 tracey int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1174 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1176 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1177 a596b957 2022-07-14 tracey if (error)
1178 a596b957 2022-07-14 tracey return error;
1180 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1181 a596b957 2022-07-14 tracey if (error)
1182 a596b957 2022-07-14 tracey return error;
1184 a596b957 2022-07-14 tracey error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1185 a596b957 2022-07-14 tracey if (error)
1186 a596b957 2022-07-14 tracey return error;
1188 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
1190 a596b957 2022-07-14 tracey if (rc->parent_id != NULL &&
1191 a596b957 2022-07-14 tracey strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1192 587550a5 2023-01-10 op error = got_repo_match_object_id(&id1, NULL,
1193 a596b957 2022-07-14 tracey rc->parent_id, GOT_OBJ_TYPE_ANY,
1194 a596b957 2022-07-14 tracey &refs, repo);
1195 a596b957 2022-07-14 tracey if (error)
1196 a596b957 2022-07-14 tracey goto done;
1199 587550a5 2023-01-10 op error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1200 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_ANY, &refs, repo);
1201 a596b957 2022-07-14 tracey if (error)
1202 a596b957 2022-07-14 tracey goto done;
1204 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, id2);
1205 a596b957 2022-07-14 tracey if (error)
1206 a596b957 2022-07-14 tracey goto done;
1208 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1209 a596b957 2022-07-14 tracey if (error)
1210 a596b957 2022-07-14 tracey goto done;
1212 a596b957 2022-07-14 tracey error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1213 a596b957 2022-07-14 tracey if (error)
1214 a596b957 2022-07-14 tracey goto done;
1216 a596b957 2022-07-14 tracey switch (obj_type) {
1217 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_BLOB:
1218 a596b957 2022-07-14 tracey error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1219 a596b957 2022-07-14 tracey id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1220 1f3405c9 2023-01-17 mark NULL, repo, f3);
1222 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_TREE:
1223 a596b957 2022-07-14 tracey error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1224 a596b957 2022-07-14 tracey id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1225 1f3405c9 2023-01-17 mark NULL, repo, f3);
1227 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_COMMIT:
1228 a596b957 2022-07-14 tracey error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1229 a596b957 2022-07-14 tracey fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1230 1f3405c9 2023-01-17 mark NULL, repo, f3);
1232 a596b957 2022-07-14 tracey default:
1233 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1235 a596b957 2022-07-14 tracey if (error)
1236 a596b957 2022-07-14 tracey goto done;
1238 a596b957 2022-07-14 tracey if (fseek(f1, 0, SEEK_SET) == -1) {
1239 a596b957 2022-07-14 tracey error = got_ferror(f1, GOT_ERR_IO);
1240 a596b957 2022-07-14 tracey goto done;
1243 a596b957 2022-07-14 tracey if (fseek(f2, 0, SEEK_SET) == -1) {
1244 a596b957 2022-07-14 tracey error = got_ferror(f2, GOT_ERR_IO);
1245 a596b957 2022-07-14 tracey goto done;
1248 a596b957 2022-07-14 tracey if (fseek(f3, 0, SEEK_SET) == -1) {
1249 a596b957 2022-07-14 tracey error = got_ferror(f3, GOT_ERR_IO);
1250 a596b957 2022-07-14 tracey goto done;
1257 a596b957 2022-07-14 tracey if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1258 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1259 a596b957 2022-07-14 tracey if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1260 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1261 a596b957 2022-07-14 tracey if (f1) {
1262 a596b957 2022-07-14 tracey const struct got_error *f1_err =
1263 a596b957 2022-07-14 tracey got_gotweb_flushfile(f1, fd1);
1264 a596b957 2022-07-14 tracey if (error == NULL)
1265 a596b957 2022-07-14 tracey error = f1_err;
1267 a596b957 2022-07-14 tracey if (f2) {
1268 a596b957 2022-07-14 tracey const struct got_error *f2_err =
1269 a596b957 2022-07-14 tracey got_gotweb_flushfile(f2, fd2);
1270 a596b957 2022-07-14 tracey if (error == NULL)
1271 a596b957 2022-07-14 tracey error = f2_err;
1273 587550a5 2023-01-10 op if (error && f3) {
1274 587550a5 2023-01-10 op got_gotweb_flushfile(f3, fd3);
1278 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
1279 a596b957 2022-07-14 tracey free(id1);
1280 a596b957 2022-07-14 tracey free(id2);
1281 a596b957 2022-07-14 tracey return error;
1284 a596b957 2022-07-14 tracey static const struct got_error *
1285 a596b957 2022-07-14 tracey got_init_repo_commit(struct repo_commit **rc)
1287 a596b957 2022-07-14 tracey *rc = calloc(1, sizeof(**rc));
1288 a596b957 2022-07-14 tracey if (*rc == NULL)
1289 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
1291 a596b957 2022-07-14 tracey (*rc)->path = NULL;
1292 a596b957 2022-07-14 tracey (*rc)->refs_str = NULL;
1293 a596b957 2022-07-14 tracey (*rc)->commit_id = NULL;
1294 a596b957 2022-07-14 tracey (*rc)->committer = NULL;
1295 a596b957 2022-07-14 tracey (*rc)->author = NULL;
1296 a596b957 2022-07-14 tracey (*rc)->parent_id = NULL;
1297 a596b957 2022-07-14 tracey (*rc)->tree_id = NULL;
1298 a596b957 2022-07-14 tracey (*rc)->commit_msg = NULL;
1300 dd84f505 2022-08-31 stsp return NULL;
1303 a596b957 2022-07-14 tracey static const struct got_error *
1304 a596b957 2022-07-14 tracey got_init_repo_tag(struct repo_tag **rt)
1306 a596b957 2022-07-14 tracey *rt = calloc(1, sizeof(**rt));
1307 a596b957 2022-07-14 tracey if (*rt == NULL)
1308 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
1310 a596b957 2022-07-14 tracey (*rt)->commit_id = NULL;
1311 a596b957 2022-07-14 tracey (*rt)->tag_name = NULL;
1312 a596b957 2022-07-14 tracey (*rt)->tag_commit = NULL;
1313 a596b957 2022-07-14 tracey (*rt)->commit_msg = NULL;
1314 a596b957 2022-07-14 tracey (*rt)->tagger = NULL;
1316 dd84f505 2022-08-31 stsp return NULL;