2 8a35f56c 2022-07-16 thomas * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
6 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
7 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
9 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
20 3e12c168 2022-07-16 thomas #include <sys/queue.h>
21 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
22 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
24 8a35f56c 2022-07-16 thomas #include <event.h>
25 588a8092 2023-02-23 thomas #include <imsg.h>
26 8a35f56c 2022-07-16 thomas #include <stdlib.h>
27 8a35f56c 2022-07-16 thomas #include <stdio.h>
28 8a35f56c 2022-07-16 thomas #include <string.h>
29 8a35f56c 2022-07-16 thomas #include <unistd.h>
31 8a35f56c 2022-07-16 thomas #include "got_error.h"
32 8a35f56c 2022-07-16 thomas #include "got_object.h"
33 8a35f56c 2022-07-16 thomas #include "got_reference.h"
34 8a35f56c 2022-07-16 thomas #include "got_repository.h"
35 8a35f56c 2022-07-16 thomas #include "got_path.h"
36 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
37 8a35f56c 2022-07-16 thomas #include "got_diff.h"
38 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
39 8a35f56c 2022-07-16 thomas #include "got_blame.h"
40 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
42 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
44 8a35f56c 2022-07-16 thomas static const struct got_error *got_init_repo_commit(struct repo_commit **);
45 8a35f56c 2022-07-16 thomas static const struct got_error *got_init_repo_tag(struct repo_tag **);
46 8a35f56c 2022-07-16 thomas static const struct got_error *got_get_repo_commit(struct request *,
47 8a35f56c 2022-07-16 thomas struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
48 8a35f56c 2022-07-16 thomas struct got_object_id *);
49 8a35f56c 2022-07-16 thomas static const struct got_error *got_gotweb_dupfd(int *, int *);
50 04b2c111 2023-05-16 thomas static const struct got_error *got_gotweb_openfile(FILE **, int *);
51 8a35f56c 2022-07-16 thomas static const struct got_error *got_gotweb_blame_cb(void *, int, int,
52 8a35f56c 2022-07-16 thomas struct got_commit_object *,struct got_object_id *);
54 dccd05b4 2023-01-10 thomas const struct got_error *
55 d00235d8 2023-05-17 thomas got_gotweb_closefile(FILE *f)
57 985ec13b 2023-05-17 thomas const struct got_error *err = NULL;
59 8a35f56c 2022-07-16 thomas if (fseek(f, 0, SEEK_SET) == -1)
60 985ec13b 2023-05-17 thomas err = got_error_from_errno("fseek");
62 985ec13b 2023-05-17 thomas if (err == NULL && ftruncate(fileno(f), 0) == -1)
63 985ec13b 2023-05-17 thomas err = got_error_from_errno("ftruncate");
65 985ec13b 2023-05-17 thomas if (fclose(f) == EOF && err == NULL)
66 985ec13b 2023-05-17 thomas err = got_error_from_errno("fclose");
68 985ec13b 2023-05-17 thomas return err;
71 8a35f56c 2022-07-16 thomas static const struct got_error *
72 04b2c111 2023-05-16 thomas got_gotweb_openfile(FILE **f, int *priv_fd)
76 04b2c111 2023-05-16 thomas fd = dup(*priv_fd);
77 04b2c111 2023-05-16 thomas if (fd == -1)
78 6e596ed0 2022-08-31 thomas return got_error_from_errno("dup");
80 04b2c111 2023-05-16 thomas *f = fdopen(fd, "w+");
81 8a35f56c 2022-07-16 thomas if (*f == NULL) {
82 04b2c111 2023-05-16 thomas close(fd);
83 e2af4cd7 2022-08-31 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
86 e2af4cd7 2022-08-31 thomas return NULL;
89 8a35f56c 2022-07-16 thomas static const struct got_error *
90 8a35f56c 2022-07-16 thomas got_gotweb_dupfd(int *priv_fd, int *fd)
92 8a35f56c 2022-07-16 thomas *fd = dup(*priv_fd);
94 a66a4a50 2023-02-03 thomas if (*fd == -1)
95 a66a4a50 2023-02-03 thomas return got_error_from_errno("dup");
97 e2af4cd7 2022-08-31 thomas return NULL;
100 8a35f56c 2022-07-16 thomas const struct got_error *
101 6c7f10f7 2022-11-23 thomas got_get_repo_owner(char **owner, struct request *c)
103 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
104 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
105 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
106 8a35f56c 2022-07-16 thomas const char *gitconfig_owner;
108 8a35f56c 2022-07-16 thomas *owner = NULL;
110 8a35f56c 2022-07-16 thomas if (srv->show_repo_owner == 0)
111 8a35f56c 2022-07-16 thomas return NULL;
113 8a35f56c 2022-07-16 thomas gitconfig_owner = got_repo_get_gitconfig_owner(repo);
114 8a35f56c 2022-07-16 thomas if (gitconfig_owner) {
115 8a35f56c 2022-07-16 thomas *owner = strdup(gitconfig_owner);
116 2f26d334 2022-08-30 thomas if (*owner == NULL)
117 2f26d334 2022-08-30 thomas return got_error_from_errno("strdup");
119 2f26d334 2022-08-30 thomas *owner = strdup("");
120 8a35f56c 2022-07-16 thomas if (*owner == NULL)
121 8a35f56c 2022-07-16 thomas return got_error_from_errno("strdup");
123 e2af4cd7 2022-08-31 thomas return NULL;
126 8a35f56c 2022-07-16 thomas const struct got_error *
127 53bf32b8 2023-01-23 thomas got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
129 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
130 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
131 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
132 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
133 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
134 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
135 8a35f56c 2022-07-16 thomas time_t committer_time = 0, cmp_time = 0;
137 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
139 b38bef13 2023-08-12 thomas *repo_age = 0;
141 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/heads",
142 8a35f56c 2022-07-16 thomas got_ref_cmp_by_name, NULL);
143 8a35f56c 2022-07-16 thomas if (error)
144 8a35f56c 2022-07-16 thomas goto done;
147 8a35f56c 2022-07-16 thomas * Find the youngest branch tip in the repository, or the age of
148 8a35f56c 2022-07-16 thomas * the a specific branch tip if a name was provided by the caller.
150 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
151 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
153 8a35f56c 2022-07-16 thomas if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
154 8a35f56c 2022-07-16 thomas continue;
156 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, re->ref);
157 8a35f56c 2022-07-16 thomas if (error)
158 8a35f56c 2022-07-16 thomas goto done;
160 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
161 8a35f56c 2022-07-16 thomas free(id);
162 8a35f56c 2022-07-16 thomas if (error)
163 8a35f56c 2022-07-16 thomas goto done;
165 8a35f56c 2022-07-16 thomas committer_time =
166 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
167 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
168 8a35f56c 2022-07-16 thomas if (cmp_time < committer_time)
169 8a35f56c 2022-07-16 thomas cmp_time = committer_time;
171 8a35f56c 2022-07-16 thomas if (refname)
175 53bf32b8 2023-01-23 thomas if (cmp_time != 0)
176 53bf32b8 2023-01-23 thomas *repo_age = cmp_time;
178 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
179 8a35f56c 2022-07-16 thomas return error;
182 8a35f56c 2022-07-16 thomas static const struct got_error *
183 8a35f56c 2022-07-16 thomas got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
184 8a35f56c 2022-07-16 thomas struct got_commit_object *commit, struct got_reflist_head *refs,
185 8a35f56c 2022-07-16 thomas struct got_object_id *id)
187 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
188 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
189 8a35f56c 2022-07-16 thomas struct got_object_id *id2 = NULL;
190 8a35f56c 2022-07-16 thomas struct got_object_qid *parent_id;
191 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
192 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
193 8a35f56c 2022-07-16 thomas char *commit_msg = NULL, *commit_msg0;
195 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, refs, entry) {
197 8a35f56c 2022-07-16 thomas const char *name;
198 8a35f56c 2022-07-16 thomas struct got_tag_object *tag = NULL;
199 8a35f56c 2022-07-16 thomas struct got_object_id *ref_id;
202 8a35f56c 2022-07-16 thomas if (got_ref_is_symbolic(re->ref))
203 8a35f56c 2022-07-16 thomas continue;
205 8a35f56c 2022-07-16 thomas name = got_ref_get_name(re->ref);
206 8a35f56c 2022-07-16 thomas if (strncmp(name, "refs/", 5) == 0)
207 8a35f56c 2022-07-16 thomas name += 5;
208 8a35f56c 2022-07-16 thomas if (strncmp(name, "got/", 4) == 0)
209 8a35f56c 2022-07-16 thomas continue;
210 8a35f56c 2022-07-16 thomas if (strncmp(name, "heads/", 6) == 0)
211 8a35f56c 2022-07-16 thomas name += 6;
212 8a35f56c 2022-07-16 thomas if (strncmp(name, "remotes/", 8) == 0) {
213 8a35f56c 2022-07-16 thomas name += 8;
214 6b42af1e 2022-09-02 thomas if (strstr(name, "/" GOT_REF_HEAD) != NULL)
215 8a35f56c 2022-07-16 thomas continue;
217 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&ref_id, t->repo, re->ref);
218 8a35f56c 2022-07-16 thomas if (error)
219 8a35f56c 2022-07-16 thomas return error;
220 8a35f56c 2022-07-16 thomas if (strncmp(name, "tags/", 5) == 0) {
221 8a35f56c 2022-07-16 thomas error = got_object_open_as_tag(&tag, t->repo, ref_id);
222 8a35f56c 2022-07-16 thomas if (error) {
223 8a35f56c 2022-07-16 thomas if (error->code != GOT_ERR_OBJ_TYPE) {
224 8a35f56c 2022-07-16 thomas free(ref_id);
225 8a35f56c 2022-07-16 thomas continue;
228 8a35f56c 2022-07-16 thomas * Ref points at something other
229 8a35f56c 2022-07-16 thomas * than a tag.
231 8a35f56c 2022-07-16 thomas error = NULL;
232 8a35f56c 2022-07-16 thomas tag = NULL;
235 8a35f56c 2022-07-16 thomas cmp = got_object_id_cmp(tag ?
236 8a35f56c 2022-07-16 thomas got_object_tag_get_object_id(tag) : ref_id, id);
237 8a35f56c 2022-07-16 thomas free(ref_id);
239 8a35f56c 2022-07-16 thomas got_object_tag_close(tag);
240 8a35f56c 2022-07-16 thomas if (cmp != 0)
241 8a35f56c 2022-07-16 thomas continue;
242 8a35f56c 2022-07-16 thomas s = repo_commit->refs_str;
243 8a35f56c 2022-07-16 thomas if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
244 8a35f56c 2022-07-16 thomas s ? ", " : "", name) == -1) {
245 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
247 8a35f56c 2022-07-16 thomas repo_commit->refs_str = NULL;
248 8a35f56c 2022-07-16 thomas return error;
253 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->commit_id, id);
254 8a35f56c 2022-07-16 thomas if (error)
255 8a35f56c 2022-07-16 thomas return error;
257 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->tree_id,
258 8a35f56c 2022-07-16 thomas got_object_commit_get_tree_id(commit));
259 8a35f56c 2022-07-16 thomas if (error)
260 8a35f56c 2022-07-16 thomas return error;
262 feaddee6 2023-12-03 thomas if (qs->action == DIFF || qs->action == PATCH) {
263 8a35f56c 2022-07-16 thomas parent_id = STAILQ_FIRST(
264 8a35f56c 2022-07-16 thomas got_object_commit_get_parent_ids(commit));
265 8a35f56c 2022-07-16 thomas if (parent_id != NULL) {
266 8a35f56c 2022-07-16 thomas id2 = got_object_id_dup(&parent_id->id);
267 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->parent_id, id2);
268 8a35f56c 2022-07-16 thomas if (error)
269 8a35f56c 2022-07-16 thomas return error;
270 8a35f56c 2022-07-16 thomas free(id2);
272 8a35f56c 2022-07-16 thomas repo_commit->parent_id = strdup("/dev/null");
273 8a35f56c 2022-07-16 thomas if (repo_commit->parent_id == NULL) {
274 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
275 8a35f56c 2022-07-16 thomas return error;
280 8a35f56c 2022-07-16 thomas repo_commit->committer_time =
281 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
283 8a35f56c 2022-07-16 thomas repo_commit->author =
284 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_author(commit));
285 8a35f56c 2022-07-16 thomas if (repo_commit->author == NULL) {
286 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
287 8a35f56c 2022-07-16 thomas return error;
289 8a35f56c 2022-07-16 thomas repo_commit->committer =
290 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_committer(commit));
291 8a35f56c 2022-07-16 thomas if (repo_commit->committer == NULL) {
292 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
293 8a35f56c 2022-07-16 thomas return error;
295 8a35f56c 2022-07-16 thomas error = got_object_commit_get_logmsg(&commit_msg0, commit);
296 8a35f56c 2022-07-16 thomas if (error)
297 8a35f56c 2022-07-16 thomas return error;
299 8a35f56c 2022-07-16 thomas commit_msg = commit_msg0;
300 8a35f56c 2022-07-16 thomas while (*commit_msg == '\n')
301 8a35f56c 2022-07-16 thomas commit_msg++;
303 8a35f56c 2022-07-16 thomas repo_commit->commit_msg = strdup(commit_msg);
304 8a35f56c 2022-07-16 thomas if (repo_commit->commit_msg == NULL)
305 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
306 8a35f56c 2022-07-16 thomas free(commit_msg0);
307 8a35f56c 2022-07-16 thomas return error;
310 8a35f56c 2022-07-16 thomas const struct got_error *
311 087cc2fc 2023-06-15 thomas got_get_repo_commits(struct request *c, size_t limit)
313 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
314 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
315 8a35f56c 2022-07-16 thomas struct got_commit_graph *graph = NULL;
316 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
317 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
318 4d0573ec 2022-09-02 thomas struct got_reference *ref = NULL;
319 8a35f56c 2022-07-16 thomas struct repo_commit *repo_commit = NULL;
320 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
321 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
322 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
323 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
324 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
325 8a35f56c 2022-07-16 thomas char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
326 7272b60a 2023-06-15 thomas int chk_next = 0;
328 087cc2fc 2023-06-15 thomas if (limit == 0)
329 087cc2fc 2023-06-15 thomas return got_error(GOT_ERR_RANGE);
331 087cc2fc 2023-06-15 thomas if (limit > 1) {
333 7272b60a 2023-06-15 thomas * Traverse one commit more than requested to provide
334 7272b60a 2023-06-15 thomas * the next button.
337 7272b60a 2023-06-15 thomas chk_next = 1;
340 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
342 102d840d 2023-06-22 thomas if (qs->file != NULL && *qs->file != '\0')
343 8a35f56c 2022-07-16 thomas if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
344 8a35f56c 2022-07-16 thomas qs->file) == -1)
345 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
347 8a35f56c 2022-07-16 thomas if (asprintf(&repo_path, "%s/%s", srv->repos_path,
348 31797419 2022-09-02 thomas repo_dir->name) == -1) {
349 31797419 2022-09-02 thomas error = got_error_from_errno("asprintf");
350 31797419 2022-09-02 thomas goto done;
353 42814e01 2023-01-23 thomas if (qs->commit) {
354 42814e01 2023-01-23 thomas error = got_repo_match_object_id_prefix(&id, qs->commit,
355 42814e01 2023-01-23 thomas GOT_OBJ_TYPE_COMMIT, repo);
356 42814e01 2023-01-23 thomas if (error)
357 42814e01 2023-01-23 thomas goto done;
359 8a35f56c 2022-07-16 thomas error = got_ref_open(&ref, repo, qs->headref, 0);
360 8a35f56c 2022-07-16 thomas if (error)
361 8a35f56c 2022-07-16 thomas goto done;
363 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, ref);
364 8a35f56c 2022-07-16 thomas if (error)
365 8a35f56c 2022-07-16 thomas goto done;
368 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, repo_path);
369 8a35f56c 2022-07-16 thomas if (error)
370 8a35f56c 2022-07-16 thomas goto done;
372 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
373 8a35f56c 2022-07-16 thomas if (error)
374 8a35f56c 2022-07-16 thomas goto done;
376 102d840d 2023-06-22 thomas if (qs->file != NULL && *qs->file != '\0') {
377 8a35f56c 2022-07-16 thomas error = got_commit_graph_open(&graph, file_path, 0);
378 8a35f56c 2022-07-16 thomas if (error)
379 8a35f56c 2022-07-16 thomas goto done;
381 8a35f56c 2022-07-16 thomas error = got_commit_graph_open(&graph, in_repo_path, 0);
382 8a35f56c 2022-07-16 thomas if (error)
383 8a35f56c 2022-07-16 thomas goto done;
386 8a35f56c 2022-07-16 thomas error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
387 8a35f56c 2022-07-16 thomas if (error)
388 8a35f56c 2022-07-16 thomas goto done;
390 8a35f56c 2022-07-16 thomas for (;;) {
391 7210b715 2022-09-11 thomas struct got_object_id next_id;
393 95326260 2022-09-05 thomas error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
395 8a35f56c 2022-07-16 thomas if (error) {
396 8a35f56c 2022-07-16 thomas if (error->code == GOT_ERR_ITER_COMPLETED)
397 8a35f56c 2022-07-16 thomas error = NULL;
398 8a35f56c 2022-07-16 thomas goto done;
401 7210b715 2022-09-11 thomas error = got_object_open_as_commit(&commit, repo, &next_id);
402 8a35f56c 2022-07-16 thomas if (error)
403 8a35f56c 2022-07-16 thomas goto done;
405 df9fed09 2022-09-02 thomas error = got_init_repo_commit(&repo_commit);
406 df9fed09 2022-09-02 thomas if (error)
407 df9fed09 2022-09-02 thomas goto done;
409 8a35f56c 2022-07-16 thomas error = got_get_repo_commit(c, repo_commit, commit,
410 7210b715 2022-09-11 thomas &refs, &next_id);
411 349f6f39 2022-09-07 thomas if (error) {
412 349f6f39 2022-09-07 thomas gotweb_free_repo_commit(repo_commit);
413 7272b60a 2023-06-15 thomas goto done;
416 7272b60a 2023-06-15 thomas if (--limit == 0 && chk_next) {
417 7272b60a 2023-06-15 thomas t->more_id = strdup(repo_commit->commit_id);
418 7272b60a 2023-06-15 thomas if (t->more_id == NULL)
419 7272b60a 2023-06-15 thomas error = got_error_from_errno("strdup");
420 8a35f56c 2022-07-16 thomas goto done;
423 349f6f39 2022-09-07 thomas TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
425 7272b60a 2023-06-15 thomas if (limit == 0)
426 7272b60a 2023-06-15 thomas goto done;
428 8a35f56c 2022-07-16 thomas if (commit) {
429 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
430 8a35f56c 2022-07-16 thomas commit = NULL;
435 4d0573ec 2022-09-02 thomas got_ref_close(ref);
436 8a35f56c 2022-07-16 thomas if (commit)
437 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
438 8a35f56c 2022-07-16 thomas if (graph)
439 8a35f56c 2022-07-16 thomas got_commit_graph_close(graph);
440 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
441 bc4d9cc8 2022-09-02 thomas free(in_repo_path);
442 8a35f56c 2022-07-16 thomas free(file_path);
443 8a35f56c 2022-07-16 thomas free(repo_path);
444 8a35f56c 2022-07-16 thomas free(id);
445 8a35f56c 2022-07-16 thomas return error;
448 8a35f56c 2022-07-16 thomas const struct got_error *
449 6c24d194 2023-06-15 thomas got_get_repo_tags(struct request *c, size_t limit)
451 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
452 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
453 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
454 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
455 8a35f56c 2022-07-16 thomas struct got_reference *ref;
456 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
457 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
458 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
459 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
460 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
461 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
462 8a35f56c 2022-07-16 thomas struct got_tag_object *tag = NULL;
463 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
464 8a35f56c 2022-07-16 thomas char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
465 217813df 2022-09-02 thomas char *tag_commit = NULL, *tag_commit0 = NULL;
466 8a35f56c 2022-07-16 thomas char *commit_msg = NULL, *commit_msg0 = NULL;
467 8a35f56c 2022-07-16 thomas int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
469 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
471 6c24d194 2023-06-15 thomas if (limit == 0)
472 6c24d194 2023-06-15 thomas return got_error(GOT_ERR_RANGE);
474 8a35f56c 2022-07-16 thomas if (asprintf(&repo_path, "%s/%s", srv->repos_path,
475 8a35f56c 2022-07-16 thomas repo_dir->name) == -1)
476 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
478 d6795e9f 2022-12-30 thomas if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
479 8a35f56c 2022-07-16 thomas error = got_ref_open(&ref, repo, qs->headref, 0);
480 8a35f56c 2022-07-16 thomas if (error)
481 8a35f56c 2022-07-16 thomas goto err;
482 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, ref);
483 8a35f56c 2022-07-16 thomas got_ref_close(ref);
484 8a35f56c 2022-07-16 thomas if (error)
485 8a35f56c 2022-07-16 thomas goto err;
486 8a35f56c 2022-07-16 thomas } else if (qs->commit == NULL && qs->action == TAG) {
487 8a35f56c 2022-07-16 thomas error = got_error_msg(GOT_ERR_EOF, "commit id missing");
488 8a35f56c 2022-07-16 thomas goto err;
490 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id_prefix(&id, qs->commit,
491 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, repo);
492 8a35f56c 2022-07-16 thomas if (error)
493 8a35f56c 2022-07-16 thomas goto err;
496 8a35f56c 2022-07-16 thomas if (qs->action != SUMMARY && qs->action != TAGS) {
497 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
498 8a35f56c 2022-07-16 thomas if (error)
499 8a35f56c 2022-07-16 thomas goto err;
500 8a35f56c 2022-07-16 thomas error = got_object_commit_get_logmsg(&commit_msg0, commit);
501 8a35f56c 2022-07-16 thomas if (error)
502 8a35f56c 2022-07-16 thomas goto err;
503 8a35f56c 2022-07-16 thomas if (commit) {
504 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
505 8a35f56c 2022-07-16 thomas commit = NULL;
509 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, repo_path);
510 8a35f56c 2022-07-16 thomas if (error)
511 8a35f56c 2022-07-16 thomas goto err;
513 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
515 8a35f56c 2022-07-16 thomas if (error)
516 8a35f56c 2022-07-16 thomas goto err;
518 8a35f56c 2022-07-16 thomas if (limit == 1)
519 8a35f56c 2022-07-16 thomas chk_multi = 0;
522 8a35f56c 2022-07-16 thomas * XXX: again, see previous message about caching
525 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
526 8a35f56c 2022-07-16 thomas struct repo_tag *new_repo_tag = NULL;
527 8a35f56c 2022-07-16 thomas error = got_init_repo_tag(&new_repo_tag);
528 8a35f56c 2022-07-16 thomas if (error)
529 8a35f56c 2022-07-16 thomas goto err;
531 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
533 8a35f56c 2022-07-16 thomas new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
534 8a35f56c 2022-07-16 thomas if (new_repo_tag->tag_name == NULL) {
535 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
536 8a35f56c 2022-07-16 thomas goto err;
539 19d60ee3 2022-09-02 thomas free(id);
540 19d60ee3 2022-09-02 thomas id = NULL;
542 19d60ee3 2022-09-02 thomas free(id_str);
543 19d60ee3 2022-09-02 thomas id_str = NULL;
545 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, re->ref);
546 8a35f56c 2022-07-16 thomas if (error)
547 8a35f56c 2022-07-16 thomas goto done;
550 acd4ff87 2022-09-02 thomas got_object_tag_close(tag);
551 8a35f56c 2022-07-16 thomas error = got_object_open_as_tag(&tag, repo, id);
552 8a35f56c 2022-07-16 thomas if (error) {
553 19d60ee3 2022-09-02 thomas if (error->code != GOT_ERR_OBJ_TYPE)
554 8a35f56c 2022-07-16 thomas goto done;
555 8a35f56c 2022-07-16 thomas /* "lightweight" tag */
556 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
557 19d60ee3 2022-09-02 thomas if (error)
558 8a35f56c 2022-07-16 thomas goto done;
559 8a35f56c 2022-07-16 thomas new_repo_tag->tagger =
560 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_committer(commit));
561 8a35f56c 2022-07-16 thomas if (new_repo_tag->tagger == NULL) {
562 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
563 8a35f56c 2022-07-16 thomas goto err;
565 8a35f56c 2022-07-16 thomas new_repo_tag->tagger_time =
566 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
567 8a35f56c 2022-07-16 thomas error = got_object_id_str(&id_str, id);
568 8a35f56c 2022-07-16 thomas if (error)
569 8a35f56c 2022-07-16 thomas goto err;
571 8a35f56c 2022-07-16 thomas new_repo_tag->tagger =
572 8a35f56c 2022-07-16 thomas strdup(got_object_tag_get_tagger(tag));
573 8a35f56c 2022-07-16 thomas if (new_repo_tag->tagger == NULL) {
574 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
575 8a35f56c 2022-07-16 thomas goto err;
577 8a35f56c 2022-07-16 thomas new_repo_tag->tagger_time =
578 8a35f56c 2022-07-16 thomas got_object_tag_get_tagger_time(tag);
579 8a35f56c 2022-07-16 thomas error = got_object_id_str(&id_str,
580 8a35f56c 2022-07-16 thomas got_object_tag_get_object_id(tag));
581 8a35f56c 2022-07-16 thomas if (error)
582 8a35f56c 2022-07-16 thomas goto err;
585 8a35f56c 2022-07-16 thomas new_repo_tag->commit_id = strdup(id_str);
586 8a35f56c 2022-07-16 thomas if (new_repo_tag->commit_id == NULL)
587 8a35f56c 2022-07-16 thomas goto err;
589 8a35f56c 2022-07-16 thomas if (commit_found == 0 && qs->commit != NULL &&
590 8a35f56c 2022-07-16 thomas strncmp(id_str, qs->commit, strlen(id_str)) != 0)
591 8a35f56c 2022-07-16 thomas continue;
593 8a35f56c 2022-07-16 thomas commit_found = 1;
595 8a35f56c 2022-07-16 thomas t->tag_count++;
598 8a35f56c 2022-07-16 thomas * check for one more commit before breaking,
599 8a35f56c 2022-07-16 thomas * so we know whether to navigate through briefs
600 8a35f56c 2022-07-16 thomas * commits and summary
602 8a35f56c 2022-07-16 thomas if (chk_next) {
603 8a35f56c 2022-07-16 thomas t->next_id = strdup(new_repo_tag->commit_id);
604 8a35f56c 2022-07-16 thomas if (t->next_id == NULL) {
605 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
606 8a35f56c 2022-07-16 thomas goto err;
608 8a35f56c 2022-07-16 thomas if (commit) {
609 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
610 8a35f56c 2022-07-16 thomas commit = NULL;
612 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
613 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(new_repo_tag);
614 8a35f56c 2022-07-16 thomas goto done;
617 8a35f56c 2022-07-16 thomas if (commit) {
618 217813df 2022-09-02 thomas error = got_object_commit_get_logmsg(&tag_commit0,
620 8a35f56c 2022-07-16 thomas if (error)
621 217813df 2022-09-02 thomas goto err;
622 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
623 8a35f56c 2022-07-16 thomas commit = NULL;
625 217813df 2022-09-02 thomas tag_commit0 = strdup(got_object_tag_get_message(tag));
626 217813df 2022-09-02 thomas if (tag_commit0 == NULL) {
627 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
628 217813df 2022-09-02 thomas goto err;
632 217813df 2022-09-02 thomas tag_commit = tag_commit0;
633 217813df 2022-09-02 thomas while (*tag_commit == '\n')
634 217813df 2022-09-02 thomas tag_commit++;
635 217813df 2022-09-02 thomas new_repo_tag->tag_commit = strdup(tag_commit);
636 217813df 2022-09-02 thomas if (new_repo_tag->tag_commit == NULL) {
637 217813df 2022-09-02 thomas error = got_error_from_errno("strdup");
638 217813df 2022-09-02 thomas free(tag_commit0);
639 217813df 2022-09-02 thomas goto err;
641 217813df 2022-09-02 thomas free(tag_commit0);
643 8a35f56c 2022-07-16 thomas if (qs->action != SUMMARY && qs->action != TAGS) {
644 8a35f56c 2022-07-16 thomas commit_msg = commit_msg0;
645 8a35f56c 2022-07-16 thomas while (*commit_msg == '\n')
646 8a35f56c 2022-07-16 thomas commit_msg++;
648 8a35f56c 2022-07-16 thomas new_repo_tag->commit_msg = strdup(commit_msg);
649 8a35f56c 2022-07-16 thomas if (new_repo_tag->commit_msg == NULL) {
650 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
651 8a35f56c 2022-07-16 thomas goto err;
655 8a35f56c 2022-07-16 thomas if (limit && --limit == 0) {
656 8a35f56c 2022-07-16 thomas if (chk_multi == 0)
658 8a35f56c 2022-07-16 thomas chk_next = 1;
664 8a35f56c 2022-07-16 thomas * we have tailq populated, so find previous commit id
665 8a35f56c 2022-07-16 thomas * for navigation through briefs and commits
667 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
668 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
669 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
670 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
673 8a35f56c 2022-07-16 thomas if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
674 8a35f56c 2022-07-16 thomas commit_found = 0;
675 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
677 8a35f56c 2022-07-16 thomas if (commit_found == 0 && rt->commit_id != NULL &&
678 8a35f56c 2022-07-16 thomas strcmp(qs->commit, rt->commit_id) != 0) {
679 8a35f56c 2022-07-16 thomas continue;
681 8a35f56c 2022-07-16 thomas commit_found = 1;
682 8a35f56c 2022-07-16 thomas if (c_cnt == srv->max_commits_display ||
683 8a35f56c 2022-07-16 thomas rt == TAILQ_FIRST(&t->repo_tags)) {
684 8a35f56c 2022-07-16 thomas t->prev_id = strdup(rt->commit_id);
685 8a35f56c 2022-07-16 thomas if (t->prev_id == NULL)
686 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
693 8a35f56c 2022-07-16 thomas if (commit)
694 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
696 19d60ee3 2022-09-02 thomas got_object_tag_close(tag);
697 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
698 ddcf1d33 2022-09-02 thomas free(commit_msg0);
699 19d60ee3 2022-09-02 thomas free(in_repo_path);
700 8a35f56c 2022-07-16 thomas free(repo_path);
701 8a35f56c 2022-07-16 thomas free(id);
702 19d60ee3 2022-09-02 thomas free(id_str);
703 8a35f56c 2022-07-16 thomas return error;
707 34875d49 2023-12-08 thomas got_output_repo_tree(struct request *c, char **readme,
708 3c14c1f2 2023-01-06 thomas int (*cb)(struct template *, struct got_tree_entry *))
710 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
711 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
712 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
713 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
714 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
715 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
716 8a35f56c 2022-07-16 thomas struct got_object_id *tree_id = NULL, *commit_id = NULL;
717 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
718 8a35f56c 2022-07-16 thomas struct got_tree_object *tree = NULL;
719 3c14c1f2 2023-01-06 thomas struct got_tree_entry *te;
720 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
721 34875d49 2023-12-08 thomas const char *name;
722 34875d49 2023-12-08 thomas mode_t mode;
723 06325330 2022-09-02 thomas char *escaped_name = NULL, *path = NULL;
724 3c14c1f2 2023-01-06 thomas int nentries, i;
726 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
727 34875d49 2023-12-08 thomas *readme = NULL;
729 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
731 8a35f56c 2022-07-16 thomas if (qs->folder != NULL) {
732 8a35f56c 2022-07-16 thomas path = strdup(qs->folder);
733 8a35f56c 2022-07-16 thomas if (path == NULL) {
734 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
735 8a35f56c 2022-07-16 thomas goto done;
738 557d32ee 2022-09-02 thomas error = got_repo_map_path(&path, repo, repo_dir->path);
739 8a35f56c 2022-07-16 thomas if (error)
740 8a35f56c 2022-07-16 thomas goto done;
743 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
744 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
745 8a35f56c 2022-07-16 thomas if (error)
746 8a35f56c 2022-07-16 thomas goto done;
748 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
749 8a35f56c 2022-07-16 thomas if (error)
750 8a35f56c 2022-07-16 thomas goto done;
752 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&tree_id, repo, commit, path);
753 8a35f56c 2022-07-16 thomas if (error)
754 8a35f56c 2022-07-16 thomas goto done;
756 8a35f56c 2022-07-16 thomas error = got_object_open_as_tree(&tree, repo, tree_id);
757 8a35f56c 2022-07-16 thomas if (error)
758 8a35f56c 2022-07-16 thomas goto done;
760 8a35f56c 2022-07-16 thomas nentries = got_object_tree_get_nentries(tree);
762 8a35f56c 2022-07-16 thomas for (i = 0; i < nentries; i++) {
763 8a35f56c 2022-07-16 thomas te = got_object_tree_get_entry(tree, i);
765 34875d49 2023-12-08 thomas name = got_tree_entry_get_name(te);
766 34875d49 2023-12-08 thomas mode = got_tree_entry_get_mode(te);
767 34875d49 2023-12-08 thomas if (!S_ISDIR(mode) && (!strcasecmp(name, "README") ||
768 34875d49 2023-12-08 thomas !strcasecmp(name, "README.md") ||
769 34875d49 2023-12-08 thomas !strcasecmp(name, "README.txt"))) {
770 34875d49 2023-12-08 thomas free(*readme);
771 34875d49 2023-12-08 thomas *readme = strdup(name);
774 8f464e92 2023-02-17 thomas if (cb(c->tp, te) == -1) {
775 8f464e92 2023-02-17 thomas error = got_error(GOT_ERR_CANCELLED);
780 df610f47 2022-08-27 thomas free(escaped_name);
781 8a35f56c 2022-07-16 thomas free(path);
782 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
783 8a35f56c 2022-07-16 thomas if (commit)
784 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
785 12e242cc 2022-09-06 thomas if (tree)
786 12e242cc 2022-09-06 thomas got_object_tree_close(tree);
787 8a35f56c 2022-07-16 thomas free(commit_id);
788 8a35f56c 2022-07-16 thomas free(tree_id);
789 3c14c1f2 2023-01-06 thomas if (error) {
790 34875d49 2023-12-08 thomas free(*readme);
791 34875d49 2023-12-08 thomas *readme = NULL;
792 aa2aecab 2023-05-25 thomas if (error->code != GOT_ERR_CANCELLED)
793 aa2aecab 2023-05-25 thomas log_warnx("%s: %s", __func__, error->msg);
794 3c14c1f2 2023-01-06 thomas return -1;
796 3c14c1f2 2023-01-06 thomas return 0;
799 8a35f56c 2022-07-16 thomas const struct got_error *
800 b82440e1 2023-01-06 thomas got_open_blob_for_output(struct got_blob_object **blob, int *fd,
801 34875d49 2023-12-08 thomas int *binary, struct request *c, const char *directory, const char *file,
802 34875d49 2023-12-08 thomas const char *commitstr)
804 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
805 34875d49 2023-12-08 thomas struct got_repository *repo = c->t->repo;
806 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
807 8a35f56c 2022-07-16 thomas struct got_object_id *commit_id = NULL;
808 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
809 8a35f56c 2022-07-16 thomas char *path = NULL, *in_repo_path = NULL;
810 b82440e1 2023-01-06 thomas int obj_type;
812 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
814 b82440e1 2023-01-06 thomas *blob = NULL;
815 b82440e1 2023-01-06 thomas *fd = -1;
816 b82440e1 2023-01-06 thomas *binary = 0;
818 34875d49 2023-12-08 thomas error = got_ref_list(&refs, repo, "refs/heads",
819 34875d49 2023-12-08 thomas got_ref_cmp_by_name, NULL);
820 34875d49 2023-12-08 thomas if (error)
821 34875d49 2023-12-08 thomas goto done;
823 34875d49 2023-12-08 thomas if (asprintf(&path, "%s%s%s", directory ? directory : "",
824 34875d49 2023-12-08 thomas directory ? "/" : "", file) == -1) {
825 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
826 8a35f56c 2022-07-16 thomas goto done;
829 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, path);
830 8a35f56c 2022-07-16 thomas if (error)
831 8a35f56c 2022-07-16 thomas goto done;
833 34875d49 2023-12-08 thomas if (commitstr == NULL)
834 34875d49 2023-12-08 thomas commitstr = GOT_REF_HEAD;
836 34875d49 2023-12-08 thomas error = got_repo_match_object_id(&commit_id, NULL, commitstr,
837 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
838 8a35f56c 2022-07-16 thomas if (error)
839 8a35f56c 2022-07-16 thomas goto done;
841 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
842 8a35f56c 2022-07-16 thomas if (error)
843 8a35f56c 2022-07-16 thomas goto done;
845 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
846 8a35f56c 2022-07-16 thomas if (error)
847 8a35f56c 2022-07-16 thomas goto done;
849 8a35f56c 2022-07-16 thomas if (commit_id == NULL) {
850 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_NO_OBJ);
851 8a35f56c 2022-07-16 thomas goto done;
854 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, commit_id);
855 8a35f56c 2022-07-16 thomas if (error)
856 8a35f56c 2022-07-16 thomas goto done;
858 8a35f56c 2022-07-16 thomas if (obj_type != GOT_OBJ_TYPE_BLOB) {
859 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
860 8a35f56c 2022-07-16 thomas goto done;
863 b82440e1 2023-01-06 thomas error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
864 8a35f56c 2022-07-16 thomas if (error)
865 8a35f56c 2022-07-16 thomas goto done;
867 b82440e1 2023-01-06 thomas error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
868 8a35f56c 2022-07-16 thomas if (error)
869 8a35f56c 2022-07-16 thomas goto done;
871 b82440e1 2023-01-06 thomas error = got_object_blob_is_binary(binary, *blob);
872 efe3fd03 2023-01-06 thomas if (error)
873 efe3fd03 2023-01-06 thomas goto done;
876 b82440e1 2023-01-06 thomas if (commit)
877 b82440e1 2023-01-06 thomas got_object_commit_close(commit);
879 b82440e1 2023-01-06 thomas if (error) {
880 b82440e1 2023-01-06 thomas if (*fd != -1)
881 b82440e1 2023-01-06 thomas close(*fd);
882 b82440e1 2023-01-06 thomas if (*blob)
883 b82440e1 2023-01-06 thomas got_object_blob_close(*blob);
884 b82440e1 2023-01-06 thomas *fd = -1;
885 b82440e1 2023-01-06 thomas *blob = NULL;
888 34875d49 2023-12-08 thomas got_ref_list_free(&refs);
889 b82440e1 2023-01-06 thomas free(in_repo_path);
890 b82440e1 2023-01-06 thomas free(commit_id);
891 b82440e1 2023-01-06 thomas free(path);
892 8a35f56c 2022-07-16 thomas return error;
896 b82440e1 2023-01-06 thomas got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
897 b82440e1 2023-01-06 thomas int (*cb)(struct template *, const char *, size_t))
899 b82440e1 2023-01-06 thomas const struct got_error *err;
900 b82440e1 2023-01-06 thomas char *line = NULL;
901 b82440e1 2023-01-06 thomas size_t linesize = 0;
902 b82440e1 2023-01-06 thomas size_t lineno = 0;
903 b82440e1 2023-01-06 thomas ssize_t linelen = 0;
905 b82440e1 2023-01-06 thomas for (;;) {
906 b82440e1 2023-01-06 thomas err = got_object_blob_getline(&line, &linelen, &linesize,
908 b82440e1 2023-01-06 thomas if (err || linelen == -1)
910 b82440e1 2023-01-06 thomas lineno++;
911 8f464e92 2023-02-17 thomas if (cb(tp, line, lineno) == -1) {
912 8f464e92 2023-02-17 thomas err = got_error(GOT_ERR_CANCELLED);
917 b82440e1 2023-01-06 thomas free(line);
919 b82440e1 2023-01-06 thomas if (err) {
920 aa2aecab 2023-05-25 thomas if (err->code != GOT_ERR_CANCELLED)
921 aa2aecab 2023-05-25 thomas log_warnx("%s: got_object_blob_getline failed: %s",
922 aa2aecab 2023-05-25 thomas __func__, err->msg);
923 b82440e1 2023-01-06 thomas return -1;
925 b82440e1 2023-01-06 thomas return 0;
928 8a35f56c 2022-07-16 thomas struct blame_cb_args {
929 8a35f56c 2022-07-16 thomas struct blame_line *lines;
930 8a35f56c 2022-07-16 thomas int nlines;
931 8a35f56c 2022-07-16 thomas int nlines_prec;
932 8a35f56c 2022-07-16 thomas int lineno_cur;
933 8a35f56c 2022-07-16 thomas off_t *line_offsets;
935 8a35f56c 2022-07-16 thomas struct got_repository *repo;
936 8a35f56c 2022-07-16 thomas struct request *c;
937 1cd5d437 2023-01-15 thomas got_render_blame_line_cb cb;
940 8a35f56c 2022-07-16 thomas static const struct got_error *
941 8a35f56c 2022-07-16 thomas got_gotweb_blame_cb(void *arg, int nlines, int lineno,
942 8a35f56c 2022-07-16 thomas struct got_commit_object *commit, struct got_object_id *id)
944 8a35f56c 2022-07-16 thomas const struct got_error *err = NULL;
945 8a35f56c 2022-07-16 thomas struct blame_cb_args *a = arg;
946 8a35f56c 2022-07-16 thomas struct blame_line *bline;
947 8a35f56c 2022-07-16 thomas struct request *c = a->c;
948 1cd5d437 2023-01-15 thomas char *line = NULL;
949 8a35f56c 2022-07-16 thomas size_t linesize = 0;
950 8a35f56c 2022-07-16 thomas off_t offset;
951 8a35f56c 2022-07-16 thomas struct tm tm;
952 8a35f56c 2022-07-16 thomas time_t committer_time;
954 8a35f56c 2022-07-16 thomas if (nlines != a->nlines ||
955 8a35f56c 2022-07-16 thomas (lineno != -1 && lineno < 1) || lineno > a->nlines)
956 8a35f56c 2022-07-16 thomas return got_error(GOT_ERR_RANGE);
958 8a35f56c 2022-07-16 thomas if (lineno == -1)
959 8a35f56c 2022-07-16 thomas return NULL; /* no change in this commit */
961 8a35f56c 2022-07-16 thomas /* Annotate this line. */
962 8a35f56c 2022-07-16 thomas bline = &a->lines[lineno - 1];
963 8a35f56c 2022-07-16 thomas if (bline->annotated)
964 8a35f56c 2022-07-16 thomas return NULL;
965 8a35f56c 2022-07-16 thomas err = got_object_id_str(&bline->id_str, id);
967 8a35f56c 2022-07-16 thomas return err;
969 8a35f56c 2022-07-16 thomas bline->committer = strdup(got_object_commit_get_committer(commit));
970 8a35f56c 2022-07-16 thomas if (bline->committer == NULL) {
971 8a35f56c 2022-07-16 thomas err = got_error_from_errno("strdup");
972 8a35f56c 2022-07-16 thomas goto done;
975 8a35f56c 2022-07-16 thomas committer_time = got_object_commit_get_committer_time(commit);
976 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
977 8a35f56c 2022-07-16 thomas return got_error_from_errno("gmtime_r");
978 8a35f56c 2022-07-16 thomas if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
979 8a35f56c 2022-07-16 thomas &tm) == 0) {
980 8a35f56c 2022-07-16 thomas err = got_error(GOT_ERR_NO_SPACE);
981 8a35f56c 2022-07-16 thomas goto done;
983 8a35f56c 2022-07-16 thomas bline->annotated = 1;
985 8a35f56c 2022-07-16 thomas /* Print lines annotated so far. */
986 8a35f56c 2022-07-16 thomas bline = &a->lines[a->lineno_cur - 1];
987 8a35f56c 2022-07-16 thomas if (!bline->annotated)
988 8a35f56c 2022-07-16 thomas goto done;
990 8a35f56c 2022-07-16 thomas offset = a->line_offsets[a->lineno_cur - 1];
991 8a35f56c 2022-07-16 thomas if (fseeko(a->f, offset, SEEK_SET) == -1) {
992 8a35f56c 2022-07-16 thomas err = got_error_from_errno("fseeko");
993 8a35f56c 2022-07-16 thomas goto done;
996 b5c07627 2022-08-18 thomas while (a->lineno_cur <= a->nlines && bline->annotated) {
997 8a35f56c 2022-07-16 thomas if (getline(&line, &linesize, a->f) == -1) {
998 8a35f56c 2022-07-16 thomas if (ferror(a->f))
999 8a35f56c 2022-07-16 thomas err = got_error_from_errno("getline");
1003 1cd5d437 2023-01-15 thomas if (a->cb(c->tp, line, bline, a->nlines_prec,
1004 ad5cb26d 2023-02-17 thomas a->lineno_cur) == -1) {
1005 ad5cb26d 2023-02-17 thomas err = got_error(GOT_ERR_CANCELLED);
1009 8a35f56c 2022-07-16 thomas a->lineno_cur++;
1010 8a35f56c 2022-07-16 thomas bline = &a->lines[a->lineno_cur - 1];
1013 8a35f56c 2022-07-16 thomas free(line);
1014 8a35f56c 2022-07-16 thomas return err;
1017 8a35f56c 2022-07-16 thomas const struct got_error *
1018 1cd5d437 2023-01-15 thomas got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1020 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1021 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1022 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1023 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
1024 8a35f56c 2022-07-16 thomas struct got_object_id *obj_id = NULL, *commit_id = NULL;
1025 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
1026 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1027 8a35f56c 2022-07-16 thomas struct got_blob_object *blob = NULL;
1028 8a35f56c 2022-07-16 thomas char *path = NULL, *in_repo_path = NULL;
1029 8a35f56c 2022-07-16 thomas struct blame_cb_args bca;
1030 db26fc4c 2023-05-16 thomas int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
1031 8a35f56c 2022-07-16 thomas off_t filesize;
1032 8a35f56c 2022-07-16 thomas FILE *f1 = NULL, *f2 = NULL;
1034 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1035 8a35f56c 2022-07-16 thomas bca.f = NULL;
1036 8a35f56c 2022-07-16 thomas bca.lines = NULL;
1037 1cd5d437 2023-01-15 thomas bca.cb = cb;
1039 8a35f56c 2022-07-16 thomas if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1040 8a35f56c 2022-07-16 thomas qs->folder ? "/" : "", qs->file) == -1) {
1041 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1042 8a35f56c 2022-07-16 thomas goto done;
1045 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, path);
1046 8a35f56c 2022-07-16 thomas if (error)
1047 8a35f56c 2022-07-16 thomas goto done;
1049 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1050 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
1051 8a35f56c 2022-07-16 thomas if (error)
1052 8a35f56c 2022-07-16 thomas goto done;
1054 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
1055 8a35f56c 2022-07-16 thomas if (error)
1056 8a35f56c 2022-07-16 thomas goto done;
1058 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1059 8a35f56c 2022-07-16 thomas if (error)
1060 8a35f56c 2022-07-16 thomas goto done;
1062 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, obj_id);
1063 8a35f56c 2022-07-16 thomas if (error)
1064 8a35f56c 2022-07-16 thomas goto done;
1066 8a35f56c 2022-07-16 thomas if (obj_type != GOT_OBJ_TYPE_BLOB) {
1067 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
1068 8a35f56c 2022-07-16 thomas goto done;
1071 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1072 8a35f56c 2022-07-16 thomas if (error)
1073 8a35f56c 2022-07-16 thomas goto done;
1075 db26fc4c 2023-05-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1076 8a35f56c 2022-07-16 thomas if (error)
1077 8a35f56c 2022-07-16 thomas goto done;
1079 db26fc4c 2023-05-16 thomas error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1080 8a35f56c 2022-07-16 thomas if (error)
1081 8a35f56c 2022-07-16 thomas goto done;
1083 8a35f56c 2022-07-16 thomas error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1084 8a35f56c 2022-07-16 thomas &bca.line_offsets, bca.f, blob);
1085 8a35f56c 2022-07-16 thomas if (error || bca.nlines == 0)
1086 8a35f56c 2022-07-16 thomas goto done;
1088 8a35f56c 2022-07-16 thomas /* Don't include \n at EOF in the blame line count. */
1089 8a35f56c 2022-07-16 thomas if (bca.line_offsets[bca.nlines - 1] == filesize)
1090 8a35f56c 2022-07-16 thomas bca.nlines--;
1092 8a35f56c 2022-07-16 thomas bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1093 8a35f56c 2022-07-16 thomas if (bca.lines == NULL) {
1094 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1095 8a35f56c 2022-07-16 thomas goto done;
1097 8a35f56c 2022-07-16 thomas bca.lineno_cur = 1;
1098 8a35f56c 2022-07-16 thomas bca.nlines_prec = 0;
1099 8a35f56c 2022-07-16 thomas i = bca.nlines;
1100 8a35f56c 2022-07-16 thomas while (i > 0) {
1101 8a35f56c 2022-07-16 thomas i /= 10;
1102 8a35f56c 2022-07-16 thomas bca.nlines_prec++;
1104 8a35f56c 2022-07-16 thomas bca.repo = repo;
1105 8a35f56c 2022-07-16 thomas bca.c = c;
1107 db26fc4c 2023-05-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1108 8a35f56c 2022-07-16 thomas if (error)
1109 8a35f56c 2022-07-16 thomas goto done;
1111 db26fc4c 2023-05-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1112 8a35f56c 2022-07-16 thomas if (error)
1113 8a35f56c 2022-07-16 thomas goto done;
1115 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1116 8a35f56c 2022-07-16 thomas if (error)
1117 8a35f56c 2022-07-16 thomas goto done;
1119 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1120 8a35f56c 2022-07-16 thomas if (error)
1121 8a35f56c 2022-07-16 thomas goto done;
1123 8a35f56c 2022-07-16 thomas error = got_blame(in_repo_path, commit_id, repo,
1124 8a35f56c 2022-07-16 thomas GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1125 db26fc4c 2023-05-16 thomas fd1, fd2, f1, f2);
1128 d38635a3 2022-09-03 thomas if (bca.lines) {
1129 8a35f56c 2022-07-16 thomas free(bca.line_offsets);
1130 8a35f56c 2022-07-16 thomas for (i = 0; i < bca.nlines; i++) {
1131 8a35f56c 2022-07-16 thomas struct blame_line *bline = &bca.lines[i];
1132 8a35f56c 2022-07-16 thomas free(bline->id_str);
1133 8a35f56c 2022-07-16 thomas free(bline->committer);
1136 8a35f56c 2022-07-16 thomas free(bca.lines);
1137 db26fc4c 2023-05-16 thomas if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1138 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1139 db26fc4c 2023-05-16 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1140 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1141 db26fc4c 2023-05-16 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1142 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1143 8a35f56c 2022-07-16 thomas if (bca.f) {
1144 d00235d8 2023-05-17 thomas const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1145 8a35f56c 2022-07-16 thomas if (error == NULL)
1146 8a35f56c 2022-07-16 thomas error = bca_err;
1148 8a35f56c 2022-07-16 thomas if (f1) {
1149 d00235d8 2023-05-17 thomas const struct got_error *f1_err = got_gotweb_closefile(f1);
1150 8a35f56c 2022-07-16 thomas if (error == NULL)
1151 8a35f56c 2022-07-16 thomas error = f1_err;
1153 8a35f56c 2022-07-16 thomas if (f2) {
1154 d00235d8 2023-05-17 thomas const struct got_error *f2_err = got_gotweb_closefile(f2);
1155 8a35f56c 2022-07-16 thomas if (error == NULL)
1156 8a35f56c 2022-07-16 thomas error = f2_err;
1158 8a35f56c 2022-07-16 thomas if (commit)
1159 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
1160 8a35f56c 2022-07-16 thomas if (blob)
1161 8a35f56c 2022-07-16 thomas got_object_blob_close(blob);
1162 8a35f56c 2022-07-16 thomas free(in_repo_path);
1163 8a35f56c 2022-07-16 thomas free(commit_id);
1164 aefd24c9 2022-09-03 thomas free(obj_id);
1165 8a35f56c 2022-07-16 thomas free(path);
1166 aefd24c9 2022-09-03 thomas got_ref_list_free(&refs);
1167 8a35f56c 2022-07-16 thomas return error;
1170 8a35f56c 2022-07-16 thomas const struct got_error *
1171 04b2c111 2023-05-16 thomas got_open_diff_for_output(FILE **fp, struct request *c)
1173 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1174 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1175 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1176 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1177 8a35f56c 2022-07-16 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
1178 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1179 8a35f56c 2022-07-16 thomas FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1180 db26fc4c 2023-05-16 thomas int obj_type, fd1 = -1, fd2 = -1;
1182 dccd05b4 2023-01-10 thomas *fp = NULL;
1184 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1186 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1187 8a35f56c 2022-07-16 thomas if (error)
1188 8a35f56c 2022-07-16 thomas return error;
1190 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1191 8a35f56c 2022-07-16 thomas if (error)
1192 8a35f56c 2022-07-16 thomas return error;
1194 04b2c111 2023-05-16 thomas error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1195 8a35f56c 2022-07-16 thomas if (error)
1196 8a35f56c 2022-07-16 thomas return error;
1198 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1200 8a35f56c 2022-07-16 thomas if (rc->parent_id != NULL &&
1201 8a35f56c 2022-07-16 thomas strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1202 dccd05b4 2023-01-10 thomas error = got_repo_match_object_id(&id1, NULL,
1203 8a35f56c 2022-07-16 thomas rc->parent_id, GOT_OBJ_TYPE_ANY,
1204 8a35f56c 2022-07-16 thomas &refs, repo);
1205 8a35f56c 2022-07-16 thomas if (error)
1206 8a35f56c 2022-07-16 thomas goto done;
1209 dccd05b4 2023-01-10 thomas error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1210 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_ANY, &refs, repo);
1211 8a35f56c 2022-07-16 thomas if (error)
1212 8a35f56c 2022-07-16 thomas goto done;
1214 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, id2);
1215 8a35f56c 2022-07-16 thomas if (error)
1216 8a35f56c 2022-07-16 thomas goto done;
1218 db26fc4c 2023-05-16 thomas error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1219 8a35f56c 2022-07-16 thomas if (error)
1220 8a35f56c 2022-07-16 thomas goto done;
1222 db26fc4c 2023-05-16 thomas error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1223 8a35f56c 2022-07-16 thomas if (error)
1224 8a35f56c 2022-07-16 thomas goto done;
1226 8a35f56c 2022-07-16 thomas switch (obj_type) {
1227 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_BLOB:
1228 db26fc4c 2023-05-16 thomas error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1229 8a35f56c 2022-07-16 thomas id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1230 be97ab03 2023-01-19 thomas NULL, repo, f3);
1232 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_TREE:
1233 db26fc4c 2023-05-16 thomas error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1234 8a35f56c 2022-07-16 thomas id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1235 be97ab03 2023-01-19 thomas NULL, repo, f3);
1237 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_COMMIT:
1238 db26fc4c 2023-05-16 thomas error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1239 db26fc4c 2023-05-16 thomas fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1240 be97ab03 2023-01-19 thomas NULL, repo, f3);
1242 8a35f56c 2022-07-16 thomas default:
1243 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
1245 8a35f56c 2022-07-16 thomas if (error)
1246 8a35f56c 2022-07-16 thomas goto done;
1248 8a35f56c 2022-07-16 thomas if (fseek(f3, 0, SEEK_SET) == -1) {
1249 8a35f56c 2022-07-16 thomas error = got_ferror(f3, GOT_ERR_IO);
1250 8a35f56c 2022-07-16 thomas goto done;
1253 dccd05b4 2023-01-10 thomas *fp = f3;
1256 db26fc4c 2023-05-16 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1257 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1258 db26fc4c 2023-05-16 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1259 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1260 8a35f56c 2022-07-16 thomas if (f1) {
1261 d00235d8 2023-05-17 thomas const struct got_error *f1_err = got_gotweb_closefile(f1);
1262 8a35f56c 2022-07-16 thomas if (error == NULL)
1263 8a35f56c 2022-07-16 thomas error = f1_err;
1265 8a35f56c 2022-07-16 thomas if (f2) {
1266 d00235d8 2023-05-17 thomas const struct got_error *f2_err = got_gotweb_closefile(f2);
1267 8a35f56c 2022-07-16 thomas if (error == NULL)
1268 8a35f56c 2022-07-16 thomas error = f2_err;
1270 dccd05b4 2023-01-10 thomas if (error && f3) {
1271 d00235d8 2023-05-17 thomas got_gotweb_closefile(f3);
1272 dccd05b4 2023-01-10 thomas *fp = NULL;
1274 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
1275 8a35f56c 2022-07-16 thomas free(id1);
1276 8a35f56c 2022-07-16 thomas free(id2);
1277 8a35f56c 2022-07-16 thomas return error;
1280 8a35f56c 2022-07-16 thomas static const struct got_error *
1281 8a35f56c 2022-07-16 thomas got_init_repo_commit(struct repo_commit **rc)
1283 8a35f56c 2022-07-16 thomas *rc = calloc(1, sizeof(**rc));
1284 8a35f56c 2022-07-16 thomas if (*rc == NULL)
1285 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "calloc");
1287 8a35f56c 2022-07-16 thomas (*rc)->path = NULL;
1288 8a35f56c 2022-07-16 thomas (*rc)->refs_str = NULL;
1289 8a35f56c 2022-07-16 thomas (*rc)->commit_id = NULL;
1290 8a35f56c 2022-07-16 thomas (*rc)->committer = NULL;
1291 8a35f56c 2022-07-16 thomas (*rc)->author = NULL;
1292 8a35f56c 2022-07-16 thomas (*rc)->parent_id = NULL;
1293 8a35f56c 2022-07-16 thomas (*rc)->tree_id = NULL;
1294 8a35f56c 2022-07-16 thomas (*rc)->commit_msg = NULL;
1296 e2af4cd7 2022-08-31 thomas return NULL;
1299 8a35f56c 2022-07-16 thomas static const struct got_error *
1300 8a35f56c 2022-07-16 thomas got_init_repo_tag(struct repo_tag **rt)
1302 8a35f56c 2022-07-16 thomas *rt = calloc(1, sizeof(**rt));
1303 8a35f56c 2022-07-16 thomas if (*rt == NULL)
1304 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "calloc");
1306 8a35f56c 2022-07-16 thomas (*rt)->commit_id = NULL;
1307 8a35f56c 2022-07-16 thomas (*rt)->tag_name = NULL;
1308 8a35f56c 2022-07-16 thomas (*rt)->tag_commit = NULL;
1309 8a35f56c 2022-07-16 thomas (*rt)->commit_msg = NULL;
1310 8a35f56c 2022-07-16 thomas (*rt)->tagger = NULL;
1312 e2af4cd7 2022-08-31 thomas return NULL;