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 6c7f10f7 2022-11-23 thomas got_get_repo_age(char **repo_age, struct request *c,
130 8a35f56c 2022-07-16 thomas const char *refname, int ref_tm)
132 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
133 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
134 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
135 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
136 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
137 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
138 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
139 8a35f56c 2022-07-16 thomas time_t committer_time = 0, cmp_time = 0;
141 8a35f56c 2022-07-16 thomas *repo_age = NULL;
142 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
144 8a35f56c 2022-07-16 thomas if (srv->show_repo_age == 0)
145 8a35f56c 2022-07-16 thomas return NULL;
147 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/heads",
148 8a35f56c 2022-07-16 thomas got_ref_cmp_by_name, NULL);
149 8a35f56c 2022-07-16 thomas if (error)
150 8a35f56c 2022-07-16 thomas goto done;
153 8a35f56c 2022-07-16 thomas * Find the youngest branch tip in the repository, or the age of
154 8a35f56c 2022-07-16 thomas * the a specific branch tip if a name was provided by the caller.
156 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
157 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
159 8a35f56c 2022-07-16 thomas if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
160 8a35f56c 2022-07-16 thomas continue;
162 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, re->ref);
163 8a35f56c 2022-07-16 thomas if (error)
164 8a35f56c 2022-07-16 thomas goto done;
166 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
167 8a35f56c 2022-07-16 thomas free(id);
168 8a35f56c 2022-07-16 thomas if (error)
169 8a35f56c 2022-07-16 thomas goto done;
171 8a35f56c 2022-07-16 thomas committer_time =
172 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
173 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
174 8a35f56c 2022-07-16 thomas if (cmp_time < committer_time)
175 8a35f56c 2022-07-16 thomas cmp_time = committer_time;
177 8a35f56c 2022-07-16 thomas if (refname)
181 8a35f56c 2022-07-16 thomas if (cmp_time != 0) {
182 8a35f56c 2022-07-16 thomas committer_time = cmp_time;
183 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
186 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
187 8a35f56c 2022-07-16 thomas return error;
190 8a35f56c 2022-07-16 thomas static const struct got_error *
191 8a35f56c 2022-07-16 thomas got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
192 8a35f56c 2022-07-16 thomas struct got_commit_object *commit, struct got_reflist_head *refs,
193 8a35f56c 2022-07-16 thomas struct got_object_id *id)
195 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
196 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
197 8a35f56c 2022-07-16 thomas struct got_object_id *id2 = NULL;
198 8a35f56c 2022-07-16 thomas struct got_object_qid *parent_id;
199 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
200 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
201 8a35f56c 2022-07-16 thomas char *commit_msg = NULL, *commit_msg0;
203 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, refs, entry) {
205 8a35f56c 2022-07-16 thomas const char *name;
206 8a35f56c 2022-07-16 thomas struct got_tag_object *tag = NULL;
207 8a35f56c 2022-07-16 thomas struct got_object_id *ref_id;
210 8a35f56c 2022-07-16 thomas if (got_ref_is_symbolic(re->ref))
211 8a35f56c 2022-07-16 thomas continue;
213 8a35f56c 2022-07-16 thomas name = got_ref_get_name(re->ref);
214 8a35f56c 2022-07-16 thomas if (strncmp(name, "refs/", 5) == 0)
215 8a35f56c 2022-07-16 thomas name += 5;
216 8a35f56c 2022-07-16 thomas if (strncmp(name, "got/", 4) == 0)
217 8a35f56c 2022-07-16 thomas continue;
218 8a35f56c 2022-07-16 thomas if (strncmp(name, "heads/", 6) == 0)
219 8a35f56c 2022-07-16 thomas name += 6;
220 8a35f56c 2022-07-16 thomas if (strncmp(name, "remotes/", 8) == 0) {
221 8a35f56c 2022-07-16 thomas name += 8;
222 6b42af1e 2022-09-02 thomas if (strstr(name, "/" GOT_REF_HEAD) != NULL)
223 8a35f56c 2022-07-16 thomas continue;
225 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&ref_id, t->repo, re->ref);
226 8a35f56c 2022-07-16 thomas if (error)
227 8a35f56c 2022-07-16 thomas return error;
228 8a35f56c 2022-07-16 thomas if (strncmp(name, "tags/", 5) == 0) {
229 8a35f56c 2022-07-16 thomas error = got_object_open_as_tag(&tag, t->repo, ref_id);
230 8a35f56c 2022-07-16 thomas if (error) {
231 8a35f56c 2022-07-16 thomas if (error->code != GOT_ERR_OBJ_TYPE) {
232 8a35f56c 2022-07-16 thomas free(ref_id);
233 8a35f56c 2022-07-16 thomas continue;
236 8a35f56c 2022-07-16 thomas * Ref points at something other
237 8a35f56c 2022-07-16 thomas * than a tag.
239 8a35f56c 2022-07-16 thomas error = NULL;
240 8a35f56c 2022-07-16 thomas tag = NULL;
243 8a35f56c 2022-07-16 thomas cmp = got_object_id_cmp(tag ?
244 8a35f56c 2022-07-16 thomas got_object_tag_get_object_id(tag) : ref_id, id);
245 8a35f56c 2022-07-16 thomas free(ref_id);
247 8a35f56c 2022-07-16 thomas got_object_tag_close(tag);
248 8a35f56c 2022-07-16 thomas if (cmp != 0)
249 8a35f56c 2022-07-16 thomas continue;
250 8a35f56c 2022-07-16 thomas s = repo_commit->refs_str;
251 8a35f56c 2022-07-16 thomas if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
252 8a35f56c 2022-07-16 thomas s ? ", " : "", name) == -1) {
253 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
255 8a35f56c 2022-07-16 thomas repo_commit->refs_str = NULL;
256 8a35f56c 2022-07-16 thomas return error;
261 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->commit_id, id);
262 8a35f56c 2022-07-16 thomas if (error)
263 8a35f56c 2022-07-16 thomas return error;
265 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->tree_id,
266 8a35f56c 2022-07-16 thomas got_object_commit_get_tree_id(commit));
267 8a35f56c 2022-07-16 thomas if (error)
268 8a35f56c 2022-07-16 thomas return error;
270 8a35f56c 2022-07-16 thomas if (qs->action == DIFF) {
271 8a35f56c 2022-07-16 thomas parent_id = STAILQ_FIRST(
272 8a35f56c 2022-07-16 thomas got_object_commit_get_parent_ids(commit));
273 8a35f56c 2022-07-16 thomas if (parent_id != NULL) {
274 8a35f56c 2022-07-16 thomas id2 = got_object_id_dup(&parent_id->id);
275 8a35f56c 2022-07-16 thomas error = got_object_id_str(&repo_commit->parent_id, id2);
276 8a35f56c 2022-07-16 thomas if (error)
277 8a35f56c 2022-07-16 thomas return error;
278 8a35f56c 2022-07-16 thomas free(id2);
280 8a35f56c 2022-07-16 thomas repo_commit->parent_id = strdup("/dev/null");
281 8a35f56c 2022-07-16 thomas if (repo_commit->parent_id == NULL) {
282 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
283 8a35f56c 2022-07-16 thomas return error;
288 8a35f56c 2022-07-16 thomas repo_commit->committer_time =
289 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
291 8a35f56c 2022-07-16 thomas repo_commit->author =
292 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_author(commit));
293 8a35f56c 2022-07-16 thomas if (repo_commit->author == NULL) {
294 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
295 8a35f56c 2022-07-16 thomas return error;
297 8a35f56c 2022-07-16 thomas repo_commit->committer =
298 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_committer(commit));
299 8a35f56c 2022-07-16 thomas if (repo_commit->committer == NULL) {
300 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
301 8a35f56c 2022-07-16 thomas return error;
303 8a35f56c 2022-07-16 thomas error = got_object_commit_get_logmsg(&commit_msg0, commit);
304 8a35f56c 2022-07-16 thomas if (error)
305 8a35f56c 2022-07-16 thomas return error;
307 8a35f56c 2022-07-16 thomas commit_msg = commit_msg0;
308 8a35f56c 2022-07-16 thomas while (*commit_msg == '\n')
309 8a35f56c 2022-07-16 thomas commit_msg++;
311 8a35f56c 2022-07-16 thomas repo_commit->commit_msg = strdup(commit_msg);
312 8a35f56c 2022-07-16 thomas if (repo_commit->commit_msg == NULL)
313 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
314 8a35f56c 2022-07-16 thomas free(commit_msg0);
315 8a35f56c 2022-07-16 thomas return error;
318 8a35f56c 2022-07-16 thomas const struct got_error *
319 8a35f56c 2022-07-16 thomas got_get_repo_commits(struct request *c, int limit)
321 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
322 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
323 8a35f56c 2022-07-16 thomas struct got_commit_graph *graph = NULL;
324 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
325 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
326 4d0573ec 2022-09-02 thomas struct got_reference *ref = NULL;
327 8a35f56c 2022-07-16 thomas struct repo_commit *repo_commit = NULL;
328 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
329 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
330 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
331 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
332 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
333 8a35f56c 2022-07-16 thomas char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
334 8a35f56c 2022-07-16 thomas int chk_next = 0, chk_multi = 0, commit_found = 0;
335 8a35f56c 2022-07-16 thomas int obj_type, limit_chk = 0;
337 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
339 8a35f56c 2022-07-16 thomas if (qs->file != NULL && strlen(qs->file) > 0)
340 8a35f56c 2022-07-16 thomas if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
341 8a35f56c 2022-07-16 thomas qs->file) == -1)
342 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
344 8a35f56c 2022-07-16 thomas if (asprintf(&repo_path, "%s/%s", srv->repos_path,
345 31797419 2022-09-02 thomas repo_dir->name) == -1) {
346 31797419 2022-09-02 thomas error = got_error_from_errno("asprintf");
347 31797419 2022-09-02 thomas goto done;
351 8a35f56c 2022-07-16 thomas * XXX: jumping directly to a commit id via
352 8a35f56c 2022-07-16 thomas * got_repo_match_object_id_prefix significantly improves performance,
353 8a35f56c 2022-07-16 thomas * but does not allow us to create a PREVIOUS button, since commits can
354 8a35f56c 2022-07-16 thomas * only be itereated forward. So, we have to match as we iterate from
355 8a35f56c 2022-07-16 thomas * the headref.
357 8a35f56c 2022-07-16 thomas if (qs->action == BRIEFS || qs->action == COMMITS ||
358 8a35f56c 2022-07-16 thomas (qs->action == TREE && qs->commit == NULL)) {
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;
366 8a35f56c 2022-07-16 thomas } else if (qs->commit != NULL) {
367 8a35f56c 2022-07-16 thomas error = got_ref_open(&ref, repo, qs->commit, 0);
368 8a35f56c 2022-07-16 thomas if (error == NULL) {
369 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, ref);
370 8a35f56c 2022-07-16 thomas if (error)
371 8a35f56c 2022-07-16 thomas goto done;
372 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, id);
373 8a35f56c 2022-07-16 thomas if (error)
374 8a35f56c 2022-07-16 thomas goto done;
375 8a35f56c 2022-07-16 thomas if (obj_type == GOT_OBJ_TYPE_TAG) {
376 8a35f56c 2022-07-16 thomas struct got_tag_object *tag;
377 8a35f56c 2022-07-16 thomas error = got_object_open_as_tag(&tag, repo, id);
378 8a35f56c 2022-07-16 thomas if (error)
379 8a35f56c 2022-07-16 thomas goto done;
380 8a35f56c 2022-07-16 thomas if (got_object_tag_get_object_type(tag) !=
381 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT) {
382 8a35f56c 2022-07-16 thomas got_object_tag_close(tag);
383 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
384 8a35f56c 2022-07-16 thomas goto done;
386 8a35f56c 2022-07-16 thomas free(id);
387 8a35f56c 2022-07-16 thomas id = got_object_id_dup(
388 8a35f56c 2022-07-16 thomas got_object_tag_get_object_id(tag));
389 8a35f56c 2022-07-16 thomas if (id == NULL)
390 8a35f56c 2022-07-16 thomas error = got_error_from_errno(
391 8a35f56c 2022-07-16 thomas "got_object_id_dup");
392 8a35f56c 2022-07-16 thomas got_object_tag_close(tag);
393 8a35f56c 2022-07-16 thomas if (error)
394 8a35f56c 2022-07-16 thomas goto done;
395 8a35f56c 2022-07-16 thomas } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
396 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
397 8a35f56c 2022-07-16 thomas goto done;
400 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id_prefix(&id, qs->commit,
401 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, repo);
402 8a35f56c 2022-07-16 thomas if (error)
403 8a35f56c 2022-07-16 thomas goto done;
406 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, repo_path);
407 8a35f56c 2022-07-16 thomas if (error)
408 8a35f56c 2022-07-16 thomas goto done;
410 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
411 8a35f56c 2022-07-16 thomas if (error)
412 8a35f56c 2022-07-16 thomas goto done;
414 8a35f56c 2022-07-16 thomas if (qs->file != NULL && strlen(qs->file) > 0) {
415 8a35f56c 2022-07-16 thomas error = got_commit_graph_open(&graph, file_path, 0);
416 8a35f56c 2022-07-16 thomas if (error)
417 8a35f56c 2022-07-16 thomas goto done;
419 8a35f56c 2022-07-16 thomas error = got_commit_graph_open(&graph, in_repo_path, 0);
420 8a35f56c 2022-07-16 thomas if (error)
421 8a35f56c 2022-07-16 thomas goto done;
424 8a35f56c 2022-07-16 thomas error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
425 8a35f56c 2022-07-16 thomas if (error)
426 8a35f56c 2022-07-16 thomas goto done;
428 8a35f56c 2022-07-16 thomas for (;;) {
429 7210b715 2022-09-11 thomas struct got_object_id next_id;
431 95326260 2022-09-05 thomas error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
433 8a35f56c 2022-07-16 thomas if (error) {
434 8a35f56c 2022-07-16 thomas if (error->code == GOT_ERR_ITER_COMPLETED)
435 8a35f56c 2022-07-16 thomas error = NULL;
436 8a35f56c 2022-07-16 thomas goto done;
439 7210b715 2022-09-11 thomas error = got_object_open_as_commit(&commit, repo, &next_id);
440 8a35f56c 2022-07-16 thomas if (error)
441 8a35f56c 2022-07-16 thomas goto done;
443 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
445 8a35f56c 2022-07-16 thomas if (error)
446 8a35f56c 2022-07-16 thomas goto done;
448 df9fed09 2022-09-02 thomas error = got_init_repo_commit(&repo_commit);
449 df9fed09 2022-09-02 thomas if (error)
450 df9fed09 2022-09-02 thomas goto done;
452 8a35f56c 2022-07-16 thomas error = got_get_repo_commit(c, repo_commit, commit,
453 7210b715 2022-09-11 thomas &refs, &next_id);
454 349f6f39 2022-09-07 thomas if (error) {
455 349f6f39 2022-09-07 thomas gotweb_free_repo_commit(repo_commit);
456 8a35f56c 2022-07-16 thomas goto done;
459 349f6f39 2022-09-07 thomas if (limit_chk == ((limit * qs->page) - limit) &&
460 349f6f39 2022-09-07 thomas commit_found == 0 && repo_commit->commit_id != NULL) {
461 349f6f39 2022-09-07 thomas t->prev_id = strdup(repo_commit->commit_id);
462 349f6f39 2022-09-07 thomas if (t->prev_id == NULL) {
463 349f6f39 2022-09-07 thomas error = got_error_from_errno("strdup");
464 349f6f39 2022-09-07 thomas gotweb_free_repo_commit(repo_commit);
465 349f6f39 2022-09-07 thomas goto done;
469 8a35f56c 2022-07-16 thomas if (qs->commit != NULL && commit_found == 0 && limit != 1) {
470 8a35f56c 2022-07-16 thomas if (strcmp(qs->commit, repo_commit->commit_id) == 0)
471 8a35f56c 2022-07-16 thomas commit_found = 1;
472 8a35f56c 2022-07-16 thomas else if (qs->file != NULL && strlen(qs->file) > 0 &&
473 8a35f56c 2022-07-16 thomas qs->page == 0)
474 8a35f56c 2022-07-16 thomas commit_found = 1;
476 349f6f39 2022-09-07 thomas gotweb_free_repo_commit(repo_commit);
477 8a35f56c 2022-07-16 thomas limit_chk++;
478 8a35f56c 2022-07-16 thomas continue;
482 349f6f39 2022-09-07 thomas TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
484 8a35f56c 2022-07-16 thomas if (limit == 1 && chk_multi == 0 &&
485 8a35f56c 2022-07-16 thomas srv->max_commits_display != 1)
486 8a35f56c 2022-07-16 thomas commit_found = 1;
488 8a35f56c 2022-07-16 thomas chk_multi = 1;
491 8a35f56c 2022-07-16 thomas * check for one more commit before breaking,
492 8a35f56c 2022-07-16 thomas * so we know whether to navigate through briefs
493 8a35f56c 2022-07-16 thomas * commits and summary
495 8a35f56c 2022-07-16 thomas if (chk_next && (qs->action == BRIEFS ||
496 8a35f56c 2022-07-16 thomas qs->action == COMMITS || qs->action == SUMMARY)) {
497 df9fed09 2022-09-02 thomas t->next_id = strdup(repo_commit->commit_id);
498 8a35f56c 2022-07-16 thomas if (t->next_id == NULL) {
499 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
500 8a35f56c 2022-07-16 thomas goto done;
502 8a35f56c 2022-07-16 thomas if (commit) {
503 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
504 8a35f56c 2022-07-16 thomas commit = NULL;
506 df9fed09 2022-09-02 thomas TAILQ_REMOVE(&t->repo_commits, repo_commit,
508 df9fed09 2022-09-02 thomas gotweb_free_repo_commit(repo_commit);
509 8a35f56c 2022-07-16 thomas goto done;
512 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
513 8a35f56c 2022-07-16 thomas if (error || (limit && --limit == 0)) {
514 8a35f56c 2022-07-16 thomas if (commit_found || (qs->file != NULL &&
515 8a35f56c 2022-07-16 thomas strlen(qs->file) > 0))
516 8a35f56c 2022-07-16 thomas if (chk_multi == 0)
518 8a35f56c 2022-07-16 thomas chk_next = 1;
520 8a35f56c 2022-07-16 thomas if (commit) {
521 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
522 8a35f56c 2022-07-16 thomas commit = NULL;
527 4d0573ec 2022-09-02 thomas got_ref_close(ref);
528 8a35f56c 2022-07-16 thomas if (commit)
529 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
530 8a35f56c 2022-07-16 thomas if (graph)
531 8a35f56c 2022-07-16 thomas got_commit_graph_close(graph);
532 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
533 bc4d9cc8 2022-09-02 thomas free(in_repo_path);
534 8a35f56c 2022-07-16 thomas free(file_path);
535 8a35f56c 2022-07-16 thomas free(repo_path);
536 8a35f56c 2022-07-16 thomas free(id);
537 8a35f56c 2022-07-16 thomas return error;
540 8a35f56c 2022-07-16 thomas const struct got_error *
541 8a35f56c 2022-07-16 thomas got_get_repo_tags(struct request *c, int limit)
543 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
544 8a35f56c 2022-07-16 thomas struct got_object_id *id = NULL;
545 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
546 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
547 8a35f56c 2022-07-16 thomas struct got_reference *ref;
548 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
549 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
550 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
551 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
552 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
553 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
554 8a35f56c 2022-07-16 thomas struct got_tag_object *tag = NULL;
555 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
556 8a35f56c 2022-07-16 thomas char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
557 217813df 2022-09-02 thomas char *tag_commit = NULL, *tag_commit0 = NULL;
558 8a35f56c 2022-07-16 thomas char *commit_msg = NULL, *commit_msg0 = NULL;
559 8a35f56c 2022-07-16 thomas int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
561 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
563 8a35f56c 2022-07-16 thomas if (asprintf(&repo_path, "%s/%s", srv->repos_path,
564 8a35f56c 2022-07-16 thomas repo_dir->name) == -1)
565 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
567 d6795e9f 2022-12-30 thomas if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
568 8a35f56c 2022-07-16 thomas error = got_ref_open(&ref, repo, qs->headref, 0);
569 8a35f56c 2022-07-16 thomas if (error)
570 8a35f56c 2022-07-16 thomas goto err;
571 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, ref);
572 8a35f56c 2022-07-16 thomas got_ref_close(ref);
573 8a35f56c 2022-07-16 thomas if (error)
574 8a35f56c 2022-07-16 thomas goto err;
575 8a35f56c 2022-07-16 thomas } else if (qs->commit == NULL && qs->action == TAG) {
576 8a35f56c 2022-07-16 thomas error = got_error_msg(GOT_ERR_EOF, "commit id missing");
577 8a35f56c 2022-07-16 thomas goto err;
579 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id_prefix(&id, qs->commit,
580 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, repo);
581 8a35f56c 2022-07-16 thomas if (error)
582 8a35f56c 2022-07-16 thomas goto err;
585 8a35f56c 2022-07-16 thomas if (qs->action != SUMMARY && qs->action != TAGS) {
586 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
587 8a35f56c 2022-07-16 thomas if (error)
588 8a35f56c 2022-07-16 thomas goto err;
589 8a35f56c 2022-07-16 thomas error = got_object_commit_get_logmsg(&commit_msg0, commit);
590 8a35f56c 2022-07-16 thomas if (error)
591 8a35f56c 2022-07-16 thomas goto err;
592 8a35f56c 2022-07-16 thomas if (commit) {
593 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
594 8a35f56c 2022-07-16 thomas commit = NULL;
598 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, repo_path);
599 8a35f56c 2022-07-16 thomas if (error)
600 8a35f56c 2022-07-16 thomas goto err;
602 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
604 8a35f56c 2022-07-16 thomas if (error)
605 8a35f56c 2022-07-16 thomas goto err;
607 8a35f56c 2022-07-16 thomas if (limit == 1)
608 8a35f56c 2022-07-16 thomas chk_multi = 0;
611 8a35f56c 2022-07-16 thomas * XXX: again, see previous message about caching
614 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
615 8a35f56c 2022-07-16 thomas struct repo_tag *new_repo_tag = NULL;
616 8a35f56c 2022-07-16 thomas error = got_init_repo_tag(&new_repo_tag);
617 8a35f56c 2022-07-16 thomas if (error)
618 8a35f56c 2022-07-16 thomas goto err;
620 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
622 8a35f56c 2022-07-16 thomas new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
623 8a35f56c 2022-07-16 thomas if (new_repo_tag->tag_name == NULL) {
624 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
625 8a35f56c 2022-07-16 thomas goto err;
628 19d60ee3 2022-09-02 thomas free(id);
629 19d60ee3 2022-09-02 thomas id = NULL;
631 19d60ee3 2022-09-02 thomas free(id_str);
632 19d60ee3 2022-09-02 thomas id_str = NULL;
634 8a35f56c 2022-07-16 thomas error = got_ref_resolve(&id, repo, re->ref);
635 8a35f56c 2022-07-16 thomas if (error)
636 8a35f56c 2022-07-16 thomas goto done;
639 acd4ff87 2022-09-02 thomas got_object_tag_close(tag);
640 8a35f56c 2022-07-16 thomas error = got_object_open_as_tag(&tag, repo, id);
641 8a35f56c 2022-07-16 thomas if (error) {
642 19d60ee3 2022-09-02 thomas if (error->code != GOT_ERR_OBJ_TYPE)
643 8a35f56c 2022-07-16 thomas goto done;
644 8a35f56c 2022-07-16 thomas /* "lightweight" tag */
645 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, id);
646 19d60ee3 2022-09-02 thomas if (error)
647 8a35f56c 2022-07-16 thomas goto done;
648 8a35f56c 2022-07-16 thomas new_repo_tag->tagger =
649 8a35f56c 2022-07-16 thomas strdup(got_object_commit_get_committer(commit));
650 8a35f56c 2022-07-16 thomas if (new_repo_tag->tagger == NULL) {
651 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
652 8a35f56c 2022-07-16 thomas goto err;
654 8a35f56c 2022-07-16 thomas new_repo_tag->tagger_time =
655 8a35f56c 2022-07-16 thomas got_object_commit_get_committer_time(commit);
656 8a35f56c 2022-07-16 thomas error = got_object_id_str(&id_str, id);
657 8a35f56c 2022-07-16 thomas if (error)
658 8a35f56c 2022-07-16 thomas goto err;
660 8a35f56c 2022-07-16 thomas new_repo_tag->tagger =
661 8a35f56c 2022-07-16 thomas strdup(got_object_tag_get_tagger(tag));
662 8a35f56c 2022-07-16 thomas if (new_repo_tag->tagger == NULL) {
663 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
664 8a35f56c 2022-07-16 thomas goto err;
666 8a35f56c 2022-07-16 thomas new_repo_tag->tagger_time =
667 8a35f56c 2022-07-16 thomas got_object_tag_get_tagger_time(tag);
668 8a35f56c 2022-07-16 thomas error = got_object_id_str(&id_str,
669 8a35f56c 2022-07-16 thomas got_object_tag_get_object_id(tag));
670 8a35f56c 2022-07-16 thomas if (error)
671 8a35f56c 2022-07-16 thomas goto err;
674 8a35f56c 2022-07-16 thomas new_repo_tag->commit_id = strdup(id_str);
675 8a35f56c 2022-07-16 thomas if (new_repo_tag->commit_id == NULL)
676 8a35f56c 2022-07-16 thomas goto err;
678 8a35f56c 2022-07-16 thomas if (commit_found == 0 && qs->commit != NULL &&
679 8a35f56c 2022-07-16 thomas strncmp(id_str, qs->commit, strlen(id_str)) != 0)
680 8a35f56c 2022-07-16 thomas continue;
682 8a35f56c 2022-07-16 thomas commit_found = 1;
684 8a35f56c 2022-07-16 thomas t->tag_count++;
687 8a35f56c 2022-07-16 thomas * check for one more commit before breaking,
688 8a35f56c 2022-07-16 thomas * so we know whether to navigate through briefs
689 8a35f56c 2022-07-16 thomas * commits and summary
691 8a35f56c 2022-07-16 thomas if (chk_next) {
692 8a35f56c 2022-07-16 thomas t->next_id = strdup(new_repo_tag->commit_id);
693 8a35f56c 2022-07-16 thomas if (t->next_id == NULL) {
694 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
695 8a35f56c 2022-07-16 thomas goto err;
697 8a35f56c 2022-07-16 thomas if (commit) {
698 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
699 8a35f56c 2022-07-16 thomas commit = NULL;
701 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
702 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(new_repo_tag);
703 8a35f56c 2022-07-16 thomas goto done;
706 8a35f56c 2022-07-16 thomas if (commit) {
707 217813df 2022-09-02 thomas error = got_object_commit_get_logmsg(&tag_commit0,
709 8a35f56c 2022-07-16 thomas if (error)
710 217813df 2022-09-02 thomas goto err;
711 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
712 8a35f56c 2022-07-16 thomas commit = NULL;
714 217813df 2022-09-02 thomas tag_commit0 = strdup(got_object_tag_get_message(tag));
715 217813df 2022-09-02 thomas if (tag_commit0 == NULL) {
716 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
717 217813df 2022-09-02 thomas goto err;
721 217813df 2022-09-02 thomas tag_commit = tag_commit0;
722 217813df 2022-09-02 thomas while (*tag_commit == '\n')
723 217813df 2022-09-02 thomas tag_commit++;
724 217813df 2022-09-02 thomas new_repo_tag->tag_commit = strdup(tag_commit);
725 217813df 2022-09-02 thomas if (new_repo_tag->tag_commit == NULL) {
726 217813df 2022-09-02 thomas error = got_error_from_errno("strdup");
727 217813df 2022-09-02 thomas free(tag_commit0);
728 217813df 2022-09-02 thomas goto err;
730 217813df 2022-09-02 thomas free(tag_commit0);
732 8a35f56c 2022-07-16 thomas if (qs->action != SUMMARY && qs->action != TAGS) {
733 8a35f56c 2022-07-16 thomas commit_msg = commit_msg0;
734 8a35f56c 2022-07-16 thomas while (*commit_msg == '\n')
735 8a35f56c 2022-07-16 thomas commit_msg++;
737 8a35f56c 2022-07-16 thomas new_repo_tag->commit_msg = strdup(commit_msg);
738 8a35f56c 2022-07-16 thomas if (new_repo_tag->commit_msg == NULL) {
739 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
740 8a35f56c 2022-07-16 thomas goto err;
744 8a35f56c 2022-07-16 thomas if (limit && --limit == 0) {
745 8a35f56c 2022-07-16 thomas if (chk_multi == 0)
747 8a35f56c 2022-07-16 thomas chk_next = 1;
753 8a35f56c 2022-07-16 thomas * we have tailq populated, so find previous commit id
754 8a35f56c 2022-07-16 thomas * for navigation through briefs and commits
756 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
757 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
758 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
759 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
762 8a35f56c 2022-07-16 thomas if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
763 8a35f56c 2022-07-16 thomas commit_found = 0;
764 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
766 8a35f56c 2022-07-16 thomas if (commit_found == 0 && rt->commit_id != NULL &&
767 8a35f56c 2022-07-16 thomas strcmp(qs->commit, rt->commit_id) != 0) {
768 8a35f56c 2022-07-16 thomas continue;
770 8a35f56c 2022-07-16 thomas commit_found = 1;
771 8a35f56c 2022-07-16 thomas if (c_cnt == srv->max_commits_display ||
772 8a35f56c 2022-07-16 thomas rt == TAILQ_FIRST(&t->repo_tags)) {
773 8a35f56c 2022-07-16 thomas t->prev_id = strdup(rt->commit_id);
774 8a35f56c 2022-07-16 thomas if (t->prev_id == NULL)
775 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
782 8a35f56c 2022-07-16 thomas if (commit)
783 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
785 19d60ee3 2022-09-02 thomas got_object_tag_close(tag);
786 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
787 ddcf1d33 2022-09-02 thomas free(commit_msg0);
788 19d60ee3 2022-09-02 thomas free(in_repo_path);
789 8a35f56c 2022-07-16 thomas free(repo_path);
790 8a35f56c 2022-07-16 thomas free(id);
791 19d60ee3 2022-09-02 thomas free(id_str);
792 8a35f56c 2022-07-16 thomas return error;
796 3c14c1f2 2023-01-06 thomas got_output_repo_tree(struct request *c,
797 3c14c1f2 2023-01-06 thomas int (*cb)(struct template *, struct got_tree_entry *))
799 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
800 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
801 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
802 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
803 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
804 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
805 8a35f56c 2022-07-16 thomas struct got_object_id *tree_id = NULL, *commit_id = NULL;
806 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
807 8a35f56c 2022-07-16 thomas struct got_tree_object *tree = NULL;
808 3c14c1f2 2023-01-06 thomas struct got_tree_entry *te;
809 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
810 06325330 2022-09-02 thomas char *escaped_name = NULL, *path = NULL;
811 3c14c1f2 2023-01-06 thomas int nentries, i;
813 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
815 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
817 8a35f56c 2022-07-16 thomas if (qs->folder != NULL) {
818 8a35f56c 2022-07-16 thomas path = strdup(qs->folder);
819 8a35f56c 2022-07-16 thomas if (path == NULL) {
820 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
821 8a35f56c 2022-07-16 thomas goto done;
824 557d32ee 2022-09-02 thomas error = got_repo_map_path(&path, repo, repo_dir->path);
825 8a35f56c 2022-07-16 thomas if (error)
826 8a35f56c 2022-07-16 thomas goto done;
829 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
830 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
831 8a35f56c 2022-07-16 thomas if (error)
832 8a35f56c 2022-07-16 thomas goto done;
834 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
835 8a35f56c 2022-07-16 thomas if (error)
836 8a35f56c 2022-07-16 thomas goto done;
838 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&tree_id, repo, commit, path);
839 8a35f56c 2022-07-16 thomas if (error)
840 8a35f56c 2022-07-16 thomas goto done;
842 8a35f56c 2022-07-16 thomas error = got_object_open_as_tree(&tree, repo, tree_id);
843 8a35f56c 2022-07-16 thomas if (error)
844 8a35f56c 2022-07-16 thomas goto done;
846 8a35f56c 2022-07-16 thomas nentries = got_object_tree_get_nentries(tree);
848 8a35f56c 2022-07-16 thomas for (i = 0; i < nentries; i++) {
849 8a35f56c 2022-07-16 thomas te = got_object_tree_get_entry(tree, i);
850 3c14c1f2 2023-01-06 thomas if (cb(c->tp, te) == -1)
854 df610f47 2022-08-27 thomas free(escaped_name);
855 8a35f56c 2022-07-16 thomas free(path);
856 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
857 8a35f56c 2022-07-16 thomas if (commit)
858 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
859 12e242cc 2022-09-06 thomas if (tree)
860 12e242cc 2022-09-06 thomas got_object_tree_close(tree);
861 8a35f56c 2022-07-16 thomas free(commit_id);
862 8a35f56c 2022-07-16 thomas free(tree_id);
863 3c14c1f2 2023-01-06 thomas if (error) {
864 3c14c1f2 2023-01-06 thomas log_warnx("%s: %s", __func__, error->msg);
865 3c14c1f2 2023-01-06 thomas return -1;
867 3c14c1f2 2023-01-06 thomas return 0;
870 8a35f56c 2022-07-16 thomas const struct got_error *
871 b82440e1 2023-01-06 thomas got_open_blob_for_output(struct got_blob_object **blob, int *fd,
872 b82440e1 2023-01-06 thomas int *binary, struct request *c)
874 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
875 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
876 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
877 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
878 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
879 8a35f56c 2022-07-16 thomas struct got_object_id *commit_id = NULL;
880 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
881 8a35f56c 2022-07-16 thomas char *path = NULL, *in_repo_path = NULL;
882 b82440e1 2023-01-06 thomas int obj_type;
884 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
886 b82440e1 2023-01-06 thomas *blob = NULL;
887 b82440e1 2023-01-06 thomas *fd = -1;
888 b82440e1 2023-01-06 thomas *binary = 0;
890 8a35f56c 2022-07-16 thomas if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
891 8a35f56c 2022-07-16 thomas qs->folder ? "/" : "", qs->file) == -1) {
892 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
893 8a35f56c 2022-07-16 thomas goto done;
896 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, path);
897 8a35f56c 2022-07-16 thomas if (error)
898 8a35f56c 2022-07-16 thomas goto done;
900 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
901 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
902 8a35f56c 2022-07-16 thomas if (error)
903 8a35f56c 2022-07-16 thomas goto done;
905 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
906 8a35f56c 2022-07-16 thomas if (error)
907 8a35f56c 2022-07-16 thomas goto done;
909 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
910 8a35f56c 2022-07-16 thomas if (error)
911 8a35f56c 2022-07-16 thomas goto done;
913 8a35f56c 2022-07-16 thomas if (commit_id == NULL) {
914 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_NO_OBJ);
915 8a35f56c 2022-07-16 thomas goto done;
918 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, commit_id);
919 8a35f56c 2022-07-16 thomas if (error)
920 8a35f56c 2022-07-16 thomas goto done;
922 8a35f56c 2022-07-16 thomas if (obj_type != GOT_OBJ_TYPE_BLOB) {
923 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
924 8a35f56c 2022-07-16 thomas goto done;
927 b82440e1 2023-01-06 thomas error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
928 8a35f56c 2022-07-16 thomas if (error)
929 8a35f56c 2022-07-16 thomas goto done;
931 b82440e1 2023-01-06 thomas error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
932 8a35f56c 2022-07-16 thomas if (error)
933 8a35f56c 2022-07-16 thomas goto done;
935 b82440e1 2023-01-06 thomas error = got_object_blob_is_binary(binary, *blob);
936 efe3fd03 2023-01-06 thomas if (error)
937 efe3fd03 2023-01-06 thomas goto done;
940 b82440e1 2023-01-06 thomas if (commit)
941 b82440e1 2023-01-06 thomas got_object_commit_close(commit);
943 b82440e1 2023-01-06 thomas if (error) {
944 b82440e1 2023-01-06 thomas if (*fd != -1)
945 b82440e1 2023-01-06 thomas close(*fd);
946 b82440e1 2023-01-06 thomas if (*blob)
947 b82440e1 2023-01-06 thomas got_object_blob_close(*blob);
948 b82440e1 2023-01-06 thomas *fd = -1;
949 b82440e1 2023-01-06 thomas *blob = NULL;
952 b82440e1 2023-01-06 thomas free(in_repo_path);
953 b82440e1 2023-01-06 thomas free(commit_id);
954 b82440e1 2023-01-06 thomas free(path);
955 b82440e1 2023-01-06 thomas return error;
958 b82440e1 2023-01-06 thomas const struct got_error *
959 b82440e1 2023-01-06 thomas got_output_file_blob(struct request *c)
961 b82440e1 2023-01-06 thomas const struct got_error *error = NULL;
962 b82440e1 2023-01-06 thomas struct querystring *qs = c->t->qs;
963 b82440e1 2023-01-06 thomas struct got_blob_object *blob = NULL;
964 b82440e1 2023-01-06 thomas size_t len;
965 b82440e1 2023-01-06 thomas int binary, fd = -1;
966 b82440e1 2023-01-06 thomas const uint8_t *buf;
968 b82440e1 2023-01-06 thomas error = got_open_blob_for_output(&blob, &fd, &binary, c);
969 b82440e1 2023-01-06 thomas if (error)
970 b82440e1 2023-01-06 thomas return error;
972 b82440e1 2023-01-06 thomas if (binary)
973 efe3fd03 2023-01-06 thomas error = gotweb_render_content_type_file(c,
974 efe3fd03 2023-01-06 thomas "application/octet-stream", qs->file, NULL);
976 efe3fd03 2023-01-06 thomas error = gotweb_render_content_type(c, "text/plain");
978 efe3fd03 2023-01-06 thomas if (error) {
979 efe3fd03 2023-01-06 thomas log_warnx("%s: %s", __func__, error->msg);
980 efe3fd03 2023-01-06 thomas goto done;
983 efe3fd03 2023-01-06 thomas for (;;) {
984 8a35f56c 2022-07-16 thomas error = got_object_blob_read_block(&len, blob);
985 8a35f56c 2022-07-16 thomas if (error)
986 8a35f56c 2022-07-16 thomas goto done;
987 efe3fd03 2023-01-06 thomas if (len == 0)
989 8a35f56c 2022-07-16 thomas buf = got_object_blob_get_read_buf(blob);
990 efe3fd03 2023-01-06 thomas fcgi_gen_binary_response(c, buf, len);
993 b82440e1 2023-01-06 thomas if (close(fd) == -1 && error == NULL)
994 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
995 8a35f56c 2022-07-16 thomas if (blob)
996 8a35f56c 2022-07-16 thomas got_object_blob_close(blob);
997 8a35f56c 2022-07-16 thomas return error;
1001 b82440e1 2023-01-06 thomas got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
1002 b82440e1 2023-01-06 thomas int (*cb)(struct template *, const char *, size_t))
1004 b82440e1 2023-01-06 thomas const struct got_error *err;
1005 b82440e1 2023-01-06 thomas char *line = NULL;
1006 b82440e1 2023-01-06 thomas size_t linesize = 0;
1007 b82440e1 2023-01-06 thomas size_t lineno = 0;
1008 b82440e1 2023-01-06 thomas ssize_t linelen = 0;
1010 b82440e1 2023-01-06 thomas for (;;) {
1011 b82440e1 2023-01-06 thomas err = got_object_blob_getline(&line, &linelen, &linesize,
1013 b82440e1 2023-01-06 thomas if (err || linelen == -1)
1015 b82440e1 2023-01-06 thomas lineno++;
1016 b82440e1 2023-01-06 thomas if (cb(tp, line, lineno) == -1)
1020 b82440e1 2023-01-06 thomas free(line);
1022 b82440e1 2023-01-06 thomas if (err) {
1023 b82440e1 2023-01-06 thomas log_warnx("%s: got_object_blob_getline failed: %s",
1024 b82440e1 2023-01-06 thomas __func__, err->msg);
1025 b82440e1 2023-01-06 thomas return -1;
1027 b82440e1 2023-01-06 thomas return 0;
1030 8a35f56c 2022-07-16 thomas struct blame_line {
1031 8a35f56c 2022-07-16 thomas int annotated;
1032 8a35f56c 2022-07-16 thomas char *id_str;
1033 8a35f56c 2022-07-16 thomas char *committer;
1034 8a35f56c 2022-07-16 thomas char datebuf[11]; /* YYYY-MM-DD + NUL */
1037 8a35f56c 2022-07-16 thomas struct blame_cb_args {
1038 8a35f56c 2022-07-16 thomas struct blame_line *lines;
1039 8a35f56c 2022-07-16 thomas int nlines;
1040 8a35f56c 2022-07-16 thomas int nlines_prec;
1041 8a35f56c 2022-07-16 thomas int lineno_cur;
1042 8a35f56c 2022-07-16 thomas off_t *line_offsets;
1043 8a35f56c 2022-07-16 thomas FILE *f;
1044 8a35f56c 2022-07-16 thomas struct got_repository *repo;
1045 8a35f56c 2022-07-16 thomas struct request *c;
1048 8a35f56c 2022-07-16 thomas static const struct got_error *
1049 8a35f56c 2022-07-16 thomas got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1050 8a35f56c 2022-07-16 thomas struct got_commit_object *commit, struct got_object_id *id)
1052 8a35f56c 2022-07-16 thomas const struct got_error *err = NULL;
1053 8a35f56c 2022-07-16 thomas struct blame_cb_args *a = arg;
1054 8a35f56c 2022-07-16 thomas struct blame_line *bline;
1055 8a35f56c 2022-07-16 thomas struct request *c = a->c;
1056 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1057 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
1058 8a35f56c 2022-07-16 thomas char *line = NULL, *eline = NULL;
1059 8a35f56c 2022-07-16 thomas size_t linesize = 0;
1060 8a35f56c 2022-07-16 thomas off_t offset;
1061 8a35f56c 2022-07-16 thomas struct tm tm;
1062 8a35f56c 2022-07-16 thomas time_t committer_time;
1065 8a35f56c 2022-07-16 thomas if (nlines != a->nlines ||
1066 8a35f56c 2022-07-16 thomas (lineno != -1 && lineno < 1) || lineno > a->nlines)
1067 8a35f56c 2022-07-16 thomas return got_error(GOT_ERR_RANGE);
1069 8a35f56c 2022-07-16 thomas if (lineno == -1)
1070 8a35f56c 2022-07-16 thomas return NULL; /* no change in this commit */
1072 8a35f56c 2022-07-16 thomas /* Annotate this line. */
1073 8a35f56c 2022-07-16 thomas bline = &a->lines[lineno - 1];
1074 8a35f56c 2022-07-16 thomas if (bline->annotated)
1075 8a35f56c 2022-07-16 thomas return NULL;
1076 8a35f56c 2022-07-16 thomas err = got_object_id_str(&bline->id_str, id);
1077 8a35f56c 2022-07-16 thomas if (err)
1078 8a35f56c 2022-07-16 thomas return err;
1080 8a35f56c 2022-07-16 thomas bline->committer = strdup(got_object_commit_get_committer(commit));
1081 8a35f56c 2022-07-16 thomas if (bline->committer == NULL) {
1082 8a35f56c 2022-07-16 thomas err = got_error_from_errno("strdup");
1083 8a35f56c 2022-07-16 thomas goto done;
1086 8a35f56c 2022-07-16 thomas committer_time = got_object_commit_get_committer_time(commit);
1087 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
1088 8a35f56c 2022-07-16 thomas return got_error_from_errno("gmtime_r");
1089 8a35f56c 2022-07-16 thomas if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1090 8a35f56c 2022-07-16 thomas &tm) == 0) {
1091 8a35f56c 2022-07-16 thomas err = got_error(GOT_ERR_NO_SPACE);
1092 8a35f56c 2022-07-16 thomas goto done;
1094 8a35f56c 2022-07-16 thomas bline->annotated = 1;
1096 8a35f56c 2022-07-16 thomas /* Print lines annotated so far. */
1097 8a35f56c 2022-07-16 thomas bline = &a->lines[a->lineno_cur - 1];
1098 8a35f56c 2022-07-16 thomas if (!bline->annotated)
1099 8a35f56c 2022-07-16 thomas goto done;
1101 8a35f56c 2022-07-16 thomas offset = a->line_offsets[a->lineno_cur - 1];
1102 8a35f56c 2022-07-16 thomas if (fseeko(a->f, offset, SEEK_SET) == -1) {
1103 8a35f56c 2022-07-16 thomas err = got_error_from_errno("fseeko");
1104 8a35f56c 2022-07-16 thomas goto done;
1107 b5c07627 2022-08-18 thomas while (a->lineno_cur <= a->nlines && bline->annotated) {
1108 8a35f56c 2022-07-16 thomas char *smallerthan, *at, *nl, *committer;
1109 8a35f56c 2022-07-16 thomas size_t len;
1111 8a35f56c 2022-07-16 thomas if (getline(&line, &linesize, a->f) == -1) {
1112 8a35f56c 2022-07-16 thomas if (ferror(a->f))
1113 8a35f56c 2022-07-16 thomas err = got_error_from_errno("getline");
1117 8a35f56c 2022-07-16 thomas committer = bline->committer;
1118 8a35f56c 2022-07-16 thomas smallerthan = strchr(committer, '<');
1119 8a35f56c 2022-07-16 thomas if (smallerthan && smallerthan[1] != '\0')
1120 8a35f56c 2022-07-16 thomas committer = smallerthan + 1;
1121 8a35f56c 2022-07-16 thomas at = strchr(committer, '@');
1123 8a35f56c 2022-07-16 thomas *at = '\0';
1124 8a35f56c 2022-07-16 thomas len = strlen(committer);
1125 8a35f56c 2022-07-16 thomas if (len >= 9)
1126 8a35f56c 2022-07-16 thomas committer[8] = '\0';
1128 8a35f56c 2022-07-16 thomas nl = strchr(line, '\n');
1130 8a35f56c 2022-07-16 thomas *nl = '\0';
1132 8a35f56c 2022-07-16 thomas err = gotweb_escape_html(&eline, line);
1133 8a35f56c 2022-07-16 thomas if (err)
1134 8a35f56c 2022-07-16 thomas goto done;
1136 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='blame_wrapper'>"
1137 79393471 2022-08-27 thomas "<div class='blame_number'>%.*d</div>"
1138 55144267 2022-09-07 thomas "<div class='blame_hash'>",
1139 55144267 2022-09-07 thomas a->nlines_prec, a->lineno_cur) == -1)
1140 55144267 2022-09-07 thomas goto done;
1142 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1143 55144267 2022-09-07 thomas .action = DIFF,
1144 55144267 2022-09-07 thomas .index_page = -1,
1145 55144267 2022-09-07 thomas .page = -1,
1146 55144267 2022-09-07 thomas .path = repo_dir->name,
1147 55144267 2022-09-07 thomas .commit = bline->id_str,
1148 55144267 2022-09-07 thomas }, "%.8s", bline->id_str);
1149 55144267 2022-09-07 thomas if (r == -1)
1150 55144267 2022-09-07 thomas goto done;
1152 55144267 2022-09-07 thomas r = fcgi_printf(c,
1153 79393471 2022-08-27 thomas "</div>"
1154 79393471 2022-08-27 thomas "<div class='blame_date'>%s</div>"
1155 79393471 2022-08-27 thomas "<div class='blame_author'>%s</div>"
1156 79393471 2022-08-27 thomas "<div class='blame_code'>%s</div>"
1157 79393471 2022-08-27 thomas "</div>", /* .blame_wrapper */
1158 79393471 2022-08-27 thomas bline->datebuf,
1159 79393471 2022-08-27 thomas committer,
1161 79393471 2022-08-27 thomas if (r == -1)
1162 8a35f56c 2022-07-16 thomas goto done;
1164 8a35f56c 2022-07-16 thomas a->lineno_cur++;
1165 8a35f56c 2022-07-16 thomas bline = &a->lines[a->lineno_cur - 1];
1167 d3a6241d 2022-09-03 thomas free(eline);
1168 d3a6241d 2022-09-03 thomas eline = NULL;
1171 8a35f56c 2022-07-16 thomas free(line);
1172 8a35f56c 2022-07-16 thomas free(eline);
1173 8a35f56c 2022-07-16 thomas return err;
1176 8a35f56c 2022-07-16 thomas const struct got_error *
1177 8a35f56c 2022-07-16 thomas got_output_file_blame(struct request *c)
1179 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1180 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1181 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1182 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
1183 8a35f56c 2022-07-16 thomas struct got_object_id *obj_id = NULL, *commit_id = NULL;
1184 8a35f56c 2022-07-16 thomas struct got_commit_object *commit = NULL;
1185 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1186 8a35f56c 2022-07-16 thomas struct got_blob_object *blob = NULL;
1187 8a35f56c 2022-07-16 thomas char *path = NULL, *in_repo_path = NULL;
1188 8a35f56c 2022-07-16 thomas struct blame_cb_args bca;
1189 8a35f56c 2022-07-16 thomas int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1190 8a35f56c 2022-07-16 thomas int fd6 = -1;
1191 8a35f56c 2022-07-16 thomas off_t filesize;
1192 8a35f56c 2022-07-16 thomas FILE *f1 = NULL, *f2 = NULL;
1194 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1195 8a35f56c 2022-07-16 thomas bca.f = NULL;
1196 8a35f56c 2022-07-16 thomas bca.lines = NULL;
1198 8a35f56c 2022-07-16 thomas if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1199 8a35f56c 2022-07-16 thomas qs->folder ? "/" : "", qs->file) == -1) {
1200 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1201 8a35f56c 2022-07-16 thomas goto done;
1204 8a35f56c 2022-07-16 thomas error = got_repo_map_path(&in_repo_path, repo, path);
1205 8a35f56c 2022-07-16 thomas if (error)
1206 8a35f56c 2022-07-16 thomas goto done;
1208 8a35f56c 2022-07-16 thomas error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1209 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
1210 8a35f56c 2022-07-16 thomas if (error)
1211 8a35f56c 2022-07-16 thomas goto done;
1213 8a35f56c 2022-07-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
1214 8a35f56c 2022-07-16 thomas if (error)
1215 8a35f56c 2022-07-16 thomas goto done;
1217 8a35f56c 2022-07-16 thomas error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1218 8a35f56c 2022-07-16 thomas if (error)
1219 8a35f56c 2022-07-16 thomas goto done;
1221 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, obj_id);
1222 8a35f56c 2022-07-16 thomas if (error)
1223 8a35f56c 2022-07-16 thomas goto done;
1225 8a35f56c 2022-07-16 thomas if (obj_type != GOT_OBJ_TYPE_BLOB) {
1226 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
1227 8a35f56c 2022-07-16 thomas goto done;
1230 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1231 8a35f56c 2022-07-16 thomas if (error)
1232 8a35f56c 2022-07-16 thomas goto done;
1234 8a35f56c 2022-07-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1235 8a35f56c 2022-07-16 thomas if (error)
1236 8a35f56c 2022-07-16 thomas goto done;
1238 8a35f56c 2022-07-16 thomas error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1239 8a35f56c 2022-07-16 thomas if (error)
1240 8a35f56c 2022-07-16 thomas goto done;
1242 8a35f56c 2022-07-16 thomas error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1243 8a35f56c 2022-07-16 thomas &bca.line_offsets, bca.f, blob);
1244 8a35f56c 2022-07-16 thomas if (error || bca.nlines == 0)
1245 8a35f56c 2022-07-16 thomas goto done;
1247 8a35f56c 2022-07-16 thomas /* Don't include \n at EOF in the blame line count. */
1248 8a35f56c 2022-07-16 thomas if (bca.line_offsets[bca.nlines - 1] == filesize)
1249 8a35f56c 2022-07-16 thomas bca.nlines--;
1251 8a35f56c 2022-07-16 thomas bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1252 8a35f56c 2022-07-16 thomas if (bca.lines == NULL) {
1253 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1254 8a35f56c 2022-07-16 thomas goto done;
1256 8a35f56c 2022-07-16 thomas bca.lineno_cur = 1;
1257 8a35f56c 2022-07-16 thomas bca.nlines_prec = 0;
1258 8a35f56c 2022-07-16 thomas i = bca.nlines;
1259 8a35f56c 2022-07-16 thomas while (i > 0) {
1260 8a35f56c 2022-07-16 thomas i /= 10;
1261 8a35f56c 2022-07-16 thomas bca.nlines_prec++;
1263 8a35f56c 2022-07-16 thomas bca.repo = repo;
1264 8a35f56c 2022-07-16 thomas bca.c = c;
1266 8a35f56c 2022-07-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1267 8a35f56c 2022-07-16 thomas if (error)
1268 8a35f56c 2022-07-16 thomas goto done;
1270 8a35f56c 2022-07-16 thomas error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1271 8a35f56c 2022-07-16 thomas if (error)
1272 8a35f56c 2022-07-16 thomas goto done;
1274 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1275 8a35f56c 2022-07-16 thomas if (error)
1276 8a35f56c 2022-07-16 thomas goto done;
1278 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1279 8a35f56c 2022-07-16 thomas if (error)
1280 8a35f56c 2022-07-16 thomas goto done;
1282 8a35f56c 2022-07-16 thomas error = got_blame(in_repo_path, commit_id, repo,
1283 8a35f56c 2022-07-16 thomas GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1284 8a35f56c 2022-07-16 thomas fd3, fd4, f1, f2);
1287 d38635a3 2022-09-03 thomas if (bca.lines) {
1288 8a35f56c 2022-07-16 thomas free(bca.line_offsets);
1289 8a35f56c 2022-07-16 thomas for (i = 0; i < bca.nlines; i++) {
1290 8a35f56c 2022-07-16 thomas struct blame_line *bline = &bca.lines[i];
1291 8a35f56c 2022-07-16 thomas free(bline->id_str);
1292 8a35f56c 2022-07-16 thomas free(bline->committer);
1295 8a35f56c 2022-07-16 thomas free(bca.lines);
1296 8a35f56c 2022-07-16 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1297 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1298 8a35f56c 2022-07-16 thomas if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1299 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1300 8a35f56c 2022-07-16 thomas if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1301 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1302 8a35f56c 2022-07-16 thomas if (bca.f) {
1303 8a35f56c 2022-07-16 thomas const struct got_error *bca_err =
1304 8a35f56c 2022-07-16 thomas got_gotweb_flushfile(bca.f, fd1);
1305 8a35f56c 2022-07-16 thomas if (error == NULL)
1306 8a35f56c 2022-07-16 thomas error = bca_err;
1308 8a35f56c 2022-07-16 thomas if (f1) {
1309 8a35f56c 2022-07-16 thomas const struct got_error *f1_err =
1310 8a35f56c 2022-07-16 thomas got_gotweb_flushfile(f1, fd5);
1311 8a35f56c 2022-07-16 thomas if (error == NULL)
1312 8a35f56c 2022-07-16 thomas error = f1_err;
1314 8a35f56c 2022-07-16 thomas if (f2) {
1315 8a35f56c 2022-07-16 thomas const struct got_error *f2_err =
1316 8a35f56c 2022-07-16 thomas got_gotweb_flushfile(f2, fd6);
1317 8a35f56c 2022-07-16 thomas if (error == NULL)
1318 8a35f56c 2022-07-16 thomas error = f2_err;
1320 8a35f56c 2022-07-16 thomas if (commit)
1321 8a35f56c 2022-07-16 thomas got_object_commit_close(commit);
1322 8a35f56c 2022-07-16 thomas if (blob)
1323 8a35f56c 2022-07-16 thomas got_object_blob_close(blob);
1324 8a35f56c 2022-07-16 thomas free(in_repo_path);
1325 8a35f56c 2022-07-16 thomas free(commit_id);
1326 aefd24c9 2022-09-03 thomas free(obj_id);
1327 8a35f56c 2022-07-16 thomas free(path);
1328 aefd24c9 2022-09-03 thomas got_ref_list_free(&refs);
1329 8a35f56c 2022-07-16 thomas return error;
1332 8a35f56c 2022-07-16 thomas const struct got_error *
1333 dccd05b4 2023-01-10 thomas got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1335 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1336 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1337 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1338 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1339 8a35f56c 2022-07-16 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
1340 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1341 8a35f56c 2022-07-16 thomas FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1342 8a35f56c 2022-07-16 thomas int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1344 dccd05b4 2023-01-10 thomas *fp = NULL;
1345 dccd05b4 2023-01-10 thomas *fd = -1;
1347 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1349 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1350 8a35f56c 2022-07-16 thomas if (error)
1351 8a35f56c 2022-07-16 thomas return error;
1353 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1354 8a35f56c 2022-07-16 thomas if (error)
1355 8a35f56c 2022-07-16 thomas return error;
1357 8a35f56c 2022-07-16 thomas error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1358 8a35f56c 2022-07-16 thomas if (error)
1359 8a35f56c 2022-07-16 thomas return error;
1361 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1363 8a35f56c 2022-07-16 thomas if (rc->parent_id != NULL &&
1364 8a35f56c 2022-07-16 thomas strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1365 dccd05b4 2023-01-10 thomas error = got_repo_match_object_id(&id1, NULL,
1366 8a35f56c 2022-07-16 thomas rc->parent_id, GOT_OBJ_TYPE_ANY,
1367 8a35f56c 2022-07-16 thomas &refs, repo);
1368 8a35f56c 2022-07-16 thomas if (error)
1369 8a35f56c 2022-07-16 thomas goto done;
1372 dccd05b4 2023-01-10 thomas error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1373 8a35f56c 2022-07-16 thomas GOT_OBJ_TYPE_ANY, &refs, repo);
1374 8a35f56c 2022-07-16 thomas if (error)
1375 8a35f56c 2022-07-16 thomas goto done;
1377 8a35f56c 2022-07-16 thomas error = got_object_get_type(&obj_type, repo, id2);
1378 8a35f56c 2022-07-16 thomas if (error)
1379 8a35f56c 2022-07-16 thomas goto done;
1381 8a35f56c 2022-07-16 thomas error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1382 8a35f56c 2022-07-16 thomas if (error)
1383 8a35f56c 2022-07-16 thomas goto done;
1385 8a35f56c 2022-07-16 thomas error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1386 8a35f56c 2022-07-16 thomas if (error)
1387 8a35f56c 2022-07-16 thomas goto done;
1389 8a35f56c 2022-07-16 thomas switch (obj_type) {
1390 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_BLOB:
1391 8a35f56c 2022-07-16 thomas error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1392 8a35f56c 2022-07-16 thomas id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1393 53d03f97 2023-01-10 thomas 0, NULL, repo, f3);
1395 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_TREE:
1396 8a35f56c 2022-07-16 thomas error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1397 8a35f56c 2022-07-16 thomas id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1398 53d03f97 2023-01-10 thomas 0, NULL, repo, f3);
1400 8a35f56c 2022-07-16 thomas case GOT_OBJ_TYPE_COMMIT:
1401 8a35f56c 2022-07-16 thomas error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1402 8a35f56c 2022-07-16 thomas fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1403 53d03f97 2023-01-10 thomas 0, NULL, repo, f3);
1405 8a35f56c 2022-07-16 thomas default:
1406 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_OBJ_TYPE);
1408 8a35f56c 2022-07-16 thomas if (error)
1409 8a35f56c 2022-07-16 thomas goto done;
1411 8a35f56c 2022-07-16 thomas if (fseek(f1, 0, SEEK_SET) == -1) {
1412 8a35f56c 2022-07-16 thomas error = got_ferror(f1, GOT_ERR_IO);
1413 8a35f56c 2022-07-16 thomas goto done;
1416 8a35f56c 2022-07-16 thomas if (fseek(f2, 0, SEEK_SET) == -1) {
1417 8a35f56c 2022-07-16 thomas error = got_ferror(f2, GOT_ERR_IO);
1418 8a35f56c 2022-07-16 thomas goto done;
1421 8a35f56c 2022-07-16 thomas if (fseek(f3, 0, SEEK_SET) == -1) {
1422 8a35f56c 2022-07-16 thomas error = got_ferror(f3, GOT_ERR_IO);
1423 8a35f56c 2022-07-16 thomas goto done;
1426 dccd05b4 2023-01-10 thomas *fp = f3;
1427 dccd05b4 2023-01-10 thomas *fd = fd3;
1430 8a35f56c 2022-07-16 thomas if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1431 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1432 8a35f56c 2022-07-16 thomas if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1433 8a35f56c 2022-07-16 thomas error = got_error_from_errno("close");
1434 8a35f56c 2022-07-16 thomas if (f1) {
1435 8a35f56c 2022-07-16 thomas const struct got_error *f1_err =
1436 8a35f56c 2022-07-16 thomas got_gotweb_flushfile(f1, fd1);
1437 8a35f56c 2022-07-16 thomas if (error == NULL)
1438 8a35f56c 2022-07-16 thomas error = f1_err;
1440 8a35f56c 2022-07-16 thomas if (f2) {
1441 8a35f56c 2022-07-16 thomas const struct got_error *f2_err =
1442 8a35f56c 2022-07-16 thomas got_gotweb_flushfile(f2, fd2);
1443 8a35f56c 2022-07-16 thomas if (error == NULL)
1444 8a35f56c 2022-07-16 thomas error = f2_err;
1446 dccd05b4 2023-01-10 thomas if (error && f3) {
1447 dccd05b4 2023-01-10 thomas got_gotweb_flushfile(f3, fd3);
1448 dccd05b4 2023-01-10 thomas *fp = NULL;
1449 dccd05b4 2023-01-10 thomas *fd = -1;
1451 8a35f56c 2022-07-16 thomas got_ref_list_free(&refs);
1452 8a35f56c 2022-07-16 thomas free(id1);
1453 8a35f56c 2022-07-16 thomas free(id2);
1454 8a35f56c 2022-07-16 thomas return error;
1457 8a35f56c 2022-07-16 thomas static const struct got_error *
1458 8a35f56c 2022-07-16 thomas got_init_repo_commit(struct repo_commit **rc)
1460 8a35f56c 2022-07-16 thomas *rc = calloc(1, sizeof(**rc));
1461 8a35f56c 2022-07-16 thomas if (*rc == NULL)
1462 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
1464 8a35f56c 2022-07-16 thomas (*rc)->path = NULL;
1465 8a35f56c 2022-07-16 thomas (*rc)->refs_str = NULL;
1466 8a35f56c 2022-07-16 thomas (*rc)->commit_id = NULL;
1467 8a35f56c 2022-07-16 thomas (*rc)->committer = NULL;
1468 8a35f56c 2022-07-16 thomas (*rc)->author = NULL;
1469 8a35f56c 2022-07-16 thomas (*rc)->parent_id = NULL;
1470 8a35f56c 2022-07-16 thomas (*rc)->tree_id = NULL;
1471 8a35f56c 2022-07-16 thomas (*rc)->commit_msg = NULL;
1473 e2af4cd7 2022-08-31 thomas return NULL;
1476 8a35f56c 2022-07-16 thomas static const struct got_error *
1477 8a35f56c 2022-07-16 thomas got_init_repo_tag(struct repo_tag **rt)
1479 8a35f56c 2022-07-16 thomas *rt = calloc(1, sizeof(**rt));
1480 8a35f56c 2022-07-16 thomas if (*rt == NULL)
1481 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
1483 8a35f56c 2022-07-16 thomas (*rt)->commit_id = NULL;
1484 8a35f56c 2022-07-16 thomas (*rt)->tag_name = NULL;
1485 8a35f56c 2022-07-16 thomas (*rt)->tag_commit = NULL;
1486 8a35f56c 2022-07-16 thomas (*rt)->commit_msg = NULL;
1487 8a35f56c 2022-07-16 thomas (*rt)->tagger = NULL;
1489 e2af4cd7 2022-08-31 thomas return NULL;