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