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 "gotwebd.h"
43 1220d7ea 2024-05-21 op #include "log.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 18069c98 2023-05-16 op static const struct got_error *got_gotweb_openfile(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 587550a5 2023-01-10 op const struct got_error *
56 24a4d801 2023-05-16 op got_gotweb_closefile(FILE *f)
58 08ea2d1b 2023-05-16 op const struct got_error *err = NULL;
60 a596b957 2022-07-14 tracey if (fseek(f, 0, SEEK_SET) == -1)
61 08ea2d1b 2023-05-16 op err = got_error_from_errno("fseek");
63 fae3f56c 2024-07-08 op if (ftruncate(fileno(f), 0) == -1 && err == NULL)
64 08ea2d1b 2023-05-16 op err = got_error_from_errno("ftruncate");
66 08ea2d1b 2023-05-16 op if (fclose(f) == EOF && err == NULL)
67 08ea2d1b 2023-05-16 op err = got_error_from_errno("fclose");
72 a596b957 2022-07-14 tracey static const struct got_error *
73 18069c98 2023-05-16 op got_gotweb_openfile(FILE **f, int *priv_fd)
77 18069c98 2023-05-16 op fd = dup(*priv_fd);
79 e9d3ad59 2022-08-31 stsp return got_error_from_errno("dup");
81 18069c98 2023-05-16 op *f = fdopen(fd, "w+");
82 a596b957 2022-07-14 tracey if (*f == NULL) {
84 dd84f505 2022-08-31 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
87 dd84f505 2022-08-31 stsp return NULL;
90 a596b957 2022-07-14 tracey static const struct got_error *
91 a596b957 2022-07-14 tracey got_gotweb_dupfd(int *priv_fd, int *fd)
93 a596b957 2022-07-14 tracey *fd = dup(*priv_fd);
95 c33c3763 2023-02-03 mark if (*fd == -1)
96 c33c3763 2023-02-03 mark return got_error_from_errno("dup");
98 dd84f505 2022-08-31 stsp return NULL;
101 a596b957 2022-07-14 tracey const struct got_error *
102 c127fc49 2022-11-22 op got_get_repo_owner(char **owner, struct request *c)
104 a596b957 2022-07-14 tracey struct transport *t = c->t;
105 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
106 a596b957 2022-07-14 tracey const char *gitconfig_owner;
108 a596b957 2022-07-14 tracey *owner = NULL;
109 a596b957 2022-07-14 tracey gitconfig_owner = got_repo_get_gitconfig_owner(repo);
110 a596b957 2022-07-14 tracey if (gitconfig_owner) {
111 a596b957 2022-07-14 tracey *owner = strdup(gitconfig_owner);
112 1999985f 2022-08-30 tracey if (*owner == NULL)
113 1999985f 2022-08-30 tracey return got_error_from_errno("strdup");
116 dd84f505 2022-08-31 stsp return NULL;
119 a596b957 2022-07-14 tracey const struct got_error *
120 cb93ab40 2023-01-22 op got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
122 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
123 a596b957 2022-07-14 tracey struct transport *t = c->t;
124 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
125 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
126 a596b957 2022-07-14 tracey struct got_reflist_head refs;
127 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
128 a596b957 2022-07-14 tracey time_t committer_time = 0, cmp_time = 0;
130 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
132 417c8923 2023-08-11 op *repo_age = 0;
134 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/heads",
135 a596b957 2022-07-14 tracey got_ref_cmp_by_name, NULL);
136 a596b957 2022-07-14 tracey if (error)
137 a596b957 2022-07-14 tracey goto done;
140 a596b957 2022-07-14 tracey * Find the youngest branch tip in the repository, or the age of
141 a596b957 2022-07-14 tracey * the a specific branch tip if a name was provided by the caller.
143 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
144 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
146 a596b957 2022-07-14 tracey if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
147 a596b957 2022-07-14 tracey continue;
149 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, re->ref);
150 a596b957 2022-07-14 tracey if (error)
151 a596b957 2022-07-14 tracey goto done;
153 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
154 a596b957 2022-07-14 tracey free(id);
155 a596b957 2022-07-14 tracey if (error)
156 a596b957 2022-07-14 tracey goto done;
158 a596b957 2022-07-14 tracey committer_time =
159 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
160 a596b957 2022-07-14 tracey got_object_commit_close(commit);
161 a596b957 2022-07-14 tracey if (cmp_time < committer_time)
162 a596b957 2022-07-14 tracey cmp_time = committer_time;
164 a596b957 2022-07-14 tracey if (refname)
168 cb93ab40 2023-01-22 op if (cmp_time != 0)
169 cb93ab40 2023-01-22 op *repo_age = cmp_time;
171 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
172 a596b957 2022-07-14 tracey return error;
175 a596b957 2022-07-14 tracey static const struct got_error *
176 a596b957 2022-07-14 tracey got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
177 a596b957 2022-07-14 tracey struct got_commit_object *commit, struct got_reflist_head *refs,
178 a596b957 2022-07-14 tracey struct got_object_id *id)
180 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
181 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
182 a596b957 2022-07-14 tracey struct got_object_id *id2 = NULL;
183 a596b957 2022-07-14 tracey struct got_object_qid *parent_id;
184 a596b957 2022-07-14 tracey struct transport *t = c->t;
185 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
186 a596b957 2022-07-14 tracey char *commit_msg = NULL, *commit_msg0;
188 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, refs, entry) {
190 a596b957 2022-07-14 tracey const char *name;
191 a596b957 2022-07-14 tracey struct got_tag_object *tag = NULL;
192 a596b957 2022-07-14 tracey struct got_object_id *ref_id;
195 a596b957 2022-07-14 tracey if (got_ref_is_symbolic(re->ref))
196 a596b957 2022-07-14 tracey continue;
198 a596b957 2022-07-14 tracey name = got_ref_get_name(re->ref);
199 a596b957 2022-07-14 tracey if (strncmp(name, "refs/", 5) == 0)
200 a596b957 2022-07-14 tracey name += 5;
201 a596b957 2022-07-14 tracey if (strncmp(name, "got/", 4) == 0)
202 a596b957 2022-07-14 tracey continue;
203 a596b957 2022-07-14 tracey if (strncmp(name, "heads/", 6) == 0)
204 a596b957 2022-07-14 tracey name += 6;
205 a596b957 2022-07-14 tracey if (strncmp(name, "remotes/", 8) == 0) {
206 a596b957 2022-07-14 tracey name += 8;
207 341fa7ca 2022-09-01 op if (strstr(name, "/" GOT_REF_HEAD) != NULL)
208 a596b957 2022-07-14 tracey continue;
210 a596b957 2022-07-14 tracey error = got_ref_resolve(&ref_id, t->repo, re->ref);
211 a596b957 2022-07-14 tracey if (error)
212 a596b957 2022-07-14 tracey return error;
213 a596b957 2022-07-14 tracey if (strncmp(name, "tags/", 5) == 0) {
214 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, t->repo, ref_id);
215 a596b957 2022-07-14 tracey if (error) {
216 a596b957 2022-07-14 tracey if (error->code != GOT_ERR_OBJ_TYPE) {
217 a596b957 2022-07-14 tracey free(ref_id);
218 a596b957 2022-07-14 tracey continue;
221 a596b957 2022-07-14 tracey * Ref points at something other
222 a596b957 2022-07-14 tracey * than a tag.
224 a596b957 2022-07-14 tracey error = NULL;
225 a596b957 2022-07-14 tracey tag = NULL;
228 a596b957 2022-07-14 tracey cmp = got_object_id_cmp(tag ?
229 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag) : ref_id, id);
230 a596b957 2022-07-14 tracey free(ref_id);
232 a596b957 2022-07-14 tracey got_object_tag_close(tag);
233 a596b957 2022-07-14 tracey if (cmp != 0)
234 a596b957 2022-07-14 tracey continue;
235 a596b957 2022-07-14 tracey s = repo_commit->refs_str;
236 a596b957 2022-07-14 tracey if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
237 a596b957 2022-07-14 tracey s ? ", " : "", name) == -1) {
238 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
240 a596b957 2022-07-14 tracey repo_commit->refs_str = NULL;
241 a596b957 2022-07-14 tracey return error;
246 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->commit_id, id);
247 a596b957 2022-07-14 tracey if (error)
248 a596b957 2022-07-14 tracey return error;
250 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->tree_id,
251 a596b957 2022-07-14 tracey got_object_commit_get_tree_id(commit));
252 a596b957 2022-07-14 tracey if (error)
253 a596b957 2022-07-14 tracey return error;
255 7f65bb55 2023-12-01 op if (qs->action == DIFF || qs->action == PATCH) {
256 a596b957 2022-07-14 tracey parent_id = STAILQ_FIRST(
257 a596b957 2022-07-14 tracey got_object_commit_get_parent_ids(commit));
258 a596b957 2022-07-14 tracey if (parent_id != NULL) {
259 a596b957 2022-07-14 tracey id2 = got_object_id_dup(&parent_id->id);
260 a596b957 2022-07-14 tracey error = got_object_id_str(&repo_commit->parent_id, id2);
261 a596b957 2022-07-14 tracey if (error)
262 a596b957 2022-07-14 tracey return error;
263 a596b957 2022-07-14 tracey free(id2);
265 a596b957 2022-07-14 tracey repo_commit->parent_id = strdup("/dev/null");
266 a596b957 2022-07-14 tracey if (repo_commit->parent_id == NULL) {
267 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
268 a596b957 2022-07-14 tracey return error;
273 a596b957 2022-07-14 tracey repo_commit->committer_time =
274 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
276 a596b957 2022-07-14 tracey repo_commit->author =
277 a596b957 2022-07-14 tracey strdup(got_object_commit_get_author(commit));
278 a596b957 2022-07-14 tracey if (repo_commit->author == NULL) {
279 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
280 a596b957 2022-07-14 tracey return error;
282 a596b957 2022-07-14 tracey repo_commit->committer =
283 a596b957 2022-07-14 tracey strdup(got_object_commit_get_committer(commit));
284 a596b957 2022-07-14 tracey if (repo_commit->committer == NULL) {
285 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
286 a596b957 2022-07-14 tracey return error;
288 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
289 a596b957 2022-07-14 tracey if (error)
290 a596b957 2022-07-14 tracey return error;
292 a596b957 2022-07-14 tracey commit_msg = commit_msg0;
293 a596b957 2022-07-14 tracey while (*commit_msg == '\n')
294 a596b957 2022-07-14 tracey commit_msg++;
296 a596b957 2022-07-14 tracey repo_commit->commit_msg = strdup(commit_msg);
297 a596b957 2022-07-14 tracey if (repo_commit->commit_msg == NULL)
298 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
299 a596b957 2022-07-14 tracey free(commit_msg0);
300 a596b957 2022-07-14 tracey return error;
303 a596b957 2022-07-14 tracey const struct got_error *
304 0a2fc486 2023-06-14 op got_get_repo_commits(struct request *c, size_t limit)
306 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
307 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
308 a596b957 2022-07-14 tracey struct got_commit_graph *graph = NULL;
309 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
310 a596b957 2022-07-14 tracey struct got_reflist_head refs;
311 bce44e0b 2022-09-02 op struct got_reference *ref = NULL;
312 a596b957 2022-07-14 tracey struct repo_commit *repo_commit = NULL;
313 a596b957 2022-07-14 tracey struct server *srv = c->srv;
314 a596b957 2022-07-14 tracey struct transport *t = c->t;
315 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
316 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
317 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
318 a596b957 2022-07-14 tracey char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
319 5144d22b 2023-06-14 op int chk_next = 0;
321 0a2fc486 2023-06-14 op if (limit == 0)
322 0a2fc486 2023-06-14 op return got_error(GOT_ERR_RANGE);
324 0a2fc486 2023-06-14 op if (limit > 1) {
326 5144d22b 2023-06-14 op * Traverse one commit more than requested to provide
327 5144d22b 2023-06-14 op * the next button.
330 5144d22b 2023-06-14 op chk_next = 1;
333 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
335 4448825a 2023-06-16 op if (qs->file != NULL && *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 4448825a 2023-06-16 op if (qs->file != NULL && *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 98297eed 2024-03-27 stsp error = got_commit_graph_bfsort(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);
409 5144d22b 2023-06-14 op if (--limit == 0 && chk_next) {
410 5144d22b 2023-06-14 op t->more_id = strdup(repo_commit->commit_id);
411 5144d22b 2023-06-14 op if (t->more_id == NULL)
412 5144d22b 2023-06-14 op error = got_error_from_errno("strdup");
413 a596b957 2022-07-14 tracey goto done;
416 4f0a80ed 2022-09-07 op TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
418 5144d22b 2023-06-14 op if (limit == 0)
421 a596b957 2022-07-14 tracey if (commit) {
422 a596b957 2022-07-14 tracey got_object_commit_close(commit);
423 a596b957 2022-07-14 tracey commit = NULL;
428 bce44e0b 2022-09-02 op got_ref_close(ref);
429 a596b957 2022-07-14 tracey if (commit)
430 a596b957 2022-07-14 tracey got_object_commit_close(commit);
431 a596b957 2022-07-14 tracey if (graph)
432 a596b957 2022-07-14 tracey got_commit_graph_close(graph);
433 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
434 dfa5768d 2022-09-02 op free(in_repo_path);
435 a596b957 2022-07-14 tracey free(file_path);
436 a596b957 2022-07-14 tracey free(repo_path);
437 a596b957 2022-07-14 tracey free(id);
438 a596b957 2022-07-14 tracey return error;
441 a596b957 2022-07-14 tracey const struct got_error *
442 23c35dae 2023-06-15 op got_get_repo_tags(struct request *c, size_t limit)
444 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
445 a596b957 2022-07-14 tracey struct got_object_id *id = NULL;
446 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
447 a596b957 2022-07-14 tracey struct got_reflist_head refs;
448 a596b957 2022-07-14 tracey struct got_reference *ref;
449 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
450 a596b957 2022-07-14 tracey struct server *srv = c->srv;
451 a596b957 2022-07-14 tracey struct transport *t = c->t;
452 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
453 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
454 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
455 a596b957 2022-07-14 tracey struct got_tag_object *tag = NULL;
456 a596b957 2022-07-14 tracey char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
457 625e5896 2022-09-01 op char *tag_commit = NULL, *tag_commit0 = NULL;
458 a596b957 2022-07-14 tracey char *commit_msg = NULL, *commit_msg0 = NULL;
459 723721e2 2023-12-08 op int chk_next = 0, chk_multi = 1, commit_found = 0;
461 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
463 23c35dae 2023-06-15 op if (limit == 0)
464 23c35dae 2023-06-15 op return got_error(GOT_ERR_RANGE);
466 a596b957 2022-07-14 tracey if (asprintf(&repo_path, "%s/%s", srv->repos_path,
467 a596b957 2022-07-14 tracey repo_dir->name) == -1)
468 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
470 1abb18e1 2022-12-20 op if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
471 a596b957 2022-07-14 tracey error = got_ref_open(&ref, repo, qs->headref, 0);
472 a596b957 2022-07-14 tracey if (error)
474 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, ref);
475 a596b957 2022-07-14 tracey got_ref_close(ref);
476 a596b957 2022-07-14 tracey if (error)
478 a596b957 2022-07-14 tracey } else if (qs->commit == NULL && qs->action == TAG) {
479 a596b957 2022-07-14 tracey error = got_error_msg(GOT_ERR_EOF, "commit id missing");
482 a596b957 2022-07-14 tracey error = got_repo_match_object_id_prefix(&id, qs->commit,
483 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, repo);
484 a596b957 2022-07-14 tracey if (error)
488 a596b957 2022-07-14 tracey if (qs->action != SUMMARY && qs->action != TAGS) {
489 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
490 a596b957 2022-07-14 tracey if (error)
492 a596b957 2022-07-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
493 a596b957 2022-07-14 tracey if (error)
495 a596b957 2022-07-14 tracey if (commit) {
496 a596b957 2022-07-14 tracey got_object_commit_close(commit);
497 a596b957 2022-07-14 tracey commit = NULL;
501 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, repo_path);
502 a596b957 2022-07-14 tracey if (error)
505 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
507 a596b957 2022-07-14 tracey if (error)
510 a596b957 2022-07-14 tracey if (limit == 1)
511 a596b957 2022-07-14 tracey chk_multi = 0;
513 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
514 a596b957 2022-07-14 tracey struct repo_tag *new_repo_tag = NULL;
515 a596b957 2022-07-14 tracey error = got_init_repo_tag(&new_repo_tag);
516 a596b957 2022-07-14 tracey if (error)
519 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
521 a596b957 2022-07-14 tracey new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
522 a596b957 2022-07-14 tracey if (new_repo_tag->tag_name == NULL) {
523 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
530 b163541d 2022-09-02 op free(id_str);
531 b163541d 2022-09-02 op id_str = NULL;
533 a596b957 2022-07-14 tracey error = got_ref_resolve(&id, repo, re->ref);
534 a596b957 2022-07-14 tracey if (error)
535 a596b957 2022-07-14 tracey goto done;
538 bc95141c 2022-09-02 op got_object_tag_close(tag);
539 a596b957 2022-07-14 tracey error = got_object_open_as_tag(&tag, repo, id);
540 a596b957 2022-07-14 tracey if (error) {
541 b163541d 2022-09-02 op if (error->code != GOT_ERR_OBJ_TYPE)
542 a596b957 2022-07-14 tracey goto done;
543 a596b957 2022-07-14 tracey /* "lightweight" tag */
544 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, id);
546 a596b957 2022-07-14 tracey goto done;
547 a596b957 2022-07-14 tracey new_repo_tag->tagger =
548 a596b957 2022-07-14 tracey strdup(got_object_commit_get_committer(commit));
549 a596b957 2022-07-14 tracey if (new_repo_tag->tagger == NULL) {
550 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
553 a596b957 2022-07-14 tracey new_repo_tag->tagger_time =
554 a596b957 2022-07-14 tracey got_object_commit_get_committer_time(commit);
555 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str, id);
556 a596b957 2022-07-14 tracey if (error)
559 a596b957 2022-07-14 tracey new_repo_tag->tagger =
560 a596b957 2022-07-14 tracey strdup(got_object_tag_get_tagger(tag));
561 a596b957 2022-07-14 tracey if (new_repo_tag->tagger == NULL) {
562 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
565 a596b957 2022-07-14 tracey new_repo_tag->tagger_time =
566 a596b957 2022-07-14 tracey got_object_tag_get_tagger_time(tag);
567 a596b957 2022-07-14 tracey error = got_object_id_str(&id_str,
568 a596b957 2022-07-14 tracey got_object_tag_get_object_id(tag));
569 a596b957 2022-07-14 tracey if (error)
573 a596b957 2022-07-14 tracey new_repo_tag->commit_id = strdup(id_str);
574 a596b957 2022-07-14 tracey if (new_repo_tag->commit_id == NULL)
577 a596b957 2022-07-14 tracey if (commit_found == 0 && qs->commit != NULL &&
578 e4caa658 2024-09-07 op strncmp(id_str, qs->commit, strlen(id_str)) != 0) {
579 e4caa658 2024-09-07 op TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
580 e4caa658 2024-09-07 op gotweb_free_repo_tag(new_repo_tag);
581 a596b957 2022-07-14 tracey continue;
583 a596b957 2022-07-14 tracey commit_found = 1;
586 a596b957 2022-07-14 tracey * check for one more commit before breaking,
587 a596b957 2022-07-14 tracey * so we know whether to navigate through briefs
588 a596b957 2022-07-14 tracey * commits and summary
590 a596b957 2022-07-14 tracey if (chk_next) {
591 723721e2 2023-12-08 op t->tags_more_id = strdup(new_repo_tag->commit_id);
592 723721e2 2023-12-08 op if (t->tags_more_id == NULL) {
593 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
596 a596b957 2022-07-14 tracey if (commit) {
597 a596b957 2022-07-14 tracey got_object_commit_close(commit);
598 a596b957 2022-07-14 tracey commit = NULL;
600 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
601 a596b957 2022-07-14 tracey gotweb_free_repo_tag(new_repo_tag);
602 a596b957 2022-07-14 tracey goto done;
605 a596b957 2022-07-14 tracey if (commit) {
606 625e5896 2022-09-01 op error = got_object_commit_get_logmsg(&tag_commit0,
608 a596b957 2022-07-14 tracey if (error)
610 a596b957 2022-07-14 tracey got_object_commit_close(commit);
611 a596b957 2022-07-14 tracey commit = NULL;
613 625e5896 2022-09-01 op tag_commit0 = strdup(got_object_tag_get_message(tag));
614 625e5896 2022-09-01 op if (tag_commit0 == NULL) {
615 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
620 625e5896 2022-09-01 op tag_commit = tag_commit0;
621 625e5896 2022-09-01 op while (*tag_commit == '\n')
622 625e5896 2022-09-01 op tag_commit++;
623 625e5896 2022-09-01 op new_repo_tag->tag_commit = strdup(tag_commit);
624 625e5896 2022-09-01 op if (new_repo_tag->tag_commit == NULL) {
625 625e5896 2022-09-01 op error = got_error_from_errno("strdup");
626 625e5896 2022-09-01 op free(tag_commit0);
629 625e5896 2022-09-01 op free(tag_commit0);
631 a596b957 2022-07-14 tracey if (qs->action != SUMMARY && qs->action != TAGS) {
632 a596b957 2022-07-14 tracey commit_msg = commit_msg0;
633 a596b957 2022-07-14 tracey while (*commit_msg == '\n')
634 a596b957 2022-07-14 tracey commit_msg++;
636 a596b957 2022-07-14 tracey new_repo_tag->commit_msg = strdup(commit_msg);
637 a596b957 2022-07-14 tracey if (new_repo_tag->commit_msg == NULL) {
638 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
643 a596b957 2022-07-14 tracey if (limit && --limit == 0) {
644 a596b957 2022-07-14 tracey if (chk_multi == 0)
646 a596b957 2022-07-14 tracey chk_next = 1;
651 a596b957 2022-07-14 tracey if (commit)
652 a596b957 2022-07-14 tracey got_object_commit_close(commit);
654 b163541d 2022-09-02 op got_object_tag_close(tag);
655 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
656 5a57034b 2022-09-02 op free(commit_msg0);
657 b163541d 2022-09-02 op free(in_repo_path);
658 a596b957 2022-07-14 tracey free(repo_path);
659 a596b957 2022-07-14 tracey free(id);
660 b163541d 2022-09-02 op free(id_str);
661 a596b957 2022-07-14 tracey return error;
665 ac15152e 2023-12-08 op got_output_repo_tree(struct request *c, char **readme,
666 43d421de 2023-01-05 op int (*cb)(struct template *, struct got_tree_entry *))
668 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
669 a596b957 2022-07-14 tracey struct transport *t = c->t;
670 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
671 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
672 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
673 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
674 a596b957 2022-07-14 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
675 a596b957 2022-07-14 tracey struct got_reflist_head refs;
676 a596b957 2022-07-14 tracey struct got_tree_object *tree = NULL;
677 43d421de 2023-01-05 op struct got_tree_entry *te;
678 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
679 ac15152e 2023-12-08 op const char *name;
681 e5e662e4 2022-09-02 op char *escaped_name = NULL, *path = NULL;
682 43d421de 2023-01-05 op int nentries, i;
684 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
685 ac15152e 2023-12-08 op *readme = NULL;
687 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
689 a596b957 2022-07-14 tracey if (qs->folder != NULL) {
690 a596b957 2022-07-14 tracey path = strdup(qs->folder);
691 a596b957 2022-07-14 tracey if (path == NULL) {
692 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
693 a596b957 2022-07-14 tracey goto done;
696 6977f45a 2022-09-02 op error = got_repo_map_path(&path, repo, repo_dir->path);
697 a596b957 2022-07-14 tracey if (error)
698 a596b957 2022-07-14 tracey goto done;
701 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
702 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
703 a596b957 2022-07-14 tracey if (error)
704 a596b957 2022-07-14 tracey goto done;
706 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
707 a596b957 2022-07-14 tracey if (error)
708 a596b957 2022-07-14 tracey goto done;
710 a596b957 2022-07-14 tracey error = got_object_id_by_path(&tree_id, repo, commit, path);
711 a596b957 2022-07-14 tracey if (error)
712 a596b957 2022-07-14 tracey goto done;
714 a596b957 2022-07-14 tracey error = got_object_open_as_tree(&tree, repo, tree_id);
715 a596b957 2022-07-14 tracey if (error)
716 a596b957 2022-07-14 tracey goto done;
718 a596b957 2022-07-14 tracey nentries = got_object_tree_get_nentries(tree);
720 a596b957 2022-07-14 tracey for (i = 0; i < nentries; i++) {
721 a596b957 2022-07-14 tracey te = got_object_tree_get_entry(tree, i);
723 ac15152e 2023-12-08 op name = got_tree_entry_get_name(te);
724 ac15152e 2023-12-08 op mode = got_tree_entry_get_mode(te);
725 ac15152e 2023-12-08 op if (!S_ISDIR(mode) && (!strcasecmp(name, "README") ||
726 ac15152e 2023-12-08 op !strcasecmp(name, "README.md") ||
727 ac15152e 2023-12-08 op !strcasecmp(name, "README.txt"))) {
728 ac15152e 2023-12-08 op free(*readme);
729 ac15152e 2023-12-08 op *readme = strdup(name);
732 01392bd6 2023-02-10 tracey if (cb(c->tp, te) == -1) {
733 01392bd6 2023-02-10 tracey error = got_error(GOT_ERR_CANCELLED);
738 b7efc9b3 2022-08-25 op free(escaped_name);
739 a596b957 2022-07-14 tracey free(path);
740 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
741 a596b957 2022-07-14 tracey if (commit)
742 a596b957 2022-07-14 tracey got_object_commit_close(commit);
744 58354f54 2022-09-06 op got_object_tree_close(tree);
745 a596b957 2022-07-14 tracey free(commit_id);
746 a596b957 2022-07-14 tracey free(tree_id);
748 ac15152e 2023-12-08 op free(*readme);
749 ac15152e 2023-12-08 op *readme = NULL;
750 8a078d7f 2023-05-17 op if (error->code != GOT_ERR_CANCELLED)
751 8a078d7f 2023-05-17 op log_warnx("%s: %s", __func__, error->msg);
757 a596b957 2022-07-14 tracey const struct got_error *
758 298f95fb 2023-01-05 op got_open_blob_for_output(struct got_blob_object **blob, int *fd,
759 ac15152e 2023-12-08 op int *binary, struct request *c, const char *directory, const char *file,
760 ac15152e 2023-12-08 op const char *commitstr)
762 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
763 ac15152e 2023-12-08 op struct got_repository *repo = c->t->repo;
764 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
765 a596b957 2022-07-14 tracey struct got_object_id *commit_id = NULL;
766 a596b957 2022-07-14 tracey struct got_reflist_head refs;
767 a596b957 2022-07-14 tracey char *path = NULL, *in_repo_path = NULL;
768 298f95fb 2023-01-05 op int obj_type;
770 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
772 298f95fb 2023-01-05 op *blob = NULL;
776 ac15152e 2023-12-08 op error = got_ref_list(&refs, repo, "refs/heads",
777 ac15152e 2023-12-08 op got_ref_cmp_by_name, NULL);
781 ac15152e 2023-12-08 op if (asprintf(&path, "%s%s%s", directory ? directory : "",
782 ac15152e 2023-12-08 op directory ? "/" : "", file) == -1) {
783 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
784 a596b957 2022-07-14 tracey goto done;
787 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, path);
788 a596b957 2022-07-14 tracey if (error)
789 a596b957 2022-07-14 tracey goto done;
791 ac15152e 2023-12-08 op if (commitstr == NULL)
792 ac15152e 2023-12-08 op commitstr = GOT_REF_HEAD;
794 ac15152e 2023-12-08 op error = got_repo_match_object_id(&commit_id, NULL, commitstr,
795 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
796 a596b957 2022-07-14 tracey if (error)
797 a596b957 2022-07-14 tracey goto done;
799 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
800 a596b957 2022-07-14 tracey if (error)
801 a596b957 2022-07-14 tracey goto done;
803 a596b957 2022-07-14 tracey error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
804 a596b957 2022-07-14 tracey if (error)
805 a596b957 2022-07-14 tracey goto done;
807 a596b957 2022-07-14 tracey if (commit_id == NULL) {
808 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_NO_OBJ);
809 a596b957 2022-07-14 tracey goto done;
812 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, commit_id);
813 a596b957 2022-07-14 tracey if (error)
814 a596b957 2022-07-14 tracey goto done;
816 a596b957 2022-07-14 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
817 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
818 a596b957 2022-07-14 tracey goto done;
821 298f95fb 2023-01-05 op error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
822 a596b957 2022-07-14 tracey if (error)
823 a596b957 2022-07-14 tracey goto done;
825 298f95fb 2023-01-05 op error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
826 a596b957 2022-07-14 tracey if (error)
827 a596b957 2022-07-14 tracey goto done;
829 298f95fb 2023-01-05 op error = got_object_blob_is_binary(binary, *blob);
835 298f95fb 2023-01-05 op got_object_commit_close(commit);
838 298f95fb 2023-01-05 op if (*fd != -1)
841 298f95fb 2023-01-05 op got_object_blob_close(*blob);
843 298f95fb 2023-01-05 op *blob = NULL;
846 ac15152e 2023-12-08 op got_ref_list_free(&refs);
847 298f95fb 2023-01-05 op free(in_repo_path);
848 298f95fb 2023-01-05 op free(commit_id);
850 a596b957 2022-07-14 tracey return error;
854 298f95fb 2023-01-05 op got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
855 298f95fb 2023-01-05 op int (*cb)(struct template *, const char *, size_t))
857 298f95fb 2023-01-05 op const struct got_error *err;
858 298f95fb 2023-01-05 op char *line = NULL;
859 298f95fb 2023-01-05 op size_t linesize = 0;
860 298f95fb 2023-01-05 op size_t lineno = 0;
861 298f95fb 2023-01-05 op ssize_t linelen = 0;
864 298f95fb 2023-01-05 op err = got_object_blob_getline(&line, &linelen, &linesize,
866 298f95fb 2023-01-05 op if (err || linelen == -1)
869 01392bd6 2023-02-10 tracey if (cb(tp, line, lineno) == -1) {
870 01392bd6 2023-02-10 tracey err = got_error(GOT_ERR_CANCELLED);
878 8a078d7f 2023-05-17 op if (err->code != GOT_ERR_CANCELLED)
879 8a078d7f 2023-05-17 op log_warnx("%s: got_object_blob_getline failed: %s",
880 8a078d7f 2023-05-17 op __func__, err->msg);
886 a596b957 2022-07-14 tracey struct blame_cb_args {
887 a596b957 2022-07-14 tracey struct blame_line *lines;
888 a596b957 2022-07-14 tracey int nlines;
889 a596b957 2022-07-14 tracey int nlines_prec;
890 a596b957 2022-07-14 tracey int lineno_cur;
891 a596b957 2022-07-14 tracey off_t *line_offsets;
893 a596b957 2022-07-14 tracey struct got_repository *repo;
894 a596b957 2022-07-14 tracey struct request *c;
895 8319855f 2023-01-15 op got_render_blame_line_cb cb;
898 a596b957 2022-07-14 tracey static const struct got_error *
899 a596b957 2022-07-14 tracey got_gotweb_blame_cb(void *arg, int nlines, int lineno,
900 a596b957 2022-07-14 tracey struct got_commit_object *commit, struct got_object_id *id)
902 a596b957 2022-07-14 tracey const struct got_error *err = NULL;
903 a596b957 2022-07-14 tracey struct blame_cb_args *a = arg;
904 a596b957 2022-07-14 tracey struct blame_line *bline;
905 a596b957 2022-07-14 tracey struct request *c = a->c;
906 8319855f 2023-01-15 op char *line = NULL;
907 a596b957 2022-07-14 tracey size_t linesize = 0;
908 a596b957 2022-07-14 tracey off_t offset;
909 a596b957 2022-07-14 tracey struct tm tm;
910 a596b957 2022-07-14 tracey time_t committer_time;
912 a596b957 2022-07-14 tracey if (nlines != a->nlines ||
913 a596b957 2022-07-14 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
914 a596b957 2022-07-14 tracey return got_error(GOT_ERR_RANGE);
916 a596b957 2022-07-14 tracey if (lineno == -1)
917 a596b957 2022-07-14 tracey return NULL; /* no change in this commit */
919 a596b957 2022-07-14 tracey /* Annotate this line. */
920 a596b957 2022-07-14 tracey bline = &a->lines[lineno - 1];
921 a596b957 2022-07-14 tracey if (bline->annotated)
922 a596b957 2022-07-14 tracey return NULL;
923 a596b957 2022-07-14 tracey err = got_object_id_str(&bline->id_str, id);
925 a596b957 2022-07-14 tracey return err;
927 a596b957 2022-07-14 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
928 a596b957 2022-07-14 tracey if (bline->committer == NULL) {
929 a596b957 2022-07-14 tracey err = got_error_from_errno("strdup");
930 a596b957 2022-07-14 tracey goto done;
933 a596b957 2022-07-14 tracey committer_time = got_object_commit_get_committer_time(commit);
934 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
935 a596b957 2022-07-14 tracey return got_error_from_errno("gmtime_r");
936 283939fb 2024-04-23 op if (strftime(bline->datebuf, sizeof(bline->datebuf), "%F", &tm) == 0) {
937 a596b957 2022-07-14 tracey err = got_error(GOT_ERR_NO_SPACE);
938 a596b957 2022-07-14 tracey goto done;
940 a596b957 2022-07-14 tracey bline->annotated = 1;
942 a596b957 2022-07-14 tracey /* Print lines annotated so far. */
943 a596b957 2022-07-14 tracey bline = &a->lines[a->lineno_cur - 1];
944 a596b957 2022-07-14 tracey if (!bline->annotated)
945 a596b957 2022-07-14 tracey goto done;
947 a596b957 2022-07-14 tracey offset = a->line_offsets[a->lineno_cur - 1];
948 a596b957 2022-07-14 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
949 a596b957 2022-07-14 tracey err = got_error_from_errno("fseeko");
950 a596b957 2022-07-14 tracey goto done;
953 85f2c2e0 2022-08-18 op while (a->lineno_cur <= a->nlines && bline->annotated) {
954 a596b957 2022-07-14 tracey if (getline(&line, &linesize, a->f) == -1) {
955 a596b957 2022-07-14 tracey if (ferror(a->f))
956 a596b957 2022-07-14 tracey err = got_error_from_errno("getline");
960 8319855f 2023-01-15 op if (a->cb(c->tp, line, bline, a->nlines_prec,
961 4f152e84 2023-02-10 op a->lineno_cur) == -1) {
962 4f152e84 2023-02-10 op err = got_error(GOT_ERR_CANCELLED);
966 a596b957 2022-07-14 tracey a->lineno_cur++;
967 a596b957 2022-07-14 tracey bline = &a->lines[a->lineno_cur - 1];
970 a596b957 2022-07-14 tracey free(line);
971 a596b957 2022-07-14 tracey return err;
974 a596b957 2022-07-14 tracey const struct got_error *
975 8319855f 2023-01-15 op got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
977 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
978 a596b957 2022-07-14 tracey struct transport *t = c->t;
979 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
980 a596b957 2022-07-14 tracey struct querystring *qs = c->t->qs;
981 a596b957 2022-07-14 tracey struct got_object_id *obj_id = NULL, *commit_id = NULL;
982 a596b957 2022-07-14 tracey struct got_commit_object *commit = NULL;
983 a596b957 2022-07-14 tracey struct got_reflist_head refs;
984 a596b957 2022-07-14 tracey struct got_blob_object *blob = NULL;
985 a596b957 2022-07-14 tracey char *path = NULL, *in_repo_path = NULL;
986 a596b957 2022-07-14 tracey struct blame_cb_args bca;
987 e62232ad 2023-05-16 op int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
988 a596b957 2022-07-14 tracey off_t filesize;
989 a596b957 2022-07-14 tracey FILE *f1 = NULL, *f2 = NULL;
991 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
992 a596b957 2022-07-14 tracey bca.f = NULL;
993 a596b957 2022-07-14 tracey bca.lines = NULL;
996 26b163a0 2024-01-30 op if (asprintf(&path, "%s/%s", qs->folder, qs->file) == -1) {
997 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
998 a596b957 2022-07-14 tracey goto done;
1001 a596b957 2022-07-14 tracey error = got_repo_map_path(&in_repo_path, repo, path);
1002 a596b957 2022-07-14 tracey if (error)
1003 a596b957 2022-07-14 tracey goto done;
1005 a596b957 2022-07-14 tracey error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1006 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_COMMIT, &refs, repo);
1007 a596b957 2022-07-14 tracey if (error)
1008 a596b957 2022-07-14 tracey goto done;
1010 a596b957 2022-07-14 tracey error = got_object_open_as_commit(&commit, repo, commit_id);
1011 a596b957 2022-07-14 tracey if (error)
1012 a596b957 2022-07-14 tracey goto done;
1014 a596b957 2022-07-14 tracey error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1015 a596b957 2022-07-14 tracey if (error)
1016 a596b957 2022-07-14 tracey goto done;
1018 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, obj_id);
1019 a596b957 2022-07-14 tracey if (error)
1020 a596b957 2022-07-14 tracey goto done;
1022 a596b957 2022-07-14 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
1023 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1024 a596b957 2022-07-14 tracey goto done;
1027 18069c98 2023-05-16 op error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1028 a596b957 2022-07-14 tracey if (error)
1029 a596b957 2022-07-14 tracey goto done;
1031 e62232ad 2023-05-16 op error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1032 a596b957 2022-07-14 tracey if (error)
1033 a596b957 2022-07-14 tracey goto done;
1035 e62232ad 2023-05-16 op error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1036 a596b957 2022-07-14 tracey if (error)
1037 a596b957 2022-07-14 tracey goto done;
1039 a596b957 2022-07-14 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1040 a596b957 2022-07-14 tracey &bca.line_offsets, bca.f, blob);
1041 a596b957 2022-07-14 tracey if (error || bca.nlines == 0)
1042 a596b957 2022-07-14 tracey goto done;
1044 a596b957 2022-07-14 tracey /* Don't include \n at EOF in the blame line count. */
1045 a596b957 2022-07-14 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
1046 a596b957 2022-07-14 tracey bca.nlines--;
1048 a596b957 2022-07-14 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1049 a596b957 2022-07-14 tracey if (bca.lines == NULL) {
1050 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1051 a596b957 2022-07-14 tracey goto done;
1053 a596b957 2022-07-14 tracey bca.lineno_cur = 1;
1054 a596b957 2022-07-14 tracey bca.nlines_prec = 0;
1055 a596b957 2022-07-14 tracey i = bca.nlines;
1056 a596b957 2022-07-14 tracey while (i > 0) {
1057 a596b957 2022-07-14 tracey i /= 10;
1058 a596b957 2022-07-14 tracey bca.nlines_prec++;
1060 a596b957 2022-07-14 tracey bca.repo = repo;
1061 a596b957 2022-07-14 tracey bca.c = c;
1063 e62232ad 2023-05-16 op error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1064 a596b957 2022-07-14 tracey if (error)
1065 a596b957 2022-07-14 tracey goto done;
1067 e62232ad 2023-05-16 op error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1068 a596b957 2022-07-14 tracey if (error)
1069 a596b957 2022-07-14 tracey goto done;
1071 18069c98 2023-05-16 op error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1072 a596b957 2022-07-14 tracey if (error)
1073 a596b957 2022-07-14 tracey goto done;
1075 18069c98 2023-05-16 op error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1076 a596b957 2022-07-14 tracey if (error)
1077 a596b957 2022-07-14 tracey goto done;
1079 a596b957 2022-07-14 tracey error = got_blame(in_repo_path, commit_id, repo,
1080 a596b957 2022-07-14 tracey GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1081 e62232ad 2023-05-16 op fd1, fd2, f1, f2);
1084 a33a44db 2022-09-03 op if (bca.lines) {
1085 a596b957 2022-07-14 tracey free(bca.line_offsets);
1086 a596b957 2022-07-14 tracey for (i = 0; i < bca.nlines; i++) {
1087 a596b957 2022-07-14 tracey struct blame_line *bline = &bca.lines[i];
1088 a596b957 2022-07-14 tracey free(bline->id_str);
1089 a596b957 2022-07-14 tracey free(bline->committer);
1092 a596b957 2022-07-14 tracey free(bca.lines);
1093 e62232ad 2023-05-16 op if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1094 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1095 e62232ad 2023-05-16 op if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1096 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1097 e62232ad 2023-05-16 op if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1098 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1099 a596b957 2022-07-14 tracey if (bca.f) {
1100 24a4d801 2023-05-16 op const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1101 a596b957 2022-07-14 tracey if (error == NULL)
1102 a596b957 2022-07-14 tracey error = bca_err;
1104 a596b957 2022-07-14 tracey if (f1) {
1105 24a4d801 2023-05-16 op const struct got_error *f1_err = got_gotweb_closefile(f1);
1106 a596b957 2022-07-14 tracey if (error == NULL)
1107 a596b957 2022-07-14 tracey error = f1_err;
1109 a596b957 2022-07-14 tracey if (f2) {
1110 24a4d801 2023-05-16 op const struct got_error *f2_err = got_gotweb_closefile(f2);
1111 a596b957 2022-07-14 tracey if (error == NULL)
1112 a596b957 2022-07-14 tracey error = f2_err;
1114 a596b957 2022-07-14 tracey if (commit)
1115 a596b957 2022-07-14 tracey got_object_commit_close(commit);
1116 a596b957 2022-07-14 tracey if (blob)
1117 a596b957 2022-07-14 tracey got_object_blob_close(blob);
1118 a596b957 2022-07-14 tracey free(in_repo_path);
1119 a596b957 2022-07-14 tracey free(commit_id);
1120 b94206d0 2022-09-03 op free(obj_id);
1121 a596b957 2022-07-14 tracey free(path);
1122 b94206d0 2022-09-03 op got_ref_list_free(&refs);
1123 a596b957 2022-07-14 tracey return error;
1126 a596b957 2022-07-14 tracey const struct got_error *
1127 18069c98 2023-05-16 op got_open_diff_for_output(FILE **fp, struct request *c)
1129 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1130 a596b957 2022-07-14 tracey struct transport *t = c->t;
1131 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1132 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
1133 a596b957 2022-07-14 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1134 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1135 a596b957 2022-07-14 tracey FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1136 e62232ad 2023-05-16 op int obj_type, fd1 = -1, fd2 = -1;
1140 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1142 18069c98 2023-05-16 op error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1143 a596b957 2022-07-14 tracey if (error)
1146 18069c98 2023-05-16 op error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1147 a596b957 2022-07-14 tracey if (error)
1150 18069c98 2023-05-16 op error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1151 a596b957 2022-07-14 tracey if (error)
1154 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
1156 a596b957 2022-07-14 tracey if (rc->parent_id != NULL &&
1157 a596b957 2022-07-14 tracey strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1158 587550a5 2023-01-10 op error = got_repo_match_object_id(&id1, NULL,
1159 a596b957 2022-07-14 tracey rc->parent_id, GOT_OBJ_TYPE_ANY,
1160 a596b957 2022-07-14 tracey &refs, repo);
1161 a596b957 2022-07-14 tracey if (error)
1162 a596b957 2022-07-14 tracey goto done;
1165 587550a5 2023-01-10 op error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1166 a596b957 2022-07-14 tracey GOT_OBJ_TYPE_ANY, &refs, repo);
1167 a596b957 2022-07-14 tracey if (error)
1168 a596b957 2022-07-14 tracey goto done;
1170 a596b957 2022-07-14 tracey error = got_object_get_type(&obj_type, repo, id2);
1171 a596b957 2022-07-14 tracey if (error)
1172 a596b957 2022-07-14 tracey goto done;
1174 e62232ad 2023-05-16 op error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1175 a596b957 2022-07-14 tracey if (error)
1176 a596b957 2022-07-14 tracey goto done;
1178 e62232ad 2023-05-16 op error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1179 a596b957 2022-07-14 tracey if (error)
1180 a596b957 2022-07-14 tracey goto done;
1182 a596b957 2022-07-14 tracey switch (obj_type) {
1183 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_BLOB:
1184 e62232ad 2023-05-16 op error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1185 a596b957 2022-07-14 tracey id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1186 1f3405c9 2023-01-17 mark NULL, repo, f3);
1188 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_TREE:
1189 e62232ad 2023-05-16 op error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1190 a596b957 2022-07-14 tracey id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1191 1f3405c9 2023-01-17 mark NULL, repo, f3);
1193 a596b957 2022-07-14 tracey case GOT_OBJ_TYPE_COMMIT:
1194 e62232ad 2023-05-16 op error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1195 e62232ad 2023-05-16 op fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1196 1f3405c9 2023-01-17 mark NULL, repo, f3);
1198 a596b957 2022-07-14 tracey default:
1199 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1201 a596b957 2022-07-14 tracey if (error)
1202 a596b957 2022-07-14 tracey goto done;
1204 a596b957 2022-07-14 tracey if (fseek(f3, 0, SEEK_SET) == -1) {
1205 a596b957 2022-07-14 tracey error = got_ferror(f3, GOT_ERR_IO);
1206 a596b957 2022-07-14 tracey goto done;
1212 e62232ad 2023-05-16 op if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1213 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1214 e62232ad 2023-05-16 op if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1215 a596b957 2022-07-14 tracey error = got_error_from_errno("close");
1216 a596b957 2022-07-14 tracey if (f1) {
1217 24a4d801 2023-05-16 op const struct got_error *f1_err = got_gotweb_closefile(f1);
1218 a596b957 2022-07-14 tracey if (error == NULL)
1219 a596b957 2022-07-14 tracey error = f1_err;
1221 a596b957 2022-07-14 tracey if (f2) {
1222 24a4d801 2023-05-16 op const struct got_error *f2_err = got_gotweb_closefile(f2);
1223 a596b957 2022-07-14 tracey if (error == NULL)
1224 a596b957 2022-07-14 tracey error = f2_err;
1226 587550a5 2023-01-10 op if (error && f3) {
1227 24a4d801 2023-05-16 op got_gotweb_closefile(f3);
1230 a596b957 2022-07-14 tracey got_ref_list_free(&refs);
1231 a596b957 2022-07-14 tracey free(id1);
1232 a596b957 2022-07-14 tracey free(id2);
1233 a596b957 2022-07-14 tracey return error;
1236 a596b957 2022-07-14 tracey static const struct got_error *
1237 a596b957 2022-07-14 tracey got_init_repo_commit(struct repo_commit **rc)
1239 a596b957 2022-07-14 tracey *rc = calloc(1, sizeof(**rc));
1240 a596b957 2022-07-14 tracey if (*rc == NULL)
1241 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "calloc");
1243 a596b957 2022-07-14 tracey (*rc)->path = NULL;
1244 a596b957 2022-07-14 tracey (*rc)->refs_str = NULL;
1245 a596b957 2022-07-14 tracey (*rc)->commit_id = NULL;
1246 a596b957 2022-07-14 tracey (*rc)->committer = NULL;
1247 a596b957 2022-07-14 tracey (*rc)->author = NULL;
1248 a596b957 2022-07-14 tracey (*rc)->parent_id = NULL;
1249 a596b957 2022-07-14 tracey (*rc)->tree_id = NULL;
1250 a596b957 2022-07-14 tracey (*rc)->commit_msg = NULL;
1252 dd84f505 2022-08-31 stsp return NULL;
1255 a596b957 2022-07-14 tracey static const struct got_error *
1256 a596b957 2022-07-14 tracey got_init_repo_tag(struct repo_tag **rt)
1258 a596b957 2022-07-14 tracey *rt = calloc(1, sizeof(**rt));
1259 a596b957 2022-07-14 tracey if (*rt == NULL)
1260 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "calloc");
1262 a596b957 2022-07-14 tracey (*rt)->commit_id = NULL;
1263 a596b957 2022-07-14 tracey (*rt)->tag_name = NULL;
1264 a596b957 2022-07-14 tracey (*rt)->tag_commit = NULL;
1265 a596b957 2022-07-14 tracey (*rt)->commit_msg = NULL;
1266 a596b957 2022-07-14 tracey (*rt)->tagger = NULL;
1268 dd84f505 2022-08-31 stsp return NULL;