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 3e12c168 2022-07-16 thomas #include <sys/queue.h>
19 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
20 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
22 8a35f56c 2022-07-16 thomas #include <event.h>
23 8a35f56c 2022-07-16 thomas #include <stdlib.h>
24 8a35f56c 2022-07-16 thomas #include <stdio.h>
25 8a35f56c 2022-07-16 thomas #include <string.h>
26 8a35f56c 2022-07-16 thomas #include <unistd.h>
28 8a35f56c 2022-07-16 thomas #include "got_error.h"
29 8a35f56c 2022-07-16 thomas #include "got_object.h"
30 8a35f56c 2022-07-16 thomas #include "got_reference.h"
31 8a35f56c 2022-07-16 thomas #include "got_repository.h"
32 8a35f56c 2022-07-16 thomas #include "got_path.h"
33 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
34 8a35f56c 2022-07-16 thomas #include "got_diff.h"
35 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
36 8a35f56c 2022-07-16 thomas #include "got_blame.h"
37 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
39 ff36aeea 2022-07-16 thomas #include "got_compat.h"
41 8a35f56c 2022-07-16 thomas #include "proc.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 8a35f56c 2022-07-16 thomas static const struct got_error *got_gotweb_openfile(FILE **, int *, 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 8a35f56c 2022-07-16 thomas got_gotweb_flushfile(FILE *f, int fd)
57 8a35f56c 2022-07-16 thomas if (fseek(f, 0, SEEK_SET) == -1)
58 8a35f56c 2022-07-16 thomas return got_error_from_errno("fseek");
60 8a35f56c 2022-07-16 thomas if (ftruncate(fd, 0) == -1)
61 8a35f56c 2022-07-16 thomas return got_error_from_errno("ftruncate");
63 8a35f56c 2022-07-16 thomas if (fsync(fd) == -1)
64 8a35f56c 2022-07-16 thomas return got_error_from_errno("fsync");
66 8a35f56c 2022-07-16 thomas if (f && fclose(f) == EOF)
67 8a35f56c 2022-07-16 thomas return got_error_from_errno("fclose");
69 8a35f56c 2022-07-16 thomas if (fd != -1 && close(fd) != -1)
70 8a35f56c 2022-07-16 thomas return got_error_from_errno("close");
72 8a35f56c 2022-07-16 thomas return NULL;
75 8a35f56c 2022-07-16 thomas static const struct got_error *
76 8a35f56c 2022-07-16 thomas got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
78 8a35f56c 2022-07-16 thomas *fd = dup(*priv_fd);
79 6e596ed0 2022-08-31 thomas if (*fd == -1)
80 6e596ed0 2022-08-31 thomas return got_error_from_errno("dup");
82 8a35f56c 2022-07-16 thomas *f = fdopen(*fd, "w+");
83 8a35f56c 2022-07-16 thomas if (*f == NULL) {
84 8a35f56c 2022-07-16 thomas close(*fd);
85 e2af4cd7 2022-08-31 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
88 e2af4cd7 2022-08-31 thomas return NULL;
91 8a35f56c 2022-07-16 thomas static const struct got_error *
92 8a35f56c 2022-07-16 thomas got_gotweb_dupfd(int *priv_fd, int *fd)
94 8a35f56c 2022-07-16 thomas *fd = dup(*priv_fd);
96 8a35f56c 2022-07-16 thomas if (*fd < 0)
97 8a35f56c 2022-07-16 thomas return NULL;
99 e2af4cd7 2022-08-31 thomas return NULL;
102 8a35f56c 2022-07-16 thomas const struct got_error *
103 6c7f10f7 2022-11-23 thomas got_get_repo_owner(char **owner, struct request *c)
105 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
106 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
107 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
108 8a35f56c 2022-07-16 thomas const char *gitconfig_owner;
110 8a35f56c 2022-07-16 thomas *owner = NULL;
112 8a35f56c 2022-07-16 thomas if (srv->show_repo_owner == 0)
113 8a35f56c 2022-07-16 thomas return NULL;
115 8a35f56c 2022-07-16 thomas gitconfig_owner = got_repo_get_gitconfig_owner(repo);
116 8a35f56c 2022-07-16 thomas if (gitconfig_owner) {
117 8a35f56c 2022-07-16 thomas *owner = strdup(gitconfig_owner);
118 2f26d334 2022-08-30 thomas if (*owner == NULL)
119 2f26d334 2022-08-30 thomas return got_error_from_errno("strdup");
121 2f26d334 2022-08-30 thomas *owner = strdup("");
122 8a35f56c 2022-07-16 thomas if (*owner == NULL)
123 8a35f56c 2022-07-16 thomas return got_error_from_errno("strdup");
125 e2af4cd7 2022-08-31 thomas return NULL;
128 8a35f56c 2022-07-16 thomas const struct got_error *
129 53bf32b8 2023-01-23 thomas got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
131 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
132 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
133 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
134 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
135 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
136 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
137 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
138 8a35f56c 2022-07-16 thomas time_t committer_time = 0, cmp_time = 0;
140 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
142 8a35f56c 2022-07-16 thomas if (srv->show_repo_age == 0)
143 8a35f56c 2022-07-16 thomas return NULL;
145 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/heads",
146 8a35f56c 2022-07-16 thomas got_ref_cmp_by_name, NULL);
147 8a35f56c 2022-07-16 thomas if (error)
148 8a35f56c 2022-07-16 thomas goto done;
151 8a35f56c 2022-07-16 thomas * Find the youngest branch tip in the repository, or the age of
152 8a35f56c 2022-07-16 thomas * the a specific branch tip if a name was provided by the caller.
154 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
155 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
157 8a35f56c 2022-07-16 thomas if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
158 8a35f56c 2022-07-16 thomas continue;
160 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, re->ref);
161 8a35f56c 2022-07-16 thomas if (error)
162 8a35f56c 2022-07-16 thomas goto done;
164 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
165 8a35f56c 2022-07-16 thomas free(id);
166 8a35f56c 2022-07-16 thomas if (error)
167 8a35f56c 2022-07-16 thomas goto done;
169 8a35f56c 2022-07-16 thomas committer_time =
170 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
171 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
172 8a35f56c 2022-07-16 thomas if (cmp_time < committer_time)
173 8a35f56c 2022-07-16 thomas cmp_time = committer_time;
175 8a35f56c 2022-07-16 thomas if (refname)
179 53bf32b8 2023-01-23 thomas if (cmp_time != 0)
180 53bf32b8 2023-01-23 thomas *repo_age = cmp_time;
182 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
183 8a35f56c 2022-07-16 thomas return error;
186 8a35f56c 2022-07-16 thomas static const struct got_error *
187 8a35f56c 2022-07-16 thomas got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
188 8a35f56c 2022-07-16 thomas struct got_commit_object *commit, struct got_reflist_head *refs,
189 8a35f56c 2022-07-16 thomas struct got_object_id *id)
191 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
192 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
193 8a35f56c 2022-07-16 thomas struct got_object_id *id2 = NULL;
194 8a35f56c 2022-07-16 thomas struct got_object_qid *parent_id;
195 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
196 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
197 8a35f56c 2022-07-16 thomas char *commit_msg = NULL, *commit_msg0;
199 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, refs, entry) {
201 8a35f56c 2022-07-16 thomas const char *name;
202 8a35f56c 2022-07-16 thomas struct got_tag_object *tag = NULL;
203 8a35f56c 2022-07-16 thomas struct got_object_id *ref_id;
206 8a35f56c 2022-07-16 thomas if (got_ref_is_symbolic(re->ref))
207 8a35f56c 2022-07-16 thomas continue;
209 8a35f56c 2022-07-16 thomas name = got_ref_get_name(re->ref);
210 8a35f56c 2022-07-16 thomas if (strncmp(name, "refs/", 5) == 0)
211 8a35f56c 2022-07-16 thomas name += 5;
212 8a35f56c 2022-07-16 thomas if (strncmp(name, "got/", 4) == 0)
213 8a35f56c 2022-07-16 thomas continue;
214 8a35f56c 2022-07-16 thomas if (strncmp(name, "heads/", 6) == 0)
215 8a35f56c 2022-07-16 thomas name += 6;
216 8a35f56c 2022-07-16 thomas if (strncmp(name, "remotes/", 8) == 0) {
217 8a35f56c 2022-07-16 thomas name += 8;
218 6b42af1e 2022-09-02 thomas if (strstr(name, "/" GOT_REF_HEAD) != NULL)
219 8a35f56c 2022-07-16 thomas continue;
221 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&ref_id, t->repo, re->ref);
222 8a35f56c 2022-07-16 thomas if (error)
223 8a35f56c 2022-07-16 thomas return error;
224 8a35f56c 2022-07-16 thomas if (strncmp(name, "tags/", 5) == 0) {
225 8a35f56c 2022-07-16 thomas error = got_object_open_as_tag(&tag, t->repo, ref_id);
226 8a35f56c 2022-07-16 thomas if (error) {
227 8a35f56c 2022-07-16 thomas if (error->code != GOT_ERR_OBJ_TYPE) {
228 8a35f56c 2022-07-16 thomas free(ref_id);
229 8a35f56c 2022-07-16 thomas continue;
232 8a35f56c 2022-07-16 thomas * Ref points at something other
233 8a35f56c 2022-07-16 thomas * than a tag.
235 8a35f56c 2022-07-16 thomas error = NULL;
236 8a35f56c 2022-07-16 thomas tag = NULL;
239 8a35f56c 2022-07-16 thomas cmp = got_object_id_cmp(tag ?
240 8a35f56c 2022-07-16 thomas got_object_tag_get_object_id(tag) : ref_id, id);
241 8a35f56c 2022-07-16 thomas free(ref_id);
243 8a35f56c 2022-07-16 thomas got_object_tag_close(tag);
244 8a35f56c 2022-07-16 thomas if (cmp != 0)
245 8a35f56c 2022-07-16 thomas continue;
246 8a35f56c 2022-07-16 thomas s = repo_commit->refs_str;
247 8a35f56c 2022-07-16 thomas if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
248 8a35f56c 2022-07-16 thomas s ? ", " : "", name) == -1) {
249 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
251 8a35f56c 2022-07-16 thomas repo_commit->refs_str = NULL;
252 8a35f56c 2022-07-16 thomas return error;
257 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->commit_id, id);
258 8a35f56c 2022-07-16 thomas if (error)
259 8a35f56c 2022-07-16 thomas return error;
261 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->tree_id,
262 8a35f56c 2022-07-16 thomas got_object_commit_get_tree_id(commit));
263 8a35f56c 2022-07-16 thomas if (error)
264 8a35f56c 2022-07-16 thomas return error;
266 8a35f56c 2022-07-16 thomas if (qs->action == DIFF) {
267 8a35f56c 2022-07-16 thomas parent_id = STAILQ_FIRST(
268 8a35f56c 2022-07-16 thomas got_object_commit_get_parent_ids(commit));
269 8a35f56c 2022-07-16 thomas if (parent_id != NULL) {
270 8a35f56c 2022-07-16 thomas id2 = got_object_id_dup(&parent_id->id);
271 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->parent_id, id2);
272 8a35f56c 2022-07-16 thomas if (error)
273 8a35f56c 2022-07-16 thomas return error;
274 8a35f56c 2022-07-16 thomas free(id2);
276 8a35f56c 2022-07-16 thomas repo_commit->parent_id = strdup("/dev/null");
277 8a35f56c 2022-07-16 thomas if (repo_commit->parent_id == NULL) {
278 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
279 8a35f56c 2022-07-16 thomas return error;
284 8a35f56c 2022-07-16 thomas repo_commit->committer_time =
285 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
287 8a35f56c 2022-07-16 thomas repo_commit->author =
288 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_author(commit));
289 8a35f56c 2022-07-16 thomas if (repo_commit->author == NULL) {
290 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
291 8a35f56c 2022-07-16 thomas return error;
293 8a35f56c 2022-07-16 thomas repo_commit->committer =
294 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_committer(commit));
295 8a35f56c 2022-07-16 thomas if (repo_commit->committer == NULL) {
296 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
297 8a35f56c 2022-07-16 thomas return error;
299 8a35f56c 2022-07-16 thomas error = got_object_commit_get_logmsg(&commit_msg0, commit);
300 8a35f56c 2022-07-16 thomas if (error)
301 8a35f56c 2022-07-16 thomas return error;
303 8a35f56c 2022-07-16 thomas commit_msg = commit_msg0;
304 8a35f56c 2022-07-16 thomas while (*commit_msg == '\n')
305 8a35f56c 2022-07-16 thomas commit_msg++;
307 8a35f56c 2022-07-16 thomas repo_commit->commit_msg = strdup(commit_msg);
308 8a35f56c 2022-07-16 thomas if (repo_commit->commit_msg == NULL)
309 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
310 8a35f56c 2022-07-16 thomas free(commit_msg0);
311 8a35f56c 2022-07-16 thomas return error;
314 8a35f56c 2022-07-16 thomas const struct got_error *
315 8a35f56c 2022-07-16 thomas got_get_repo_commits(struct request *c, int limit)
317 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
318 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
319 8a35f56c 2022-07-16 thomas struct got_commit_graph *graph = NULL;
320 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
321 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
322 4d0573ec 2022-09-02 thomas struct got_reference *ref = NULL;
323 8a35f56c 2022-07-16 thomas struct repo_commit *repo_commit = NULL;
324 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
325 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
326 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
327 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
328 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
329 8a35f56c 2022-07-16 thomas char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
330 42814e01 2023-01-23 thomas int chk_next = 0, chk_multi = 0;
332 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
334 8a35f56c 2022-07-16 thomas if (qs->file != NULL && strlen(qs->file) > 0)
335 8a35f56c 2022-07-16 thomas if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
336 8a35f56c 2022-07-16 thomas qs->file) == -1)
337 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
339 8a35f56c 2022-07-16 thomas if (asprintf(&repo_path, "%s/%s", srv->repos_path,
340 31797419 2022-09-02 thomas repo_dir->name) == -1) {
341 31797419 2022-09-02 thomas error = got_error_from_errno("asprintf");
342 31797419 2022-09-02 thomas goto done;
345 42814e01 2023-01-23 thomas if (qs->commit) {
346 42814e01 2023-01-23 thomas error = got_repo_match_object_id_prefix(&id, qs->commit,
347 42814e01 2023-01-23 thomas GOT_OBJ_TYPE_COMMIT, repo);
348 42814e01 2023-01-23 thomas if (error)
349 42814e01 2023-01-23 thomas goto done;
351 8a35f56c 2022-07-16 thomas error = got_ref_open(&ref, repo, qs->headref, 0);
352 8a35f56c 2022-07-16 thomas if (error)
353 8a35f56c 2022-07-16 thomas goto done;
355 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, ref);
356 8a35f56c 2022-07-16 thomas if (error)
357 8a35f56c 2022-07-16 thomas goto done;
360 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, repo_path);
361 8a35f56c 2022-07-16 thomas if (error)
362 8a35f56c 2022-07-16 thomas goto done;
364 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
365 8a35f56c 2022-07-16 thomas if (error)
366 8a35f56c 2022-07-16 thomas goto done;
368 8a35f56c 2022-07-16 thomas if (qs->file != NULL && strlen(qs->file) > 0) {
369 8a35f56c 2022-07-16 thomas error = got_commit_graph_open(&graph, file_path, 0);
370 8a35f56c 2022-07-16 thomas if (error)
371 8a35f56c 2022-07-16 thomas goto done;
373 8a35f56c 2022-07-16 thomas error = got_commit_graph_open(&graph, in_repo_path, 0);
374 8a35f56c 2022-07-16 thomas if (error)
375 8a35f56c 2022-07-16 thomas goto done;
378 8a35f56c 2022-07-16 thomas error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
379 8a35f56c 2022-07-16 thomas if (error)
380 8a35f56c 2022-07-16 thomas goto done;
382 8a35f56c 2022-07-16 thomas for (;;) {
383 7210b715 2022-09-11 thomas struct got_object_id next_id;
385 95326260 2022-09-05 thomas error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
387 8a35f56c 2022-07-16 thomas if (error) {
388 8a35f56c 2022-07-16 thomas if (error->code == GOT_ERR_ITER_COMPLETED)
389 8a35f56c 2022-07-16 thomas error = NULL;
390 8a35f56c 2022-07-16 thomas goto done;
393 7210b715 2022-09-11 thomas error = got_object_open_as_commit(&commit, repo, &next_id);
394 8a35f56c 2022-07-16 thomas if (error)
395 8a35f56c 2022-07-16 thomas goto done;
397 df9fed09 2022-09-02 thomas error = got_init_repo_commit(&repo_commit);
398 df9fed09 2022-09-02 thomas if (error)
399 df9fed09 2022-09-02 thomas goto done;
401 8a35f56c 2022-07-16 thomas error = got_get_repo_commit(c, repo_commit, commit,
402 7210b715 2022-09-11 thomas &refs, &next_id);
403 349f6f39 2022-09-07 thomas if (error) {
404 349f6f39 2022-09-07 thomas gotweb_free_repo_commit(repo_commit);
405 8a35f56c 2022-07-16 thomas goto done;
408 349f6f39 2022-09-07 thomas TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
410 42814e01 2023-01-23 thomas if (!chk_multi || limit != 1 ||
411 42814e01 2023-01-23 thomas srv->max_commits_display == 1) {
412 8a35f56c 2022-07-16 thomas chk_multi = 1;
415 8a35f56c 2022-07-16 thomas * check for one more commit before breaking,
416 8a35f56c 2022-07-16 thomas * so we know whether to navigate through briefs
417 8a35f56c 2022-07-16 thomas * commits and summary
419 8a35f56c 2022-07-16 thomas if (chk_next && (qs->action == BRIEFS ||
420 8a35f56c 2022-07-16 thomas qs->action == COMMITS || qs->action == SUMMARY)) {
421 df9fed09 2022-09-02 thomas t->next_id = strdup(repo_commit->commit_id);
422 8a35f56c 2022-07-16 thomas if (t->next_id == NULL) {
423 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
424 8a35f56c 2022-07-16 thomas goto done;
426 8a35f56c 2022-07-16 thomas if (commit) {
427 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
428 8a35f56c 2022-07-16 thomas commit = NULL;
430 df9fed09 2022-09-02 thomas TAILQ_REMOVE(&t->repo_commits, repo_commit,
432 df9fed09 2022-09-02 thomas gotweb_free_repo_commit(repo_commit);
433 8a35f56c 2022-07-16 thomas goto done;
436 8a35f56c 2022-07-16 thomas if (error || (limit && --limit == 0)) {
437 42814e01 2023-01-23 thomas if (qs->file != NULL && *qs->file != '\0')
438 8a35f56c 2022-07-16 thomas if (chk_multi == 0)
440 8a35f56c 2022-07-16 thomas chk_next = 1;
442 8a35f56c 2022-07-16 thomas if (commit) {
443 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
444 8a35f56c 2022-07-16 thomas commit = NULL;
449 4d0573ec 2022-09-02 thomas got_ref_close(ref);
450 8a35f56c 2022-07-16 thomas if (commit)
451 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
452 8a35f56c 2022-07-16 thomas if (graph)
453 8a35f56c 2022-07-16 thomas got_commit_graph_close(graph);
454 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
455 bc4d9cc8 2022-09-02 thomas free(in_repo_path);
456 8a35f56c 2022-07-16 thomas free(file_path);
457 8a35f56c 2022-07-16 thomas free(repo_path);
458 8a35f56c 2022-07-16 thomas free(id);
459 8a35f56c 2022-07-16 thomas return error;
462 8a35f56c 2022-07-16 thomas const struct got_error *
463 8a35f56c 2022-07-16 thomas got_get_repo_tags(struct request *c, int limit)
465 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
466 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
467 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
468 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
469 8a35f56c 2022-07-16 thomas struct got_reference *ref;
470 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
471 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
472 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
473 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
474 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
475 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
476 8a35f56c 2022-07-16 thomas struct got_tag_object *tag = NULL;
477 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
478 8a35f56c 2022-07-16 thomas char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
479 217813df 2022-09-02 thomas char *tag_commit = NULL, *tag_commit0 = NULL;
480 8a35f56c 2022-07-16 thomas char *commit_msg = NULL, *commit_msg0 = NULL;
481 8a35f56c 2022-07-16 thomas int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
483 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
485 8a35f56c 2022-07-16 thomas if (asprintf(&repo_path, "%s/%s", srv->repos_path,
486 8a35f56c 2022-07-16 thomas repo_dir->name) == -1)
487 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
489 d6795e9f 2022-12-30 thomas if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
490 8a35f56c 2022-07-16 thomas error = got_ref_open(&ref, repo, qs->headref, 0);
491 8a35f56c 2022-07-16 thomas if (error)
492 8a35f56c 2022-07-16 thomas goto err;
493 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, ref);
494 8a35f56c 2022-07-16 thomas got_ref_close(ref);
495 8a35f56c 2022-07-16 thomas if (error)
496 8a35f56c 2022-07-16 thomas goto err;
497 8a35f56c 2022-07-16 thomas } else if (qs->commit == NULL && qs->action == TAG) {
498 8a35f56c 2022-07-16 thomas error = got_error_msg(GOT_ERR_EOF, "commit id missing");
499 8a35f56c 2022-07-16 thomas goto err;
501 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id_prefix(&id, qs->commit,
502 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, repo);
503 8a35f56c 2022-07-16 thomas if (error)
504 8a35f56c 2022-07-16 thomas goto err;
507 8a35f56c 2022-07-16 thomas if (qs->action != SUMMARY && qs->action != TAGS) {
508 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
509 8a35f56c 2022-07-16 thomas if (error)
510 8a35f56c 2022-07-16 thomas goto err;
511 8a35f56c 2022-07-16 thomas error = got_object_commit_get_logmsg(&commit_msg0, commit);
512 8a35f56c 2022-07-16 thomas if (error)
513 8a35f56c 2022-07-16 thomas goto err;
514 8a35f56c 2022-07-16 thomas if (commit) {
515 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
516 8a35f56c 2022-07-16 thomas commit = NULL;
520 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, repo_path);
521 8a35f56c 2022-07-16 thomas if (error)
522 8a35f56c 2022-07-16 thomas goto err;
524 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
526 8a35f56c 2022-07-16 thomas if (error)
527 8a35f56c 2022-07-16 thomas goto err;
529 8a35f56c 2022-07-16 thomas if (limit == 1)
530 8a35f56c 2022-07-16 thomas chk_multi = 0;
533 8a35f56c 2022-07-16 thomas * XXX: again, see previous message about caching
536 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
537 8a35f56c 2022-07-16 thomas struct repo_tag *new_repo_tag = NULL;
538 8a35f56c 2022-07-16 thomas error = got_init_repo_tag(&new_repo_tag);
539 8a35f56c 2022-07-16 thomas if (error)
540 8a35f56c 2022-07-16 thomas goto err;
542 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
544 8a35f56c 2022-07-16 thomas new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
545 8a35f56c 2022-07-16 thomas if (new_repo_tag->tag_name == NULL) {
546 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
547 8a35f56c 2022-07-16 thomas goto err;
550 19d60ee3 2022-09-02 thomas free(id);
551 19d60ee3 2022-09-02 thomas id = NULL;
553 19d60ee3 2022-09-02 thomas free(id_str);
554 19d60ee3 2022-09-02 thomas id_str = NULL;
556 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, re->ref);
557 8a35f56c 2022-07-16 thomas if (error)
558 8a35f56c 2022-07-16 thomas goto done;
561 acd4ff87 2022-09-02 thomas got_object_tag_close(tag);
562 8a35f56c 2022-07-16 thomas error = got_object_open_as_tag(&tag, repo, id);
563 8a35f56c 2022-07-16 thomas if (error) {
564 19d60ee3 2022-09-02 thomas if (error->code != GOT_ERR_OBJ_TYPE)
565 8a35f56c 2022-07-16 thomas goto done;
566 8a35f56c 2022-07-16 thomas /* "lightweight" tag */
567 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
568 19d60ee3 2022-09-02 thomas if (error)
569 8a35f56c 2022-07-16 thomas goto done;
570 8a35f56c 2022-07-16 thomas new_repo_tag->tagger =
571 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_committer(commit));
572 8a35f56c 2022-07-16 thomas if (new_repo_tag->tagger == NULL) {
573 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
574 8a35f56c 2022-07-16 thomas goto err;
576 8a35f56c 2022-07-16 thomas new_repo_tag->tagger_time =
577 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
578 8a35f56c 2022-07-16 thomas error = got_object_id_str(&id_str, id);
579 8a35f56c 2022-07-16 thomas if (error)
580 8a35f56c 2022-07-16 thomas goto err;
582 8a35f56c 2022-07-16 thomas new_repo_tag->tagger =
583 8a35f56c 2022-07-16 thomas strdup(got_object_tag_get_tagger(tag));
584 8a35f56c 2022-07-16 thomas if (new_repo_tag->tagger == NULL) {
585 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
586 8a35f56c 2022-07-16 thomas goto err;
588 8a35f56c 2022-07-16 thomas new_repo_tag->tagger_time =
589 8a35f56c 2022-07-16 thomas got_object_tag_get_tagger_time(tag);
590 8a35f56c 2022-07-16 thomas error = got_object_id_str(&id_str,
591 8a35f56c 2022-07-16 thomas got_object_tag_get_object_id(tag));
592 8a35f56c 2022-07-16 thomas if (error)
593 8a35f56c 2022-07-16 thomas goto err;
596 8a35f56c 2022-07-16 thomas new_repo_tag->commit_id = strdup(id_str);
597 8a35f56c 2022-07-16 thomas if (new_repo_tag->commit_id == NULL)
598 8a35f56c 2022-07-16 thomas goto err;
600 8a35f56c 2022-07-16 thomas if (commit_found == 0 && qs->commit != NULL &&
601 8a35f56c 2022-07-16 thomas strncmp(id_str, qs->commit, strlen(id_str)) != 0)
602 8a35f56c 2022-07-16 thomas continue;
604 8a35f56c 2022-07-16 thomas commit_found = 1;
606 8a35f56c 2022-07-16 thomas t->tag_count++;
609 8a35f56c 2022-07-16 thomas * check for one more commit before breaking,
610 8a35f56c 2022-07-16 thomas * so we know whether to navigate through briefs
611 8a35f56c 2022-07-16 thomas * commits and summary
613 8a35f56c 2022-07-16 thomas if (chk_next) {
614 8a35f56c 2022-07-16 thomas t->next_id = strdup(new_repo_tag->commit_id);
615 8a35f56c 2022-07-16 thomas if (t->next_id == NULL) {
616 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
617 8a35f56c 2022-07-16 thomas goto err;
619 8a35f56c 2022-07-16 thomas if (commit) {
620 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
621 8a35f56c 2022-07-16 thomas commit = NULL;
623 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
624 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(new_repo_tag);
625 8a35f56c 2022-07-16 thomas goto done;
628 8a35f56c 2022-07-16 thomas if (commit) {
629 217813df 2022-09-02 thomas error = got_object_commit_get_logmsg(&tag_commit0,
631 8a35f56c 2022-07-16 thomas if (error)
632 217813df 2022-09-02 thomas goto err;
633 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
634 8a35f56c 2022-07-16 thomas commit = NULL;
636 217813df 2022-09-02 thomas tag_commit0 = strdup(got_object_tag_get_message(tag));
637 217813df 2022-09-02 thomas if (tag_commit0 == NULL) {
638 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
639 217813df 2022-09-02 thomas goto err;
643 217813df 2022-09-02 thomas tag_commit = tag_commit0;
644 217813df 2022-09-02 thomas while (*tag_commit == '\n')
645 217813df 2022-09-02 thomas tag_commit++;
646 217813df 2022-09-02 thomas new_repo_tag->tag_commit = strdup(tag_commit);
647 217813df 2022-09-02 thomas if (new_repo_tag->tag_commit == NULL) {
648 217813df 2022-09-02 thomas error = got_error_from_errno("strdup");
649 217813df 2022-09-02 thomas free(tag_commit0);
650 217813df 2022-09-02 thomas goto err;
652 217813df 2022-09-02 thomas free(tag_commit0);
654 8a35f56c 2022-07-16 thomas if (qs->action != SUMMARY && qs->action != TAGS) {
655 8a35f56c 2022-07-16 thomas commit_msg = commit_msg0;
656 8a35f56c 2022-07-16 thomas while (*commit_msg == '\n')
657 8a35f56c 2022-07-16 thomas commit_msg++;
659 8a35f56c 2022-07-16 thomas new_repo_tag->commit_msg = strdup(commit_msg);
660 8a35f56c 2022-07-16 thomas if (new_repo_tag->commit_msg == NULL) {
661 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
662 8a35f56c 2022-07-16 thomas goto err;
666 8a35f56c 2022-07-16 thomas if (limit && --limit == 0) {
667 8a35f56c 2022-07-16 thomas if (chk_multi == 0)
669 8a35f56c 2022-07-16 thomas chk_next = 1;
675 8a35f56c 2022-07-16 thomas * we have tailq populated, so find previous commit id
676 8a35f56c 2022-07-16 thomas * for navigation through briefs and commits
678 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
679 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
680 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
681 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
684 8a35f56c 2022-07-16 thomas if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
685 8a35f56c 2022-07-16 thomas commit_found = 0;
686 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
688 8a35f56c 2022-07-16 thomas if (commit_found == 0 && rt->commit_id != NULL &&
689 8a35f56c 2022-07-16 thomas strcmp(qs->commit, rt->commit_id) != 0) {
690 8a35f56c 2022-07-16 thomas continue;
692 8a35f56c 2022-07-16 thomas commit_found = 1;
693 8a35f56c 2022-07-16 thomas if (c_cnt == srv->max_commits_display ||
694 8a35f56c 2022-07-16 thomas rt == TAILQ_FIRST(&t->repo_tags)) {
695 8a35f56c 2022-07-16 thomas t->prev_id = strdup(rt->commit_id);
696 8a35f56c 2022-07-16 thomas if (t->prev_id == NULL)
697 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
704 8a35f56c 2022-07-16 thomas if (commit)
705 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
707 19d60ee3 2022-09-02 thomas got_object_tag_close(tag);
708 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
709 ddcf1d33 2022-09-02 thomas free(commit_msg0);
710 19d60ee3 2022-09-02 thomas free(in_repo_path);
711 8a35f56c 2022-07-16 thomas free(repo_path);
712 8a35f56c 2022-07-16 thomas free(id);
713 19d60ee3 2022-09-02 thomas free(id_str);
714 8a35f56c 2022-07-16 thomas return error;
718 3c14c1f2 2023-01-06 thomas got_output_repo_tree(struct request *c,
719 3c14c1f2 2023-01-06 thomas int (*cb)(struct template *, struct got_tree_entry *))
721 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
722 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
723 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
724 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
725 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
726 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
727 8a35f56c 2022-07-16 thomas struct got_object_id *tree_id = NULL, *commit_id = NULL;
728 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
729 8a35f56c 2022-07-16 thomas struct got_tree_object *tree = NULL;
730 3c14c1f2 2023-01-06 thomas struct got_tree_entry *te;
731 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
732 06325330 2022-09-02 thomas char *escaped_name = NULL, *path = NULL;
733 3c14c1f2 2023-01-06 thomas int nentries, i;
735 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
737 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
739 8a35f56c 2022-07-16 thomas if (qs->folder != NULL) {
740 8a35f56c 2022-07-16 thomas path = strdup(qs->folder);
741 8a35f56c 2022-07-16 thomas if (path == NULL) {
742 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
743 8a35f56c 2022-07-16 thomas goto done;
746 557d32ee 2022-09-02 thomas error = got_repo_map_path(&path, repo, repo_dir->path);
747 8a35f56c 2022-07-16 thomas if (error)
748 8a35f56c 2022-07-16 thomas goto done;
751 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
752 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
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_commit(&commit, repo, commit_id);
757 8a35f56c 2022-07-16 thomas if (error)
758 8a35f56c 2022-07-16 thomas goto done;
760 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&tree_id, repo, commit, path);
761 8a35f56c 2022-07-16 thomas if (error)
762 8a35f56c 2022-07-16 thomas goto done;
764 8a35f56c 2022-07-16 thomas error = got_object_open_as_tree(&tree, repo, tree_id);
765 8a35f56c 2022-07-16 thomas if (error)
766 8a35f56c 2022-07-16 thomas goto done;
768 8a35f56c 2022-07-16 thomas nentries = got_object_tree_get_nentries(tree);
770 8a35f56c 2022-07-16 thomas for (i = 0; i < nentries; i++) {
771 8a35f56c 2022-07-16 thomas te = got_object_tree_get_entry(tree, i);
772 3c14c1f2 2023-01-06 thomas if (cb(c->tp, te) == -1)
776 df610f47 2022-08-27 thomas free(escaped_name);
777 8a35f56c 2022-07-16 thomas free(path);
778 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
779 8a35f56c 2022-07-16 thomas if (commit)
780 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
781 12e242cc 2022-09-06 thomas if (tree)
782 12e242cc 2022-09-06 thomas got_object_tree_close(tree);
783 8a35f56c 2022-07-16 thomas free(commit_id);
784 8a35f56c 2022-07-16 thomas free(tree_id);
785 3c14c1f2 2023-01-06 thomas if (error) {
786 3c14c1f2 2023-01-06 thomas log_warnx("%s: %s", __func__, error->msg);
787 3c14c1f2 2023-01-06 thomas return -1;
789 3c14c1f2 2023-01-06 thomas return 0;
792 8a35f56c 2022-07-16 thomas const struct got_error *
793 b82440e1 2023-01-06 thomas got_open_blob_for_output(struct got_blob_object **blob, int *fd,
794 b82440e1 2023-01-06 thomas int *binary, struct request *c)
796 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
797 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
798 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
799 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
800 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
801 8a35f56c 2022-07-16 thomas struct got_object_id *commit_id = NULL;
802 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
803 8a35f56c 2022-07-16 thomas char *path = NULL, *in_repo_path = NULL;
804 b82440e1 2023-01-06 thomas int obj_type;
806 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
808 b82440e1 2023-01-06 thomas *blob = NULL;
809 b82440e1 2023-01-06 thomas *fd = -1;
810 b82440e1 2023-01-06 thomas *binary = 0;
812 8a35f56c 2022-07-16 thomas if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
813 8a35f56c 2022-07-16 thomas qs->folder ? "/" : "", qs->file) == -1) {
814 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
815 8a35f56c 2022-07-16 thomas goto done;
818 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, path);
819 8a35f56c 2022-07-16 thomas if (error)
820 8a35f56c 2022-07-16 thomas goto done;
822 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
823 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
824 8a35f56c 2022-07-16 thomas if (error)
825 8a35f56c 2022-07-16 thomas goto done;
827 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
828 8a35f56c 2022-07-16 thomas if (error)
829 8a35f56c 2022-07-16 thomas goto done;
831 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
832 8a35f56c 2022-07-16 thomas if (error)
833 8a35f56c 2022-07-16 thomas goto done;
835 8a35f56c 2022-07-16 thomas if (commit_id == NULL) {
836 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_NO_OBJ);
837 8a35f56c 2022-07-16 thomas goto done;
840 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, commit_id);
841 8a35f56c 2022-07-16 thomas if (error)
842 8a35f56c 2022-07-16 thomas goto done;
844 8a35f56c 2022-07-16 thomas if (obj_type != GOT_OBJ_TYPE_BLOB) {
845 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
846 8a35f56c 2022-07-16 thomas goto done;
849 b82440e1 2023-01-06 thomas error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
850 8a35f56c 2022-07-16 thomas if (error)
851 8a35f56c 2022-07-16 thomas goto done;
853 b82440e1 2023-01-06 thomas error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
854 8a35f56c 2022-07-16 thomas if (error)
855 8a35f56c 2022-07-16 thomas goto done;
857 b82440e1 2023-01-06 thomas error = got_object_blob_is_binary(binary, *blob);
858 efe3fd03 2023-01-06 thomas if (error)
859 efe3fd03 2023-01-06 thomas goto done;
862 b82440e1 2023-01-06 thomas if (commit)
863 b82440e1 2023-01-06 thomas got_object_commit_close(commit);
865 b82440e1 2023-01-06 thomas if (error) {
866 b82440e1 2023-01-06 thomas if (*fd != -1)
867 b82440e1 2023-01-06 thomas close(*fd);
868 b82440e1 2023-01-06 thomas if (*blob)
869 b82440e1 2023-01-06 thomas got_object_blob_close(*blob);
870 b82440e1 2023-01-06 thomas *fd = -1;
871 b82440e1 2023-01-06 thomas *blob = NULL;
874 b82440e1 2023-01-06 thomas free(in_repo_path);
875 b82440e1 2023-01-06 thomas free(commit_id);
876 b82440e1 2023-01-06 thomas free(path);
877 8a35f56c 2022-07-16 thomas return error;
881 b82440e1 2023-01-06 thomas got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
882 b82440e1 2023-01-06 thomas int (*cb)(struct template *, const char *, size_t))
884 b82440e1 2023-01-06 thomas const struct got_error *err;
885 b82440e1 2023-01-06 thomas char *line = NULL;
886 b82440e1 2023-01-06 thomas size_t linesize = 0;
887 b82440e1 2023-01-06 thomas size_t lineno = 0;
888 b82440e1 2023-01-06 thomas ssize_t linelen = 0;
890 b82440e1 2023-01-06 thomas for (;;) {
891 b82440e1 2023-01-06 thomas err = got_object_blob_getline(&line, &linelen, &linesize,
893 b82440e1 2023-01-06 thomas if (err || linelen == -1)
895 b82440e1 2023-01-06 thomas lineno++;
896 b82440e1 2023-01-06 thomas if (cb(tp, line, lineno) == -1)
900 b82440e1 2023-01-06 thomas free(line);
902 b82440e1 2023-01-06 thomas if (err) {
903 b82440e1 2023-01-06 thomas log_warnx("%s: got_object_blob_getline failed: %s",
904 b82440e1 2023-01-06 thomas __func__, err->msg);
905 b82440e1 2023-01-06 thomas return -1;
907 b82440e1 2023-01-06 thomas return 0;
910 8a35f56c 2022-07-16 thomas struct blame_cb_args {
911 8a35f56c 2022-07-16 thomas struct blame_line *lines;
912 8a35f56c 2022-07-16 thomas int nlines;
913 8a35f56c 2022-07-16 thomas int nlines_prec;
914 8a35f56c 2022-07-16 thomas int lineno_cur;
915 8a35f56c 2022-07-16 thomas off_t *line_offsets;
917 8a35f56c 2022-07-16 thomas struct got_repository *repo;
918 8a35f56c 2022-07-16 thomas struct request *c;
919 1cd5d437 2023-01-15 thomas got_render_blame_line_cb cb;
922 8a35f56c 2022-07-16 thomas static const struct got_error *
923 8a35f56c 2022-07-16 thomas got_gotweb_blame_cb(void *arg, int nlines, int lineno,
924 8a35f56c 2022-07-16 thomas struct got_commit_object *commit, struct got_object_id *id)
926 8a35f56c 2022-07-16 thomas const struct got_error *err = NULL;
927 8a35f56c 2022-07-16 thomas struct blame_cb_args *a = arg;
928 8a35f56c 2022-07-16 thomas struct blame_line *bline;
929 8a35f56c 2022-07-16 thomas struct request *c = a->c;
930 1cd5d437 2023-01-15 thomas char *line = NULL;
931 8a35f56c 2022-07-16 thomas size_t linesize = 0;
932 8a35f56c 2022-07-16 thomas off_t offset;
933 8a35f56c 2022-07-16 thomas struct tm tm;
934 8a35f56c 2022-07-16 thomas time_t committer_time;
936 8a35f56c 2022-07-16 thomas if (nlines != a->nlines ||
937 8a35f56c 2022-07-16 thomas (lineno != -1 && lineno < 1) || lineno > a->nlines)
938 8a35f56c 2022-07-16 thomas return got_error(GOT_ERR_RANGE);
940 8a35f56c 2022-07-16 thomas if (lineno == -1)
941 8a35f56c 2022-07-16 thomas return NULL; /* no change in this commit */
943 8a35f56c 2022-07-16 thomas /* Annotate this line. */
944 8a35f56c 2022-07-16 thomas bline = &a->lines[lineno - 1];
945 8a35f56c 2022-07-16 thomas if (bline->annotated)
946 8a35f56c 2022-07-16 thomas return NULL;
947 8a35f56c 2022-07-16 thomas err = got_object_id_str(&bline->id_str, id);
949 8a35f56c 2022-07-16 thomas return err;
951 8a35f56c 2022-07-16 thomas bline->committer = strdup(got_object_commit_get_committer(commit));
952 8a35f56c 2022-07-16 thomas if (bline->committer == NULL) {
953 8a35f56c 2022-07-16 thomas err = got_error_from_errno("strdup");
954 8a35f56c 2022-07-16 thomas goto done;
957 8a35f56c 2022-07-16 thomas committer_time = got_object_commit_get_committer_time(commit);
958 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
959 8a35f56c 2022-07-16 thomas return got_error_from_errno("gmtime_r");
960 8a35f56c 2022-07-16 thomas if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
961 8a35f56c 2022-07-16 thomas &tm) == 0) {
962 8a35f56c 2022-07-16 thomas err = got_error(GOT_ERR_NO_SPACE);
963 8a35f56c 2022-07-16 thomas goto done;
965 8a35f56c 2022-07-16 thomas bline->annotated = 1;
967 8a35f56c 2022-07-16 thomas /* Print lines annotated so far. */
968 8a35f56c 2022-07-16 thomas bline = &a->lines[a->lineno_cur - 1];
969 8a35f56c 2022-07-16 thomas if (!bline->annotated)
970 8a35f56c 2022-07-16 thomas goto done;
972 8a35f56c 2022-07-16 thomas offset = a->line_offsets[a->lineno_cur - 1];
973 8a35f56c 2022-07-16 thomas if (fseeko(a->f, offset, SEEK_SET) == -1) {
974 8a35f56c 2022-07-16 thomas err = got_error_from_errno("fseeko");
975 8a35f56c 2022-07-16 thomas goto done;
978 b5c07627 2022-08-18 thomas while (a->lineno_cur <= a->nlines && bline->annotated) {
979 8a35f56c 2022-07-16 thomas if (getline(&line, &linesize, a->f) == -1) {
980 8a35f56c 2022-07-16 thomas if (ferror(a->f))
981 8a35f56c 2022-07-16 thomas err = got_error_from_errno("getline");
985 1cd5d437 2023-01-15 thomas if (a->cb(c->tp, line, bline, a->nlines_prec,
986 1cd5d437 2023-01-15 thomas a->lineno_cur) == -1)
989 8a35f56c 2022-07-16 thomas a->lineno_cur++;
990 8a35f56c 2022-07-16 thomas bline = &a->lines[a->lineno_cur - 1];
993 8a35f56c 2022-07-16 thomas free(line);
994 8a35f56c 2022-07-16 thomas return err;
997 8a35f56c 2022-07-16 thomas const struct got_error *
998 1cd5d437 2023-01-15 thomas got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1000 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1001 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1002 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1003 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
1004 8a35f56c 2022-07-16 thomas struct got_object_id *obj_id = NULL, *commit_id = NULL;
1005 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
1006 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1007 8a35f56c 2022-07-16 thomas struct got_blob_object *blob = NULL;
1008 8a35f56c 2022-07-16 thomas char *path = NULL, *in_repo_path = NULL;
1009 8a35f56c 2022-07-16 thomas struct blame_cb_args bca;
1010 8a35f56c 2022-07-16 thomas int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1011 8a35f56c 2022-07-16 thomas int fd6 = -1;
1012 8a35f56c 2022-07-16 thomas off_t filesize;
1013 8a35f56c 2022-07-16 thomas FILE *f1 = NULL, *f2 = NULL;
1015 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1016 8a35f56c 2022-07-16 thomas bca.f = NULL;
1017 8a35f56c 2022-07-16 thomas bca.lines = NULL;
1018 1cd5d437 2023-01-15 thomas bca.cb = cb;
1020 8a35f56c 2022-07-16 thomas if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1021 8a35f56c 2022-07-16 thomas qs->folder ? "/" : "", qs->file) == -1) {
1022 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1023 8a35f56c 2022-07-16 thomas goto done;
1026 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, path);
1027 8a35f56c 2022-07-16 thomas if (error)
1028 8a35f56c 2022-07-16 thomas goto done;
1030 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1031 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
1032 8a35f56c 2022-07-16 thomas if (error)
1033 8a35f56c 2022-07-16 thomas goto done;
1035 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
1036 8a35f56c 2022-07-16 thomas if (error)
1037 8a35f56c 2022-07-16 thomas goto done;
1039 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1040 8a35f56c 2022-07-16 thomas if (error)
1041 8a35f56c 2022-07-16 thomas goto done;
1043 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, obj_id);
1044 8a35f56c 2022-07-16 thomas if (error)
1045 8a35f56c 2022-07-16 thomas goto done;
1047 8a35f56c 2022-07-16 thomas if (obj_type != GOT_OBJ_TYPE_BLOB) {
1048 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
1049 8a35f56c 2022-07-16 thomas goto done;
1052 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1053 8a35f56c 2022-07-16 thomas if (error)
1054 8a35f56c 2022-07-16 thomas goto done;
1056 8a35f56c 2022-07-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1057 8a35f56c 2022-07-16 thomas if (error)
1058 8a35f56c 2022-07-16 thomas goto done;
1060 8a35f56c 2022-07-16 thomas error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1061 8a35f56c 2022-07-16 thomas if (error)
1062 8a35f56c 2022-07-16 thomas goto done;
1064 8a35f56c 2022-07-16 thomas error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1065 8a35f56c 2022-07-16 thomas &bca.line_offsets, bca.f, blob);
1066 8a35f56c 2022-07-16 thomas if (error || bca.nlines == 0)
1067 8a35f56c 2022-07-16 thomas goto done;
1069 8a35f56c 2022-07-16 thomas /* Don't include \n at EOF in the blame line count. */
1070 8a35f56c 2022-07-16 thomas if (bca.line_offsets[bca.nlines - 1] == filesize)
1071 8a35f56c 2022-07-16 thomas bca.nlines--;
1073 8a35f56c 2022-07-16 thomas bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1074 8a35f56c 2022-07-16 thomas if (bca.lines == NULL) {
1075 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1076 8a35f56c 2022-07-16 thomas goto done;
1078 8a35f56c 2022-07-16 thomas bca.lineno_cur = 1;
1079 8a35f56c 2022-07-16 thomas bca.nlines_prec = 0;
1080 8a35f56c 2022-07-16 thomas i = bca.nlines;
1081 8a35f56c 2022-07-16 thomas while (i > 0) {
1082 8a35f56c 2022-07-16 thomas i /= 10;
1083 8a35f56c 2022-07-16 thomas bca.nlines_prec++;
1085 8a35f56c 2022-07-16 thomas bca.repo = repo;
1086 8a35f56c 2022-07-16 thomas bca.c = c;
1088 8a35f56c 2022-07-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1089 8a35f56c 2022-07-16 thomas if (error)
1090 8a35f56c 2022-07-16 thomas goto done;
1092 8a35f56c 2022-07-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1093 8a35f56c 2022-07-16 thomas if (error)
1094 8a35f56c 2022-07-16 thomas goto done;
1096 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1097 8a35f56c 2022-07-16 thomas if (error)
1098 8a35f56c 2022-07-16 thomas goto done;
1100 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1101 8a35f56c 2022-07-16 thomas if (error)
1102 8a35f56c 2022-07-16 thomas goto done;
1104 8a35f56c 2022-07-16 thomas error = got_blame(in_repo_path, commit_id, repo,
1105 8a35f56c 2022-07-16 thomas GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1106 8a35f56c 2022-07-16 thomas fd3, fd4, f1, f2);
1109 d38635a3 2022-09-03 thomas if (bca.lines) {
1110 8a35f56c 2022-07-16 thomas free(bca.line_offsets);
1111 8a35f56c 2022-07-16 thomas for (i = 0; i < bca.nlines; i++) {
1112 8a35f56c 2022-07-16 thomas struct blame_line *bline = &bca.lines[i];
1113 8a35f56c 2022-07-16 thomas free(bline->id_str);
1114 8a35f56c 2022-07-16 thomas free(bline->committer);
1117 8a35f56c 2022-07-16 thomas free(bca.lines);
1118 8a35f56c 2022-07-16 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1119 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1120 8a35f56c 2022-07-16 thomas if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1121 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1122 8a35f56c 2022-07-16 thomas if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1123 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1124 8a35f56c 2022-07-16 thomas if (bca.f) {
1125 8a35f56c 2022-07-16 thomas const struct got_error *bca_err =
1126 8a35f56c 2022-07-16 thomas got_gotweb_flushfile(bca.f, fd1);
1127 8a35f56c 2022-07-16 thomas if (error == NULL)
1128 8a35f56c 2022-07-16 thomas error = bca_err;
1130 8a35f56c 2022-07-16 thomas if (f1) {
1131 8a35f56c 2022-07-16 thomas const struct got_error *f1_err =
1132 8a35f56c 2022-07-16 thomas got_gotweb_flushfile(f1, fd5);
1133 8a35f56c 2022-07-16 thomas if (error == NULL)
1134 8a35f56c 2022-07-16 thomas error = f1_err;
1136 8a35f56c 2022-07-16 thomas if (f2) {
1137 8a35f56c 2022-07-16 thomas const struct got_error *f2_err =
1138 8a35f56c 2022-07-16 thomas got_gotweb_flushfile(f2, fd6);
1139 8a35f56c 2022-07-16 thomas if (error == NULL)
1140 8a35f56c 2022-07-16 thomas error = f2_err;
1142 8a35f56c 2022-07-16 thomas if (commit)
1143 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
1144 8a35f56c 2022-07-16 thomas if (blob)
1145 8a35f56c 2022-07-16 thomas got_object_blob_close(blob);
1146 8a35f56c 2022-07-16 thomas free(in_repo_path);
1147 8a35f56c 2022-07-16 thomas free(commit_id);
1148 aefd24c9 2022-09-03 thomas free(obj_id);
1149 8a35f56c 2022-07-16 thomas free(path);
1150 aefd24c9 2022-09-03 thomas got_ref_list_free(&refs);
1151 8a35f56c 2022-07-16 thomas return error;
1154 8a35f56c 2022-07-16 thomas const struct got_error *
1155 dccd05b4 2023-01-10 thomas got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1157 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1158 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1159 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1160 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1161 8a35f56c 2022-07-16 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
1162 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1163 8a35f56c 2022-07-16 thomas FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1164 8a35f56c 2022-07-16 thomas int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1166 dccd05b4 2023-01-10 thomas *fp = NULL;
1167 dccd05b4 2023-01-10 thomas *fd = -1;
1169 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1171 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1172 8a35f56c 2022-07-16 thomas if (error)
1173 8a35f56c 2022-07-16 thomas return error;
1175 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1176 8a35f56c 2022-07-16 thomas if (error)
1177 8a35f56c 2022-07-16 thomas return error;
1179 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1180 8a35f56c 2022-07-16 thomas if (error)
1181 8a35f56c 2022-07-16 thomas return error;
1183 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1185 8a35f56c 2022-07-16 thomas if (rc->parent_id != NULL &&
1186 8a35f56c 2022-07-16 thomas strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1187 dccd05b4 2023-01-10 thomas error = got_repo_match_object_id(&id1, NULL,
1188 8a35f56c 2022-07-16 thomas rc->parent_id, GOT_OBJ_TYPE_ANY,
1189 8a35f56c 2022-07-16 thomas &refs, repo);
1190 8a35f56c 2022-07-16 thomas if (error)
1191 8a35f56c 2022-07-16 thomas goto done;
1194 dccd05b4 2023-01-10 thomas error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1195 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_ANY, &refs, repo);
1196 8a35f56c 2022-07-16 thomas if (error)
1197 8a35f56c 2022-07-16 thomas goto done;
1199 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, id2);
1200 8a35f56c 2022-07-16 thomas if (error)
1201 8a35f56c 2022-07-16 thomas goto done;
1203 8a35f56c 2022-07-16 thomas error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1204 8a35f56c 2022-07-16 thomas if (error)
1205 8a35f56c 2022-07-16 thomas goto done;
1207 8a35f56c 2022-07-16 thomas error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1208 8a35f56c 2022-07-16 thomas if (error)
1209 8a35f56c 2022-07-16 thomas goto done;
1211 8a35f56c 2022-07-16 thomas switch (obj_type) {
1212 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_BLOB:
1213 8a35f56c 2022-07-16 thomas error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1214 8a35f56c 2022-07-16 thomas id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1215 be97ab03 2023-01-19 thomas NULL, repo, f3);
1217 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_TREE:
1218 8a35f56c 2022-07-16 thomas error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1219 8a35f56c 2022-07-16 thomas id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1220 be97ab03 2023-01-19 thomas NULL, repo, f3);
1222 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_COMMIT:
1223 8a35f56c 2022-07-16 thomas error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1224 8a35f56c 2022-07-16 thomas fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1225 be97ab03 2023-01-19 thomas NULL, repo, f3);
1227 8a35f56c 2022-07-16 thomas default:
1228 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
1230 8a35f56c 2022-07-16 thomas if (error)
1231 8a35f56c 2022-07-16 thomas goto done;
1233 8a35f56c 2022-07-16 thomas if (fseek(f1, 0, SEEK_SET) == -1) {
1234 8a35f56c 2022-07-16 thomas error = got_ferror(f1, GOT_ERR_IO);
1235 8a35f56c 2022-07-16 thomas goto done;
1238 8a35f56c 2022-07-16 thomas if (fseek(f2, 0, SEEK_SET) == -1) {
1239 8a35f56c 2022-07-16 thomas error = got_ferror(f2, GOT_ERR_IO);
1240 8a35f56c 2022-07-16 thomas goto done;
1243 8a35f56c 2022-07-16 thomas if (fseek(f3, 0, SEEK_SET) == -1) {
1244 8a35f56c 2022-07-16 thomas error = got_ferror(f3, GOT_ERR_IO);
1245 8a35f56c 2022-07-16 thomas goto done;
1248 dccd05b4 2023-01-10 thomas *fp = f3;
1249 dccd05b4 2023-01-10 thomas *fd = fd3;
1252 8a35f56c 2022-07-16 thomas if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1253 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1254 8a35f56c 2022-07-16 thomas if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1255 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1256 8a35f56c 2022-07-16 thomas if (f1) {
1257 8a35f56c 2022-07-16 thomas const struct got_error *f1_err =
1258 8a35f56c 2022-07-16 thomas got_gotweb_flushfile(f1, fd1);
1259 8a35f56c 2022-07-16 thomas if (error == NULL)
1260 8a35f56c 2022-07-16 thomas error = f1_err;
1262 8a35f56c 2022-07-16 thomas if (f2) {
1263 8a35f56c 2022-07-16 thomas const struct got_error *f2_err =
1264 8a35f56c 2022-07-16 thomas got_gotweb_flushfile(f2, fd2);
1265 8a35f56c 2022-07-16 thomas if (error == NULL)
1266 8a35f56c 2022-07-16 thomas error = f2_err;
1268 dccd05b4 2023-01-10 thomas if (error && f3) {
1269 dccd05b4 2023-01-10 thomas got_gotweb_flushfile(f3, fd3);
1270 dccd05b4 2023-01-10 thomas *fp = NULL;
1271 dccd05b4 2023-01-10 thomas *fd = -1;
1273 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
1274 8a35f56c 2022-07-16 thomas free(id1);
1275 8a35f56c 2022-07-16 thomas free(id2);
1276 8a35f56c 2022-07-16 thomas return error;
1279 8a35f56c 2022-07-16 thomas static const struct got_error *
1280 8a35f56c 2022-07-16 thomas got_init_repo_commit(struct repo_commit **rc)
1282 8a35f56c 2022-07-16 thomas *rc = calloc(1, sizeof(**rc));
1283 8a35f56c 2022-07-16 thomas if (*rc == NULL)
1284 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
1286 8a35f56c 2022-07-16 thomas (*rc)->path = NULL;
1287 8a35f56c 2022-07-16 thomas (*rc)->refs_str = NULL;
1288 8a35f56c 2022-07-16 thomas (*rc)->commit_id = NULL;
1289 8a35f56c 2022-07-16 thomas (*rc)->committer = NULL;
1290 8a35f56c 2022-07-16 thomas (*rc)->author = NULL;
1291 8a35f56c 2022-07-16 thomas (*rc)->parent_id = NULL;
1292 8a35f56c 2022-07-16 thomas (*rc)->tree_id = NULL;
1293 8a35f56c 2022-07-16 thomas (*rc)->commit_msg = NULL;
1295 e2af4cd7 2022-08-31 thomas return NULL;
1298 8a35f56c 2022-07-16 thomas static const struct got_error *
1299 8a35f56c 2022-07-16 thomas got_init_repo_tag(struct repo_tag **rt)
1301 8a35f56c 2022-07-16 thomas *rt = calloc(1, sizeof(**rt));
1302 8a35f56c 2022-07-16 thomas if (*rt == NULL)
1303 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
1305 8a35f56c 2022-07-16 thomas (*rt)->commit_id = NULL;
1306 8a35f56c 2022-07-16 thomas (*rt)->tag_name = NULL;
1307 8a35f56c 2022-07-16 thomas (*rt)->tag_commit = NULL;
1308 8a35f56c 2022-07-16 thomas (*rt)->commit_msg = NULL;
1309 8a35f56c 2022-07-16 thomas (*rt)->tagger = NULL;
1311 e2af4cd7 2022-08-31 thomas return NULL;