Blame


1 372ccdbb 2018-06-10 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 372ccdbb 2018-06-10 stsp *
4 372ccdbb 2018-06-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 372ccdbb 2018-06-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 372ccdbb 2018-06-10 stsp * copyright notice and this permission notice appear in all copies.
7 372ccdbb 2018-06-10 stsp *
8 372ccdbb 2018-06-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 372ccdbb 2018-06-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 372ccdbb 2018-06-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 372ccdbb 2018-06-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 372ccdbb 2018-06-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 372ccdbb 2018-06-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 372ccdbb 2018-06-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 372ccdbb 2018-06-10 stsp */
16 372ccdbb 2018-06-10 stsp
17 372ccdbb 2018-06-10 stsp #include <sys/types.h>
18 372ccdbb 2018-06-10 stsp #include <sys/stat.h>
19 372ccdbb 2018-06-10 stsp #include <sys/queue.h>
20 372ccdbb 2018-06-10 stsp #include <sys/stdint.h>
21 372ccdbb 2018-06-10 stsp
22 56e0773d 2019-11-28 stsp #include <limits.h>
23 372ccdbb 2018-06-10 stsp #include <stdio.h>
24 372ccdbb 2018-06-10 stsp #include <stdlib.h>
25 372ccdbb 2018-06-10 stsp #include <string.h>
26 372ccdbb 2018-06-10 stsp #include <sha1.h>
27 372ccdbb 2018-06-10 stsp #include <zlib.h>
28 372ccdbb 2018-06-10 stsp #include <ctype.h>
29 372ccdbb 2018-06-10 stsp
30 372ccdbb 2018-06-10 stsp #include "got_error.h"
31 372ccdbb 2018-06-10 stsp #include "got_object.h"
32 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
33 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
34 324d37e7 2019-05-11 stsp #include "got_path.h"
35 372ccdbb 2018-06-10 stsp
36 372ccdbb 2018-06-10 stsp #include "got_lib_delta.h"
37 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
38 372ccdbb 2018-06-10 stsp #include "got_lib_object.h"
39 372ccdbb 2018-06-10 stsp #include "got_lib_object_idset.h"
40 372ccdbb 2018-06-10 stsp
41 372ccdbb 2018-06-10 stsp struct got_commit_graph_node {
42 372ccdbb 2018-06-10 stsp struct got_object_id id;
43 b43fbaa0 2018-06-11 stsp
44 ca6e02ac 2020-01-07 stsp /* Used only during iteration. */
45 ca6e02ac 2020-01-07 stsp time_t timestamp;
46 372ccdbb 2018-06-10 stsp TAILQ_ENTRY(got_commit_graph_node) entry;
47 372ccdbb 2018-06-10 stsp };
48 372ccdbb 2018-06-10 stsp
49 9ba79e04 2018-06-11 stsp TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
50 9ba79e04 2018-06-11 stsp
51 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip {
52 32777563 2018-11-07 stsp struct got_object_id *commit_id;
53 32777563 2018-11-07 stsp struct got_commit_object *commit;
54 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
55 b565f6f8 2018-07-23 stsp };
56 b565f6f8 2018-07-23 stsp
57 372ccdbb 2018-06-10 stsp struct got_commit_graph {
58 372ccdbb 2018-06-10 stsp /* The set of all commits we have traversed. */
59 372ccdbb 2018-06-10 stsp struct got_object_idset *node_ids;
60 372ccdbb 2018-06-10 stsp
61 0ed6ed4c 2018-06-13 stsp int flags;
62 0ed6ed4c 2018-06-13 stsp #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
63 0ed6ed4c 2018-06-13 stsp
64 372ccdbb 2018-06-10 stsp /*
65 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
66 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
67 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
68 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
69 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
70 372ccdbb 2018-06-10 stsp *
71 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
72 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
73 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
74 372ccdbb 2018-06-10 stsp *
75 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
76 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
77 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
78 372ccdbb 2018-06-10 stsp */
79 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
80 b565f6f8 2018-07-23 stsp
81 32777563 2018-11-07 stsp /* Array of branch tips for fetch_commits_from_open_branches(). */
82 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
83 5e50c36a 2018-11-08 stsp int ntips;
84 372ccdbb 2018-06-10 stsp
85 31cedeaf 2018-09-15 stsp /* Path of tree entry of interest to the API user. */
86 31cedeaf 2018-09-15 stsp char *path;
87 31cedeaf 2018-09-15 stsp
88 94489f7d 2020-01-04 stsp /*
89 94489f7d 2020-01-04 stsp * Nodes which will be passed to the API user next, sorted by
90 94489f7d 2020-01-04 stsp * commit timestmap.
91 94489f7d 2020-01-04 stsp */
92 9ba79e04 2018-06-11 stsp struct got_commit_graph_iter_list iter_list;
93 372ccdbb 2018-06-10 stsp };
94 372ccdbb 2018-06-10 stsp
95 31cedeaf 2018-09-15 stsp static const struct got_error *
96 41fa1437 2018-11-05 stsp detect_changed_path(int *changed, struct got_commit_object *commit,
97 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
98 31cedeaf 2018-09-15 stsp struct got_repository *repo)
99 31cedeaf 2018-09-15 stsp {
100 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
101 41fa1437 2018-11-05 stsp struct got_commit_object *pcommit = NULL;
102 31cedeaf 2018-09-15 stsp struct got_tree_object *tree = NULL, *ptree = NULL;
103 31cedeaf 2018-09-15 stsp struct got_object_qid *pid;
104 31cedeaf 2018-09-15 stsp
105 31cedeaf 2018-09-15 stsp if (got_path_is_root_dir(path)) {
106 31cedeaf 2018-09-15 stsp *changed = 1;
107 31cedeaf 2018-09-15 stsp return NULL;
108 31cedeaf 2018-09-15 stsp }
109 31cedeaf 2018-09-15 stsp
110 31cedeaf 2018-09-15 stsp *changed = 0;
111 31cedeaf 2018-09-15 stsp
112 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
113 31cedeaf 2018-09-15 stsp if (pid == NULL) {
114 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
115 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, repo, commit, path);
116 fd1d2703 2018-11-04 stsp if (err) {
117 d1451975 2018-11-11 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
118 fd1d2703 2018-11-04 stsp err = NULL;
119 fd1d2703 2018-11-04 stsp } else
120 fd1d2703 2018-11-04 stsp *changed = 1; /* The path was created in this commit. */
121 31cedeaf 2018-09-15 stsp free(obj_id);
122 0e9101d5 2018-11-18 stsp return err;
123 0e9101d5 2018-11-18 stsp }
124 7310c1c3 2018-11-18 stsp
125 0e9101d5 2018-11-18 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
126 0e9101d5 2018-11-18 stsp if (err)
127 0e9101d5 2018-11-18 stsp return err;
128 372ccdbb 2018-06-10 stsp
129 d7b5a0e8 2022-04-20 stsp err = got_object_open_as_commit(&pcommit, repo, &pid->id);
130 0e9101d5 2018-11-18 stsp if (err)
131 0e9101d5 2018-11-18 stsp goto done;
132 0e9101d5 2018-11-18 stsp
133 0e9101d5 2018-11-18 stsp err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
134 0e9101d5 2018-11-18 stsp if (err)
135 0e9101d5 2018-11-18 stsp goto done;
136 0e9101d5 2018-11-18 stsp
137 81a966c0 2018-11-18 stsp err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
138 31cedeaf 2018-09-15 stsp done:
139 31cedeaf 2018-09-15 stsp if (tree)
140 31cedeaf 2018-09-15 stsp got_object_tree_close(tree);
141 31cedeaf 2018-09-15 stsp if (ptree)
142 31cedeaf 2018-09-15 stsp got_object_tree_close(ptree);
143 31cedeaf 2018-09-15 stsp if (pcommit)
144 41fa1437 2018-11-05 stsp got_object_commit_close(pcommit);
145 31cedeaf 2018-09-15 stsp return err;
146 31cedeaf 2018-09-15 stsp }
147 31cedeaf 2018-09-15 stsp
148 4626e416 2018-06-10 stsp static void
149 9ba79e04 2018-06-11 stsp add_node_to_iter_list(struct got_commit_graph *graph,
150 ca6e02ac 2020-01-07 stsp struct got_commit_graph_node *node, time_t committer_time)
151 372ccdbb 2018-06-10 stsp {
152 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
153 1c7a5dcb 2018-09-15 stsp
154 ca6e02ac 2020-01-07 stsp node->timestamp = committer_time;
155 ca6e02ac 2020-01-07 stsp
156 94489f7d 2020-01-04 stsp n = TAILQ_FIRST(&graph->iter_list);
157 bee6b577 2018-09-19 stsp while (n) {
158 bee6b577 2018-09-19 stsp next = TAILQ_NEXT(n, entry);
159 73026088 2018-11-18 stsp if (next && node->timestamp >= next->timestamp) {
160 bee6b577 2018-09-19 stsp TAILQ_INSERT_BEFORE(next, node, entry);
161 bee6b577 2018-09-19 stsp return;
162 fe8df4c2 2018-06-16 stsp }
163 bee6b577 2018-09-19 stsp n = next;
164 fe8df4c2 2018-06-16 stsp }
165 93e45b7c 2018-09-24 stsp TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
166 0ed6ed4c 2018-06-13 stsp }
167 0ed6ed4c 2018-06-13 stsp
168 0ed6ed4c 2018-06-13 stsp static const struct got_error *
169 ca6e02ac 2020-01-07 stsp add_node(struct got_commit_graph_node **new_node,
170 ca6e02ac 2020-01-07 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
171 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
172 ca6e02ac 2020-01-07 stsp {
173 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
174 ca6e02ac 2020-01-07 stsp struct got_commit_graph_node *node;
175 ca6e02ac 2020-01-07 stsp
176 ca6e02ac 2020-01-07 stsp *new_node = NULL;
177 ca6e02ac 2020-01-07 stsp
178 ca6e02ac 2020-01-07 stsp node = calloc(1, sizeof(*node));
179 ca6e02ac 2020-01-07 stsp if (node == NULL)
180 ca6e02ac 2020-01-07 stsp return got_error_from_errno("calloc");
181 ca6e02ac 2020-01-07 stsp
182 ca6e02ac 2020-01-07 stsp memcpy(&node->id, commit_id, sizeof(node->id));
183 ca6e02ac 2020-01-07 stsp err = got_object_idset_add(graph->node_ids, &node->id, NULL);
184 ca6e02ac 2020-01-07 stsp if (err)
185 ca6e02ac 2020-01-07 stsp free(node);
186 ca6e02ac 2020-01-07 stsp else
187 ca6e02ac 2020-01-07 stsp *new_node = node;
188 ca6e02ac 2020-01-07 stsp return err;
189 ca6e02ac 2020-01-07 stsp }
190 ca6e02ac 2020-01-07 stsp
191 ca6e02ac 2020-01-07 stsp /*
192 ca6e02ac 2020-01-07 stsp * Ask got-read-pack to traverse first-parent history until a commit is
193 ca6e02ac 2020-01-07 stsp * encountered which modified graph->path, or until the pack file runs
194 ca6e02ac 2020-01-07 stsp * out of relevant commits. This is faster than sending an individual
195 ca6e02ac 2020-01-07 stsp * request for each commit stored in the pack file.
196 ca6e02ac 2020-01-07 stsp */
197 ca6e02ac 2020-01-07 stsp static const struct got_error *
198 ca6e02ac 2020-01-07 stsp packed_first_parent_traversal(int *ncommits_traversed,
199 ca6e02ac 2020-01-07 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
200 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
201 ca6e02ac 2020-01-07 stsp {
202 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
203 ca6e02ac 2020-01-07 stsp struct got_object_id_queue traversed_commits;
204 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
205 ca6e02ac 2020-01-07 stsp
206 dbdddfee 2021-06-23 naddy STAILQ_INIT(&traversed_commits);
207 ca6e02ac 2020-01-07 stsp *ncommits_traversed = 0;
208 ca6e02ac 2020-01-07 stsp
209 ca6e02ac 2020-01-07 stsp err = got_traverse_packed_commits(&traversed_commits,
210 ca6e02ac 2020-01-07 stsp commit_id, graph->path, repo);
211 ca6e02ac 2020-01-07 stsp if (err)
212 ca6e02ac 2020-01-07 stsp return err;
213 ca6e02ac 2020-01-07 stsp
214 ca6e02ac 2020-01-07 stsp /* Add all traversed commits to the graph... */
215 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &traversed_commits, entry) {
216 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(graph->open_branches, &qid->id))
217 ca6e02ac 2020-01-07 stsp continue;
218 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(graph->node_ids, &qid->id))
219 ca6e02ac 2020-01-07 stsp continue;
220 ca6e02ac 2020-01-07 stsp
221 ca6e02ac 2020-01-07 stsp (*ncommits_traversed)++;
222 ca6e02ac 2020-01-07 stsp
223 ca6e02ac 2020-01-07 stsp /* ... except the last commit is the new branch tip. */
224 dbdddfee 2021-06-23 naddy if (STAILQ_NEXT(qid, entry) == NULL) {
225 ca6e02ac 2020-01-07 stsp err = got_object_idset_add(graph->open_branches,
226 d7b5a0e8 2022-04-20 stsp &qid->id, NULL);
227 ca6e02ac 2020-01-07 stsp break;
228 ca6e02ac 2020-01-07 stsp }
229 ca6e02ac 2020-01-07 stsp
230 932b646a 2022-09-05 stsp err = got_object_idset_add(graph->node_ids, &qid->id, NULL);
231 ca6e02ac 2020-01-07 stsp if (err)
232 ca6e02ac 2020-01-07 stsp break;
233 ca6e02ac 2020-01-07 stsp }
234 ca6e02ac 2020-01-07 stsp
235 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(&traversed_commits);
236 ca6e02ac 2020-01-07 stsp return err;
237 ca6e02ac 2020-01-07 stsp }
238 ca6e02ac 2020-01-07 stsp
239 ca6e02ac 2020-01-07 stsp static const struct got_error *
240 32c85d2c 2020-01-06 stsp close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
241 32c85d2c 2020-01-06 stsp {
242 32c85d2c 2020-01-06 stsp const struct got_error *err;
243 32c85d2c 2020-01-06 stsp
244 32c85d2c 2020-01-06 stsp err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
245 32c85d2c 2020-01-06 stsp if (err && err->code != GOT_ERR_NO_OBJ)
246 32c85d2c 2020-01-06 stsp return err;
247 32c85d2c 2020-01-06 stsp return NULL;
248 32c85d2c 2020-01-06 stsp }
249 32c85d2c 2020-01-06 stsp
250 32c85d2c 2020-01-06 stsp static const struct got_error *
251 14159a7b 2020-01-04 stsp advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
252 14159a7b 2020-01-04 stsp struct got_commit_object *commit, struct got_repository *repo)
253 0ed6ed4c 2018-06-13 stsp {
254 0ed6ed4c 2018-06-13 stsp const struct got_error *err;
255 0ed6ed4c 2018-06-13 stsp struct got_object_qid *qid;
256 0ed6ed4c 2018-06-13 stsp
257 32c85d2c 2020-01-06 stsp err = close_branch(graph, commit_id);
258 32c85d2c 2020-01-06 stsp if (err)
259 32c85d2c 2020-01-06 stsp return err;
260 32c85d2c 2020-01-06 stsp
261 0ed6ed4c 2018-06-13 stsp if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
262 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&commit->parent_ids);
263 9b88e78c 2018-11-18 stsp if (qid == NULL ||
264 d7b5a0e8 2022-04-20 stsp got_object_idset_contains(graph->open_branches, &qid->id))
265 0ed6ed4c 2018-06-13 stsp return NULL;
266 ca6e02ac 2020-01-07 stsp /*
267 ca6e02ac 2020-01-07 stsp * The root directory always changes by definition, and when
268 ca6e02ac 2020-01-07 stsp * logging the root we want to traverse consecutive commits
269 ca6e02ac 2020-01-07 stsp * even if they point at the same tree.
270 ca6e02ac 2020-01-07 stsp * But if we are looking for a specific path then we can avoid
271 ca6e02ac 2020-01-07 stsp * fetching packed commits which did not modify the path and
272 ca6e02ac 2020-01-07 stsp * only fetch their IDs. This speeds up 'got blame'.
273 ca6e02ac 2020-01-07 stsp */
274 ca6e02ac 2020-01-07 stsp if (!got_path_is_root_dir(graph->path) &&
275 ca6e02ac 2020-01-07 stsp (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
276 ca6e02ac 2020-01-07 stsp int ncommits = 0;
277 ca6e02ac 2020-01-07 stsp err = packed_first_parent_traversal(&ncommits,
278 d7b5a0e8 2022-04-20 stsp graph, &qid->id, repo);
279 ca6e02ac 2020-01-07 stsp if (err || ncommits > 0)
280 ca6e02ac 2020-01-07 stsp return err;
281 ca6e02ac 2020-01-07 stsp }
282 9b88e78c 2018-11-18 stsp return got_object_idset_add(graph->open_branches,
283 d7b5a0e8 2022-04-20 stsp &qid->id, NULL);
284 bee6b577 2018-09-19 stsp }
285 bee6b577 2018-09-19 stsp
286 bee6b577 2018-09-19 stsp /*
287 bee6b577 2018-09-19 stsp * If we are graphing commits for a specific path, skip branches
288 bee6b577 2018-09-19 stsp * which do not contribute any content to this path.
289 bee6b577 2018-09-19 stsp */
290 7a62478b 2018-11-18 stsp if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
291 6dcdad08 2018-11-18 stsp struct got_object_id *merged_id, *prev_id = NULL;
292 bee6b577 2018-09-19 stsp int branches_differ = 0;
293 bee6b577 2018-09-19 stsp
294 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&merged_id, repo, commit,
295 bee6b577 2018-09-19 stsp graph->path);
296 bee6b577 2018-09-19 stsp if (err)
297 bee6b577 2018-09-19 stsp return err;
298 bee6b577 2018-09-19 stsp
299 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
300 a44927cc 2022-04-07 stsp struct got_object_id *id = NULL;
301 a44927cc 2022-04-07 stsp struct got_commit_object *pcommit = NULL;
302 6dcdad08 2018-11-18 stsp
303 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->open_branches,
304 d7b5a0e8 2022-04-20 stsp &qid->id))
305 9b88e78c 2018-11-18 stsp continue;
306 bee6b577 2018-09-19 stsp
307 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit, repo,
308 d7b5a0e8 2022-04-20 stsp &qid->id);
309 a44927cc 2022-04-07 stsp if (err) {
310 a44927cc 2022-04-07 stsp free(merged_id);
311 a44927cc 2022-04-07 stsp free(prev_id);
312 a44927cc 2022-04-07 stsp return err;
313 a44927cc 2022-04-07 stsp }
314 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, pcommit,
315 bee6b577 2018-09-19 stsp graph->path);
316 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
317 a44927cc 2022-04-07 stsp pcommit = NULL;
318 bee6b577 2018-09-19 stsp if (err) {
319 d1451975 2018-11-11 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
320 bee6b577 2018-09-19 stsp branches_differ = 1;
321 bee6b577 2018-09-19 stsp continue;
322 bee6b577 2018-09-19 stsp }
323 6dcdad08 2018-11-18 stsp free(merged_id);
324 6dcdad08 2018-11-18 stsp free(prev_id);
325 bee6b577 2018-09-19 stsp return err;
326 bee6b577 2018-09-19 stsp }
327 bee6b577 2018-09-19 stsp
328 bee6b577 2018-09-19 stsp if (prev_id) {
329 bee6b577 2018-09-19 stsp if (!branches_differ &&
330 d2c2d781 2018-11-18 stsp got_object_id_cmp(id, prev_id) != 0)
331 bee6b577 2018-09-19 stsp branches_differ = 1;
332 6dcdad08 2018-11-18 stsp free(prev_id);
333 6dcdad08 2018-11-18 stsp }
334 6dcdad08 2018-11-18 stsp prev_id = id;
335 bee6b577 2018-09-19 stsp
336 bee6b577 2018-09-19 stsp /*
337 bee6b577 2018-09-19 stsp * If a branch has created the merged content we can
338 bee6b577 2018-09-19 stsp * skip any other branches.
339 bee6b577 2018-09-19 stsp */
340 bee6b577 2018-09-19 stsp if (got_object_id_cmp(merged_id, id) == 0) {
341 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches,
342 d7b5a0e8 2022-04-20 stsp &qid->id, NULL);
343 6dcdad08 2018-11-18 stsp free(merged_id);
344 6dcdad08 2018-11-18 stsp free(id);
345 b36429ab 2018-11-05 stsp return err;
346 bee6b577 2018-09-19 stsp }
347 bee6b577 2018-09-19 stsp }
348 6dcdad08 2018-11-18 stsp
349 6dcdad08 2018-11-18 stsp free(prev_id);
350 6dcdad08 2018-11-18 stsp prev_id = NULL;
351 6dcdad08 2018-11-18 stsp free(merged_id);
352 6dcdad08 2018-11-18 stsp merged_id = NULL;
353 bee6b577 2018-09-19 stsp
354 bee6b577 2018-09-19 stsp /*
355 bee6b577 2018-09-19 stsp * If the path's content is the same on all branches,
356 bee6b577 2018-09-19 stsp * follow the first parent only.
357 bee6b577 2018-09-19 stsp */
358 bee6b577 2018-09-19 stsp if (!branches_differ) {
359 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&commit->parent_ids);
360 bee6b577 2018-09-19 stsp if (qid == NULL)
361 bee6b577 2018-09-19 stsp return NULL;
362 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->open_branches,
363 d7b5a0e8 2022-04-20 stsp &qid->id))
364 b36429ab 2018-11-05 stsp return NULL;
365 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->node_ids,
366 d7b5a0e8 2022-04-20 stsp &qid->id))
367 998ff57f 2018-11-18 stsp return NULL; /* parent already traversed */
368 b36429ab 2018-11-05 stsp return got_object_idset_add(graph->open_branches,
369 d7b5a0e8 2022-04-20 stsp &qid->id, NULL);
370 bee6b577 2018-09-19 stsp }
371 0ed6ed4c 2018-06-13 stsp }
372 0ed6ed4c 2018-06-13 stsp
373 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
374 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(graph->open_branches, &qid->id))
375 b36429ab 2018-11-05 stsp continue;
376 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(graph->node_ids, &qid->id))
377 998ff57f 2018-11-18 stsp continue; /* parent already traversed */
378 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(graph->open_branches, &qid->id,
379 d7b5a0e8 2022-04-20 stsp NULL);
380 b36429ab 2018-11-05 stsp if (err)
381 0ed6ed4c 2018-06-13 stsp return err;
382 0ed6ed4c 2018-06-13 stsp }
383 0ed6ed4c 2018-06-13 stsp
384 b43fbaa0 2018-06-11 stsp return NULL;
385 de56b2d7 2020-01-04 stsp }
386 372ccdbb 2018-06-10 stsp
387 de56b2d7 2020-01-04 stsp const struct got_error *
388 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
389 3d509237 2020-01-04 stsp const char *path, int first_parent_traversal)
390 3d509237 2020-01-04 stsp {
391 22220781 2020-01-04 stsp const struct got_error *err = NULL;
392 3ddcebf3 2020-01-04 stsp
393 3ddcebf3 2020-01-04 stsp *graph = calloc(1, sizeof(**graph));
394 3d509237 2020-01-04 stsp if (*graph == NULL)
395 3ddcebf3 2020-01-04 stsp return got_error_from_errno("calloc");
396 88cdb9c6 2020-01-04 stsp
397 88cdb9c6 2020-01-04 stsp TAILQ_INIT(&(*graph)->iter_list);
398 3ddcebf3 2020-01-04 stsp
399 3ddcebf3 2020-01-04 stsp (*graph)->path = strdup(path);
400 3ddcebf3 2020-01-04 stsp if ((*graph)->path == NULL) {
401 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("strdup");
402 22220781 2020-01-04 stsp goto done;
403 3ddcebf3 2020-01-04 stsp }
404 3ddcebf3 2020-01-04 stsp
405 3ddcebf3 2020-01-04 stsp (*graph)->node_ids = got_object_idset_alloc();
406 3ddcebf3 2020-01-04 stsp if ((*graph)->node_ids == NULL) {
407 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("got_object_idset_alloc");
408 22220781 2020-01-04 stsp goto done;
409 3ddcebf3 2020-01-04 stsp }
410 372ccdbb 2018-06-10 stsp
411 3ddcebf3 2020-01-04 stsp (*graph)->open_branches = got_object_idset_alloc();
412 3ddcebf3 2020-01-04 stsp if ((*graph)->open_branches == NULL) {
413 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("got_object_idset_alloc");
414 22220781 2020-01-04 stsp goto done;
415 3ddcebf3 2020-01-04 stsp }
416 3ddcebf3 2020-01-04 stsp
417 0ed6ed4c 2018-06-13 stsp if (first_parent_traversal)
418 0ed6ed4c 2018-06-13 stsp (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
419 22220781 2020-01-04 stsp done:
420 22220781 2020-01-04 stsp if (err) {
421 22220781 2020-01-04 stsp got_commit_graph_close(*graph);
422 22220781 2020-01-04 stsp *graph = NULL;
423 22220781 2020-01-04 stsp }
424 22220781 2020-01-04 stsp return err;
425 372ccdbb 2018-06-10 stsp }
426 372ccdbb 2018-06-10 stsp
427 32777563 2018-11-07 stsp struct add_branch_tip_arg {
428 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
429 b565f6f8 2018-07-23 stsp int ntips;
430 32777563 2018-11-07 stsp struct got_repository *repo;
431 32777563 2018-11-07 stsp struct got_commit_graph *graph;
432 372ccdbb 2018-06-10 stsp };
433 372ccdbb 2018-06-10 stsp
434 cb103d04 2018-11-07 stsp static const struct got_error *
435 32777563 2018-11-07 stsp add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
436 372ccdbb 2018-06-10 stsp {
437 32777563 2018-11-07 stsp const struct got_error *err;
438 32777563 2018-11-07 stsp struct add_branch_tip_arg *a = arg;
439 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
440 32777563 2018-11-07 stsp struct got_commit_object *commit;
441 32777563 2018-11-07 stsp
442 32777563 2018-11-07 stsp err = got_object_open_as_commit(&commit, a->repo, commit_id);
443 32777563 2018-11-07 stsp if (err)
444 32777563 2018-11-07 stsp return err;
445 32777563 2018-11-07 stsp
446 ca6e02ac 2020-01-07 stsp err = add_node(&new_node, a->graph, commit_id, a->repo);
447 c877c437 2022-09-04 op if (err) {
448 c877c437 2022-09-04 op got_object_commit_close(commit);
449 32777563 2018-11-07 stsp return err;
450 c877c437 2022-09-04 op }
451 32777563 2018-11-07 stsp
452 c877c437 2022-09-04 op a->tips[a->ntips].commit_id = &new_node->id;
453 32777563 2018-11-07 stsp a->tips[a->ntips].commit = commit;
454 32777563 2018-11-07 stsp a->tips[a->ntips].new_node = new_node;
455 b565f6f8 2018-07-23 stsp a->ntips++;
456 32777563 2018-11-07 stsp
457 cb103d04 2018-11-07 stsp return NULL;
458 372ccdbb 2018-06-10 stsp }
459 372ccdbb 2018-06-10 stsp
460 1142eae9 2018-06-11 stsp static const struct got_error *
461 57eecd46 2020-01-04 stsp fetch_commits_from_open_branches(struct got_commit_graph *graph,
462 6fb7cd11 2019-08-22 stsp struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
463 372ccdbb 2018-06-10 stsp {
464 372ccdbb 2018-06-10 stsp const struct got_error *err;
465 32777563 2018-11-07 stsp struct add_branch_tip_arg arg;
466 32777563 2018-11-07 stsp int i, ntips;
467 372ccdbb 2018-06-10 stsp
468 32777563 2018-11-07 stsp ntips = got_object_idset_num_elements(graph->open_branches);
469 32777563 2018-11-07 stsp if (ntips == 0)
470 372ccdbb 2018-06-10 stsp return NULL;
471 372ccdbb 2018-06-10 stsp
472 32777563 2018-11-07 stsp /* (Re-)allocate branch tips array if necessary. */
473 32777563 2018-11-07 stsp if (graph->ntips < ntips) {
474 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
475 5e50c36a 2018-11-08 stsp tips = recallocarray(graph->tips, graph->ntips, ntips,
476 5e50c36a 2018-11-08 stsp sizeof(*tips));
477 b565f6f8 2018-07-23 stsp if (tips == NULL)
478 638f9024 2019-05-13 stsp return got_error_from_errno("recallocarray");
479 b565f6f8 2018-07-23 stsp graph->tips = tips;
480 32777563 2018-11-07 stsp graph->ntips = ntips;
481 b565f6f8 2018-07-23 stsp }
482 b565f6f8 2018-07-23 stsp arg.tips = graph->tips;
483 32777563 2018-11-07 stsp arg.ntips = 0; /* add_branch_tip() will increment */
484 32777563 2018-11-07 stsp arg.repo = repo;
485 32777563 2018-11-07 stsp arg.graph = graph;
486 32777563 2018-11-07 stsp err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
487 32777563 2018-11-07 stsp &arg);
488 cb103d04 2018-11-07 stsp if (err)
489 32777563 2018-11-07 stsp goto done;
490 372ccdbb 2018-06-10 stsp
491 b565f6f8 2018-07-23 stsp for (i = 0; i < arg.ntips; i++) {
492 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
493 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
494 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
495 13a851c1 2020-01-04 stsp int changed;
496 372ccdbb 2018-06-10 stsp
497 6fb7cd11 2019-08-22 stsp if (cancel_cb) {
498 6fb7cd11 2019-08-22 stsp err = (*cancel_cb)(cancel_arg);
499 6fb7cd11 2019-08-22 stsp if (err)
500 6fb7cd11 2019-08-22 stsp break;
501 6fb7cd11 2019-08-22 stsp }
502 6fb7cd11 2019-08-22 stsp
503 32777563 2018-11-07 stsp commit_id = arg.tips[i].commit_id;
504 32777563 2018-11-07 stsp commit = arg.tips[i].commit;
505 32777563 2018-11-07 stsp new_node = arg.tips[i].new_node;
506 de56b2d7 2020-01-04 stsp
507 13a851c1 2020-01-04 stsp err = detect_changed_path(&changed, commit, commit_id,
508 13a851c1 2020-01-04 stsp graph->path, repo);
509 13a851c1 2020-01-04 stsp if (err) {
510 13a851c1 2020-01-04 stsp if (err->code != GOT_ERR_NO_OBJ)
511 13a851c1 2020-01-04 stsp break;
512 13a851c1 2020-01-04 stsp /*
513 13a851c1 2020-01-04 stsp * History of the path stops here on the current
514 13a851c1 2020-01-04 stsp * branch. Keep going on other branches.
515 13a851c1 2020-01-04 stsp */
516 32c85d2c 2020-01-06 stsp err = close_branch(graph, commit_id);
517 32c85d2c 2020-01-06 stsp if (err)
518 32c85d2c 2020-01-06 stsp break;
519 ec1904dc 2020-01-04 stsp continue;
520 13a851c1 2020-01-04 stsp }
521 85a99d1e 2022-09-04 op if (changed) {
522 ca6e02ac 2020-01-07 stsp add_node_to_iter_list(graph, new_node,
523 ca6e02ac 2020-01-07 stsp got_object_commit_get_committer_time(commit));
524 85a99d1e 2022-09-04 op arg.tips[i].new_node = NULL;
525 85a99d1e 2022-09-04 op }
526 14159a7b 2020-01-04 stsp err = advance_branch(graph, commit_id, commit, repo);
527 cb352812 2018-07-22 stsp if (err)
528 cb352812 2018-07-22 stsp break;
529 372ccdbb 2018-06-10 stsp }
530 32777563 2018-11-07 stsp done:
531 85a99d1e 2022-09-04 op for (i = 0; i < arg.ntips; i++) {
532 32777563 2018-11-07 stsp got_object_commit_close(arg.tips[i].commit);
533 85a99d1e 2022-09-04 op free(arg.tips[i].new_node);
534 85a99d1e 2022-09-04 op }
535 372ccdbb 2018-06-10 stsp return err;
536 372ccdbb 2018-06-10 stsp }
537 372ccdbb 2018-06-10 stsp
538 372ccdbb 2018-06-10 stsp void
539 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
540 372ccdbb 2018-06-10 stsp {
541 9aae7366 2022-09-04 op struct got_commit_graph_node *node;
542 9aae7366 2022-09-04 op
543 9aae7366 2022-09-04 op while ((node = TAILQ_FIRST(&graph->iter_list))) {
544 9aae7366 2022-09-04 op TAILQ_REMOVE(&graph->iter_list, node, entry);
545 9aae7366 2022-09-04 op free(node);
546 9aae7366 2022-09-04 op }
547 9aae7366 2022-09-04 op
548 22220781 2020-01-04 stsp if (graph->open_branches)
549 22220781 2020-01-04 stsp got_object_idset_free(graph->open_branches);
550 22220781 2020-01-04 stsp if (graph->node_ids)
551 22220781 2020-01-04 stsp got_object_idset_free(graph->node_ids);
552 b565f6f8 2018-07-23 stsp free(graph->tips);
553 31cedeaf 2018-09-15 stsp free(graph->path);
554 372ccdbb 2018-06-10 stsp free(graph);
555 372ccdbb 2018-06-10 stsp }
556 372ccdbb 2018-06-10 stsp
557 372ccdbb 2018-06-10 stsp const struct got_error *
558 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
559 6fb7cd11 2019-08-22 stsp struct got_object_id *id, struct got_repository *repo,
560 6fb7cd11 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
561 372ccdbb 2018-06-10 stsp {
562 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
563 372ccdbb 2018-06-10 stsp
564 3d509237 2020-01-04 stsp if (!TAILQ_EMPTY(&graph->iter_list))
565 3d509237 2020-01-04 stsp return got_error(GOT_ERR_ITER_BUSY);
566 31cedeaf 2018-09-15 stsp
567 3ff3126d 2020-01-04 stsp err = got_object_idset_add(graph->open_branches, id, NULL);
568 3d509237 2020-01-04 stsp if (err)
569 7e33c8c5 2020-01-04 stsp return err;
570 3d509237 2020-01-04 stsp
571 3ff3126d 2020-01-04 stsp /* Locate first commit which changed graph->path. */
572 94489f7d 2020-01-04 stsp while (TAILQ_EMPTY(&graph->iter_list) &&
573 3ff3126d 2020-01-04 stsp got_object_idset_num_elements(graph->open_branches) > 0) {
574 3ff3126d 2020-01-04 stsp err = fetch_commits_from_open_branches(graph, repo,
575 3ff3126d 2020-01-04 stsp cancel_cb, cancel_arg);
576 3ff3126d 2020-01-04 stsp if (err)
577 7e33c8c5 2020-01-04 stsp return err;
578 5175b31a 2020-01-04 stsp }
579 5175b31a 2020-01-04 stsp
580 94489f7d 2020-01-04 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
581 5175b31a 2020-01-04 stsp const char *path;
582 5175b31a 2020-01-04 stsp if (got_path_is_root_dir(graph->path))
583 5175b31a 2020-01-04 stsp return got_error_no_obj(id);
584 5175b31a 2020-01-04 stsp path = graph->path;
585 5175b31a 2020-01-04 stsp while (path[0] == '/')
586 5175b31a 2020-01-04 stsp path++;
587 5175b31a 2020-01-04 stsp return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
588 31cedeaf 2018-09-15 stsp }
589 7e33c8c5 2020-01-04 stsp
590 7e33c8c5 2020-01-04 stsp return NULL;
591 372ccdbb 2018-06-10 stsp }
592 372ccdbb 2018-06-10 stsp
593 372ccdbb 2018-06-10 stsp const struct got_error *
594 d9787ed8 2022-09-10 op got_commit_graph_iter_next(struct got_object_id *id,
595 ee780d5c 2020-01-04 stsp struct got_commit_graph *graph, struct got_repository *repo,
596 ee780d5c 2020-01-04 stsp got_cancel_cb cancel_cb, void *cancel_arg)
597 372ccdbb 2018-06-10 stsp {
598 ee780d5c 2020-01-04 stsp const struct got_error *err = NULL;
599 94489f7d 2020-01-04 stsp struct got_commit_graph_node *node;
600 372ccdbb 2018-06-10 stsp
601 df8cd9c6 2020-01-05 stsp node = TAILQ_FIRST(&graph->iter_list);
602 df8cd9c6 2020-01-05 stsp if (node == NULL) {
603 2c7f8870 2018-09-19 stsp /* We are done iterating, or iteration was not started. */
604 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
605 9ba79e04 2018-06-11 stsp }
606 9ba79e04 2018-06-11 stsp
607 94489f7d 2020-01-04 stsp while (TAILQ_NEXT(node, entry) == NULL &&
608 ee780d5c 2020-01-04 stsp got_object_idset_num_elements(graph->open_branches) > 0) {
609 57eecd46 2020-01-04 stsp err = fetch_commits_from_open_branches(graph, repo,
610 57eecd46 2020-01-04 stsp cancel_cb, cancel_arg);
611 ee780d5c 2020-01-04 stsp if (err)
612 ee780d5c 2020-01-04 stsp return err;
613 ee780d5c 2020-01-04 stsp }
614 372ccdbb 2018-06-10 stsp
615 d9787ed8 2022-09-10 op memcpy(id, &node->id, sizeof(*id));
616 e15c42de 2022-09-05 op
617 94489f7d 2020-01-04 stsp TAILQ_REMOVE(&graph->iter_list, node, entry);
618 e15c42de 2022-09-05 op free(node);
619 372ccdbb 2018-06-10 stsp return NULL;
620 d9787ed8 2022-09-10 op }
621 d9787ed8 2022-09-10 op
622 d9787ed8 2022-09-10 op static const struct got_error *
623 d9787ed8 2022-09-10 op find_yca_add_id(struct got_object_id **yca_id, struct got_commit_graph *graph,
624 d9787ed8 2022-09-10 op struct got_object_idset *commit_ids, struct got_repository *repo,
625 d9787ed8 2022-09-10 op got_cancel_cb cancel_cb, void *cancel_arg)
626 d9787ed8 2022-09-10 op {
627 d9787ed8 2022-09-10 op const struct got_error *err = NULL;
628 d9787ed8 2022-09-10 op struct got_object_id id;
629 d9787ed8 2022-09-10 op
630 d9787ed8 2022-09-10 op err = got_commit_graph_iter_next(&id, graph, repo, cancel_cb,
631 d9787ed8 2022-09-10 op cancel_arg);
632 d9787ed8 2022-09-10 op if (err)
633 d9787ed8 2022-09-10 op return err;
634 d9787ed8 2022-09-10 op
635 d9787ed8 2022-09-10 op if (got_object_idset_contains(commit_ids, &id)) {
636 d9787ed8 2022-09-10 op *yca_id = got_object_id_dup(&id);
637 d9787ed8 2022-09-10 op if (*yca_id == NULL)
638 d9787ed8 2022-09-10 op err = got_error_from_errno("got_object_id_dup");
639 d9787ed8 2022-09-10 op return err;
640 d9787ed8 2022-09-10 op }
641 d9787ed8 2022-09-10 op
642 d9787ed8 2022-09-10 op return got_object_idset_add(commit_ids, &id, NULL);
643 372ccdbb 2018-06-10 stsp }
644 a9833bc9 2019-05-13 stsp
645 a9833bc9 2019-05-13 stsp const struct got_error *
646 a9833bc9 2019-05-13 stsp got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
647 a9833bc9 2019-05-13 stsp struct got_object_id *commit_id, struct got_object_id *commit_id2,
648 4e91ef15 2021-09-26 stsp int first_parent_traversal,
649 6fb7cd11 2019-08-22 stsp struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
650 a9833bc9 2019-05-13 stsp {
651 a9833bc9 2019-05-13 stsp const struct got_error *err = NULL;
652 a9833bc9 2019-05-13 stsp struct got_commit_graph *graph = NULL, *graph2 = NULL;
653 a9833bc9 2019-05-13 stsp int completed = 0, completed2 = 0;
654 a9833bc9 2019-05-13 stsp struct got_object_idset *commit_ids;
655 a9833bc9 2019-05-13 stsp
656 a9833bc9 2019-05-13 stsp *yca_id = NULL;
657 a9833bc9 2019-05-13 stsp
658 a9833bc9 2019-05-13 stsp commit_ids = got_object_idset_alloc();
659 a9833bc9 2019-05-13 stsp if (commit_ids == NULL)
660 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_idset_alloc");
661 a9833bc9 2019-05-13 stsp
662 4e91ef15 2021-09-26 stsp err = got_commit_graph_open(&graph, "/", first_parent_traversal);
663 a9833bc9 2019-05-13 stsp if (err)
664 a9833bc9 2019-05-13 stsp goto done;
665 a9833bc9 2019-05-13 stsp
666 4e91ef15 2021-09-26 stsp err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
667 a9833bc9 2019-05-13 stsp if (err)
668 a9833bc9 2019-05-13 stsp goto done;
669 a9833bc9 2019-05-13 stsp
670 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, commit_id, repo,
671 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
672 a9833bc9 2019-05-13 stsp if (err)
673 a9833bc9 2019-05-13 stsp goto done;
674 a9833bc9 2019-05-13 stsp
675 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph2, commit_id2, repo,
676 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
677 a9833bc9 2019-05-13 stsp if (err)
678 a9833bc9 2019-05-13 stsp goto done;
679 a9833bc9 2019-05-13 stsp
680 a9833bc9 2019-05-13 stsp for (;;) {
681 6fb7cd11 2019-08-22 stsp if (cancel_cb) {
682 6fb7cd11 2019-08-22 stsp err = (*cancel_cb)(cancel_arg);
683 6fb7cd11 2019-08-22 stsp if (err)
684 6fb7cd11 2019-08-22 stsp break;
685 6fb7cd11 2019-08-22 stsp }
686 6fb7cd11 2019-08-22 stsp
687 a9833bc9 2019-05-13 stsp if (!completed) {
688 d9787ed8 2022-09-10 op err = find_yca_add_id(yca_id, graph, commit_ids, repo,
689 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
690 a9833bc9 2019-05-13 stsp if (err) {
691 ee780d5c 2020-01-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
692 a9833bc9 2019-05-13 stsp break;
693 ee780d5c 2020-01-04 stsp err = NULL;
694 ee780d5c 2020-01-04 stsp completed = 1;
695 a9833bc9 2019-05-13 stsp }
696 d9787ed8 2022-09-10 op if (*yca_id)
697 d9787ed8 2022-09-10 op break;
698 a9833bc9 2019-05-13 stsp }
699 a9833bc9 2019-05-13 stsp
700 a9833bc9 2019-05-13 stsp if (!completed2) {
701 d9787ed8 2022-09-10 op err = find_yca_add_id(yca_id, graph2, commit_ids, repo,
702 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
703 a9833bc9 2019-05-13 stsp if (err) {
704 ee780d5c 2020-01-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
705 a9833bc9 2019-05-13 stsp break;
706 ee780d5c 2020-01-04 stsp err = NULL;
707 ee780d5c 2020-01-04 stsp completed2 = 1;
708 a9833bc9 2019-05-13 stsp }
709 d9787ed8 2022-09-10 op if (*yca_id)
710 a9833bc9 2019-05-13 stsp break;
711 a9833bc9 2019-05-13 stsp }
712 a9833bc9 2019-05-13 stsp
713 a9833bc9 2019-05-13 stsp if (completed && completed2) {
714 a9833bc9 2019-05-13 stsp err = got_error(GOT_ERR_ANCESTRY);
715 a9833bc9 2019-05-13 stsp break;
716 a9833bc9 2019-05-13 stsp }
717 a9833bc9 2019-05-13 stsp }
718 a9833bc9 2019-05-13 stsp done:
719 a9833bc9 2019-05-13 stsp got_object_idset_free(commit_ids);
720 a9833bc9 2019-05-13 stsp if (graph)
721 a9833bc9 2019-05-13 stsp got_commit_graph_close(graph);
722 a9833bc9 2019-05-13 stsp if (graph2)
723 a9833bc9 2019-05-13 stsp got_commit_graph_close(graph2);
724 a9833bc9 2019-05-13 stsp return err;
725 a9833bc9 2019-05-13 stsp }