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