2 372ccdbb 2018-06-10 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
22 372ccdbb 2018-06-10 stsp #include <stdio.h>
23 372ccdbb 2018-06-10 stsp #include <stdlib.h>
24 372ccdbb 2018-06-10 stsp #include <string.h>
25 372ccdbb 2018-06-10 stsp #include <sha1.h>
26 372ccdbb 2018-06-10 stsp #include <zlib.h>
27 372ccdbb 2018-06-10 stsp #include <ctype.h>
29 372ccdbb 2018-06-10 stsp #include "got_error.h"
30 372ccdbb 2018-06-10 stsp #include "got_object.h"
31 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
33 372ccdbb 2018-06-10 stsp #include "got_lib_delta.h"
34 372ccdbb 2018-06-10 stsp #include "got_lib_zbuf.h"
35 372ccdbb 2018-06-10 stsp #include "got_lib_object.h"
36 372ccdbb 2018-06-10 stsp #include "got_lib_object_idset.h"
38 372ccdbb 2018-06-10 stsp struct got_commit_graph_node {
39 372ccdbb 2018-06-10 stsp struct got_object_id id;
42 372ccdbb 2018-06-10 stsp * Each graph node corresponds to a commit object.
43 372ccdbb 2018-06-10 stsp * Graph vertices are modelled with two separate adjacency lists:
44 372ccdbb 2018-06-10 stsp * Adjacencies of a graph node are either parent (older) commits,
45 372ccdbb 2018-06-10 stsp * and child (younger) commits.
47 b43fbaa0 2018-06-11 stsp int nparents;
48 b43fbaa0 2018-06-11 stsp struct got_object_id_list parent_ids;
49 372ccdbb 2018-06-10 stsp int nchildren;
50 b43fbaa0 2018-06-11 stsp struct got_object_id_list child_ids;
52 b43fbaa0 2018-06-11 stsp time_t commit_timestamp;
54 372ccdbb 2018-06-10 stsp /* Used during graph iteration. */
55 372ccdbb 2018-06-10 stsp TAILQ_ENTRY(got_commit_graph_node) entry;
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;
62 372ccdbb 2018-06-10 stsp /* The commit at which traversal began (youngest commit in node_ids). */
63 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *head_node;
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.
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.
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.
80 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
82 372ccdbb 2018-06-10 stsp /* The next commit to return when the API user asks for one. */
83 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *iter_node;
85 372ccdbb 2018-06-10 stsp TAILQ_HEAD(, got_commit_graph_node) iter_candidates;
88 372ccdbb 2018-06-10 stsp static struct got_commit_graph *
89 372ccdbb 2018-06-10 stsp alloc_graph(void)
91 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
93 372ccdbb 2018-06-10 stsp graph = calloc(1, sizeof(*graph));
94 372ccdbb 2018-06-10 stsp if (graph == NULL)
95 372ccdbb 2018-06-10 stsp return NULL;
97 372ccdbb 2018-06-10 stsp graph->node_ids = got_object_idset_alloc();
98 372ccdbb 2018-06-10 stsp if (graph->node_ids == NULL) {
99 372ccdbb 2018-06-10 stsp free(graph);
100 372ccdbb 2018-06-10 stsp return NULL;
103 372ccdbb 2018-06-10 stsp graph->open_branches = got_object_idset_alloc();
104 372ccdbb 2018-06-10 stsp if (graph->open_branches == NULL) {
105 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
106 372ccdbb 2018-06-10 stsp free(graph);
107 372ccdbb 2018-06-10 stsp return NULL;
110 372ccdbb 2018-06-10 stsp TAILQ_INIT(&graph->iter_candidates);
111 372ccdbb 2018-06-10 stsp return graph;
116 372ccdbb 2018-06-10 stsp is_head_node(struct got_commit_graph_node *node)
118 372ccdbb 2018-06-10 stsp return node->nchildren == 0;
122 372ccdbb 2018-06-10 stsp is_merge_point(struct got_commit_graph_node *node)
124 b43fbaa0 2018-06-11 stsp return node->nparents > 1;
128 372ccdbb 2018-06-10 stsp is_branch_point(struct got_commit_graph_node *node)
130 372ccdbb 2018-06-10 stsp return node->nchildren > 1;
135 372ccdbb 2018-06-10 stsp is_root_node(struct got_commit_graph_node *node)
137 b43fbaa0 2018-06-11 stsp return node->nparents == 0;
140 4626e416 2018-06-10 stsp static void
141 372ccdbb 2018-06-10 stsp add_iteration_candidate(struct got_commit_graph *graph,
142 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node)
144 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
146 372ccdbb 2018-06-10 stsp if (TAILQ_EMPTY(&graph->iter_candidates)) {
147 372ccdbb 2018-06-10 stsp TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
151 372ccdbb 2018-06-10 stsp TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
152 b43fbaa0 2018-06-11 stsp if (node->commit_timestamp < n->commit_timestamp) {
153 4bd3f2bb 2018-06-10 stsp next = TAILQ_NEXT(n, entry);
154 4bd3f2bb 2018-06-10 stsp if (next == NULL) {
155 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_AFTER(&graph->iter_candidates, n,
156 4bd3f2bb 2018-06-10 stsp node, entry);
159 b43fbaa0 2018-06-11 stsp if (node->commit_timestamp >= next->commit_timestamp) {
160 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_BEFORE(next, node, entry);
164 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_BEFORE(n, node, entry);
170 372ccdbb 2018-06-10 stsp static const struct got_error *
171 b43fbaa0 2018-06-11 stsp add_vertex(struct got_object_id_list *id_list, struct got_object_id *id)
173 b43fbaa0 2018-06-11 stsp struct got_parent_id *pid;
175 b43fbaa0 2018-06-11 stsp pid = calloc(1, sizeof(*pid));
176 b43fbaa0 2018-06-11 stsp if (pid == NULL)
177 b43fbaa0 2018-06-11 stsp return got_error_from_errno();
179 b43fbaa0 2018-06-11 stsp pid->id = got_object_id_dup(id);
180 b43fbaa0 2018-06-11 stsp if (pid->id == NULL) {
181 b43fbaa0 2018-06-11 stsp const struct got_error *err = got_error_from_errno();
183 b43fbaa0 2018-06-11 stsp return err;
186 b43fbaa0 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(id_list, pid, entry);
187 b43fbaa0 2018-06-11 stsp return NULL;
190 b43fbaa0 2018-06-11 stsp static const struct got_error *
191 372ccdbb 2018-06-10 stsp add_node(struct got_commit_graph_node **new_node,
192 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
193 372ccdbb 2018-06-10 stsp struct got_commit_object *commit, struct got_object_id *child_commit_id)
195 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
196 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node, *existing_node;
197 b43fbaa0 2018-06-11 stsp struct got_parent_id *pid;
199 372ccdbb 2018-06-10 stsp *new_node = NULL;
201 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
202 372ccdbb 2018-06-10 stsp if (node == NULL)
203 372ccdbb 2018-06-10 stsp return got_error_from_errno();
205 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
206 b43fbaa0 2018-06-11 stsp SIMPLEQ_INIT(&node->parent_ids);
207 372ccdbb 2018-06-10 stsp SIMPLEQ_INIT(&node->child_ids);
208 b43fbaa0 2018-06-11 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
209 b43fbaa0 2018-06-11 stsp err = add_vertex(&node->parent_ids, pid->id);
211 b43fbaa0 2018-06-11 stsp return err;
212 b43fbaa0 2018-06-11 stsp node->nparents++;
214 b43fbaa0 2018-06-11 stsp node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
216 372ccdbb 2018-06-10 stsp err = got_object_idset_add((void **)(&existing_node),
217 372ccdbb 2018-06-10 stsp graph->node_ids, &node->id, node);
218 372ccdbb 2018-06-10 stsp if (err == NULL) {
219 372ccdbb 2018-06-10 stsp struct got_parent_id *pid;
221 4626e416 2018-06-10 stsp add_iteration_candidate(graph, node);
222 372ccdbb 2018-06-10 stsp err = got_object_idset_remove(graph->open_branches, commit_id);
223 372ccdbb 2018-06-10 stsp if (err && err->code != GOT_ERR_NO_OBJ)
224 372ccdbb 2018-06-10 stsp return err;
225 372ccdbb 2018-06-10 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
226 372ccdbb 2018-06-10 stsp if (got_object_idset_get(graph->node_ids, pid->id))
227 372ccdbb 2018-06-10 stsp continue; /* parent already traversed */
228 372ccdbb 2018-06-10 stsp err = got_object_idset_add(NULL, graph->open_branches,
229 372ccdbb 2018-06-10 stsp pid->id, node);
230 372ccdbb 2018-06-10 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
231 372ccdbb 2018-06-10 stsp return err;
233 372ccdbb 2018-06-10 stsp *new_node = node;
234 372ccdbb 2018-06-10 stsp } else if (err->code == GOT_ERR_OBJ_EXISTS) {
235 372ccdbb 2018-06-10 stsp err = NULL;
236 372ccdbb 2018-06-10 stsp free(node);
237 372ccdbb 2018-06-10 stsp node = existing_node;
239 372ccdbb 2018-06-10 stsp free(node);
240 372ccdbb 2018-06-10 stsp return err;
243 372ccdbb 2018-06-10 stsp if (child_commit_id) {
244 b43fbaa0 2018-06-11 stsp struct got_parent_id *cid;
246 372ccdbb 2018-06-10 stsp /* Prevent linking to self. */
247 372ccdbb 2018-06-10 stsp if (got_object_id_cmp(commit_id, child_commit_id) == 0)
248 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
250 372ccdbb 2018-06-10 stsp /* Prevent double-linking to the same child. */
251 372ccdbb 2018-06-10 stsp SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
252 372ccdbb 2018-06-10 stsp if (got_object_id_cmp(cid->id, child_commit_id) == 0)
253 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
256 b43fbaa0 2018-06-11 stsp err = add_vertex(&node->child_ids, child_commit_id);
258 372ccdbb 2018-06-10 stsp return err;
259 372ccdbb 2018-06-10 stsp node->nchildren++;
263 372ccdbb 2018-06-10 stsp return err;
266 372ccdbb 2018-06-10 stsp const struct got_error *
267 372ccdbb 2018-06-10 stsp got_commit_graph_open(struct got_commit_graph **graph,
268 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id, struct got_repository *repo)
270 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
271 372ccdbb 2018-06-10 stsp struct got_object *obj;
272 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
274 372ccdbb 2018-06-10 stsp *graph = NULL;
276 372ccdbb 2018-06-10 stsp err = got_object_open(&obj, repo, commit_id);
278 372ccdbb 2018-06-10 stsp return err;
279 372ccdbb 2018-06-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
280 372ccdbb 2018-06-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
281 372ccdbb 2018-06-10 stsp got_object_close(obj);
282 372ccdbb 2018-06-10 stsp return err;
285 372ccdbb 2018-06-10 stsp err = got_object_commit_open(&commit, repo, obj);
286 372ccdbb 2018-06-10 stsp got_object_close(obj);
288 372ccdbb 2018-06-10 stsp return err;
290 372ccdbb 2018-06-10 stsp *graph = alloc_graph();
291 372ccdbb 2018-06-10 stsp if (*graph == NULL) {
292 372ccdbb 2018-06-10 stsp got_object_commit_close(commit);
293 372ccdbb 2018-06-10 stsp return got_error_from_errno();
296 372ccdbb 2018-06-10 stsp err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
297 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
299 372ccdbb 2018-06-10 stsp got_commit_graph_close(*graph);
300 372ccdbb 2018-06-10 stsp *graph = NULL;
301 372ccdbb 2018-06-10 stsp return err;
304 372ccdbb 2018-06-10 stsp return NULL;
307 372ccdbb 2018-06-10 stsp static const struct got_error *
308 372ccdbb 2018-06-10 stsp open_commit(struct got_commit_object **commit, struct got_object_id *id,
309 372ccdbb 2018-06-10 stsp struct got_repository *repo)
311 372ccdbb 2018-06-10 stsp const struct got_error *err;
312 372ccdbb 2018-06-10 stsp struct got_object *obj;
314 372ccdbb 2018-06-10 stsp err = got_object_open(&obj, repo, id);
316 372ccdbb 2018-06-10 stsp return err;
317 372ccdbb 2018-06-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
318 372ccdbb 2018-06-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
322 372ccdbb 2018-06-10 stsp err = got_object_commit_open(commit, repo, obj);
324 372ccdbb 2018-06-10 stsp got_object_close(obj);
325 372ccdbb 2018-06-10 stsp return err;
328 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch {
329 372ccdbb 2018-06-10 stsp struct got_object_id parent_id;
330 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node;
333 372ccdbb 2018-06-10 stsp struct gather_branches_arg {
334 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch *branches;
335 372ccdbb 2018-06-10 stsp int nbranches;
338 372ccdbb 2018-06-10 stsp static void
339 372ccdbb 2018-06-10 stsp gather_branches(struct got_object_id *id, void *data, void *arg)
341 372ccdbb 2018-06-10 stsp struct gather_branches_arg *a = arg;
342 372ccdbb 2018-06-10 stsp memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
343 372ccdbb 2018-06-10 stsp a->branches[a->nbranches].node = data;
344 372ccdbb 2018-06-10 stsp a->nbranches++;
347 372ccdbb 2018-06-10 stsp const struct got_error *
348 372ccdbb 2018-06-10 stsp fetch_commits_from_open_branches(int *ncommits,
349 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph, struct got_repository *repo)
351 372ccdbb 2018-06-10 stsp const struct got_error *err;
352 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch *branches;
353 372ccdbb 2018-06-10 stsp struct gather_branches_arg arg;
356 372ccdbb 2018-06-10 stsp *ncommits = 0;
358 372ccdbb 2018-06-10 stsp arg.nbranches = got_object_idset_num_elements(graph->open_branches);
359 372ccdbb 2018-06-10 stsp if (arg.nbranches == 0)
360 372ccdbb 2018-06-10 stsp return NULL;
363 372ccdbb 2018-06-10 stsp * Adding nodes to the graph might change the graph's open
364 372ccdbb 2018-06-10 stsp * branches state. Create a local copy of the current state.
366 372ccdbb 2018-06-10 stsp branches = calloc(arg.nbranches, sizeof(*branches));
367 372ccdbb 2018-06-10 stsp if (branches == NULL)
368 372ccdbb 2018-06-10 stsp return got_error_from_errno();
369 372ccdbb 2018-06-10 stsp arg.nbranches = 0; /* reset; gather_branches() will increment */
370 372ccdbb 2018-06-10 stsp arg.branches = branches;
371 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
373 372ccdbb 2018-06-10 stsp for (i = 0; i < arg.nbranches; i++) {
374 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
375 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *child_node, *new_node;
376 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
378 372ccdbb 2018-06-10 stsp commit_id = &branches[i].parent_id;
379 372ccdbb 2018-06-10 stsp child_node = branches[i].node;
381 372ccdbb 2018-06-10 stsp err = open_commit(&commit, commit_id, repo);
385 372ccdbb 2018-06-10 stsp err = add_node(&new_node, graph, commit_id, commit,
386 372ccdbb 2018-06-10 stsp &child_node->id);
387 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
389 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_OBJ_EXISTS)
391 372ccdbb 2018-06-10 stsp err = NULL;
393 372ccdbb 2018-06-10 stsp if (new_node)
394 372ccdbb 2018-06-10 stsp (*ncommits)++;
397 372ccdbb 2018-06-10 stsp free(branches);
398 372ccdbb 2018-06-10 stsp return err;
401 372ccdbb 2018-06-10 stsp const struct got_error *
402 372ccdbb 2018-06-10 stsp got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
403 372ccdbb 2018-06-10 stsp int limit, struct got_repository *repo)
405 372ccdbb 2018-06-10 stsp const struct got_error *err;
406 372ccdbb 2018-06-10 stsp int total = 0, ncommits;
408 372ccdbb 2018-06-10 stsp *nfetched = 0;
410 372ccdbb 2018-06-10 stsp while (total < limit) {
411 372ccdbb 2018-06-10 stsp err = fetch_commits_from_open_branches(&ncommits, graph, repo);
413 372ccdbb 2018-06-10 stsp return err;
414 372ccdbb 2018-06-10 stsp if (ncommits == 0)
416 372ccdbb 2018-06-10 stsp total += ncommits;
419 372ccdbb 2018-06-10 stsp *nfetched = total;
420 372ccdbb 2018-06-10 stsp return NULL;
423 372ccdbb 2018-06-10 stsp static void
424 372ccdbb 2018-06-10 stsp free_graph_node(struct got_object_id *id, void *data, void *arg)
426 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node = data;
427 372ccdbb 2018-06-10 stsp while (!SIMPLEQ_EMPTY(&node->child_ids)) {
428 372ccdbb 2018-06-10 stsp struct got_parent_id *child = SIMPLEQ_FIRST(&node->child_ids);
429 372ccdbb 2018-06-10 stsp SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
430 372ccdbb 2018-06-10 stsp free(child);
432 372ccdbb 2018-06-10 stsp free(node);
436 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
438 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->open_branches);
439 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
440 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
441 372ccdbb 2018-06-10 stsp free(graph);
444 372ccdbb 2018-06-10 stsp const struct got_error *
445 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
446 372ccdbb 2018-06-10 stsp struct got_object_id *id)
448 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *start_node, *node;
449 372ccdbb 2018-06-10 stsp struct got_parent_id *pid;
451 372ccdbb 2018-06-10 stsp start_node = got_object_idset_get(graph->node_ids, id);
452 372ccdbb 2018-06-10 stsp if (start_node == NULL)
453 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_NO_OBJ);
455 372ccdbb 2018-06-10 stsp graph->iter_node = start_node;
457 372ccdbb 2018-06-10 stsp while (!TAILQ_EMPTY(&graph->iter_candidates)) {
458 372ccdbb 2018-06-10 stsp node = TAILQ_FIRST(&graph->iter_candidates);
459 372ccdbb 2018-06-10 stsp TAILQ_REMOVE(&graph->iter_candidates, node, entry);
462 372ccdbb 2018-06-10 stsp /* Put all known parents of this commit on the candidate list. */
463 b43fbaa0 2018-06-11 stsp SIMPLEQ_FOREACH(pid, &start_node->parent_ids, entry) {
464 372ccdbb 2018-06-10 stsp node = got_object_idset_get(graph->node_ids, pid->id);
466 4626e416 2018-06-10 stsp add_iteration_candidate(graph, node);
469 372ccdbb 2018-06-10 stsp return NULL;
472 372ccdbb 2018-06-10 stsp const struct got_error *
473 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
474 b43fbaa0 2018-06-11 stsp struct got_commit_graph *graph)
476 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node;
478 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
479 372ccdbb 2018-06-10 stsp /* We are done interating, or iteration was not started. */
480 372ccdbb 2018-06-10 stsp *id = NULL;
481 372ccdbb 2018-06-10 stsp return NULL;
484 372ccdbb 2018-06-10 stsp if (TAILQ_EMPTY(&graph->iter_candidates)) {
485 372ccdbb 2018-06-10 stsp if (is_root_node(graph->iter_node) &&
486 372ccdbb 2018-06-10 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
487 372ccdbb 2018-06-10 stsp *id = &graph->iter_node->id;
488 372ccdbb 2018-06-10 stsp /* We are done interating. */
489 372ccdbb 2018-06-10 stsp graph->iter_node = NULL;
490 372ccdbb 2018-06-10 stsp return NULL;
492 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_ITER_NEED_MORE);
495 372ccdbb 2018-06-10 stsp *id = &graph->iter_node->id;
496 372ccdbb 2018-06-10 stsp node = TAILQ_FIRST(&graph->iter_candidates);
497 372ccdbb 2018-06-10 stsp TAILQ_REMOVE(&graph->iter_candidates, node, entry);
498 372ccdbb 2018-06-10 stsp graph->iter_node = node;
499 372ccdbb 2018-06-10 stsp return NULL;