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 79f35eb3 2018-06-11 stsp struct got_object_id_queue parent_ids;
49 372ccdbb 2018-06-10 stsp int nchildren;
50 79f35eb3 2018-06-11 stsp struct got_object_id_queue 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 9ba79e04 2018-06-11 stsp TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
60 372ccdbb 2018-06-10 stsp struct got_commit_graph {
61 372ccdbb 2018-06-10 stsp /* The set of all commits we have traversed. */
62 372ccdbb 2018-06-10 stsp struct got_object_idset *node_ids;
64 372ccdbb 2018-06-10 stsp /* The commit at which traversal began (youngest commit in node_ids). */
65 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *head_node;
68 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
69 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
70 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
71 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
72 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
74 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
75 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
76 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
78 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
79 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
80 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
82 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
84 372ccdbb 2018-06-10 stsp /* The next commit to return when the API user asks for one. */
85 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *iter_node;
87 9ba79e04 2018-06-11 stsp /* The graph iteration list contains all nodes in sorted order. */
88 9ba79e04 2018-06-11 stsp struct got_commit_graph_iter_list iter_list;
91 372ccdbb 2018-06-10 stsp static struct got_commit_graph *
92 372ccdbb 2018-06-10 stsp alloc_graph(void)
94 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
96 372ccdbb 2018-06-10 stsp graph = calloc(1, sizeof(*graph));
97 372ccdbb 2018-06-10 stsp if (graph == NULL)
98 372ccdbb 2018-06-10 stsp return NULL;
100 372ccdbb 2018-06-10 stsp graph->node_ids = got_object_idset_alloc();
101 372ccdbb 2018-06-10 stsp if (graph->node_ids == NULL) {
102 372ccdbb 2018-06-10 stsp free(graph);
103 372ccdbb 2018-06-10 stsp return NULL;
106 372ccdbb 2018-06-10 stsp graph->open_branches = got_object_idset_alloc();
107 372ccdbb 2018-06-10 stsp if (graph->open_branches == NULL) {
108 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
109 372ccdbb 2018-06-10 stsp free(graph);
110 372ccdbb 2018-06-10 stsp return NULL;
113 31920504 2018-06-11 stsp TAILQ_INIT(&graph->iter_list);
114 372ccdbb 2018-06-10 stsp return graph;
119 372ccdbb 2018-06-10 stsp is_head_node(struct got_commit_graph_node *node)
121 372ccdbb 2018-06-10 stsp return node->nchildren == 0;
125 372ccdbb 2018-06-10 stsp is_merge_point(struct got_commit_graph_node *node)
127 b43fbaa0 2018-06-11 stsp return node->nparents > 1;
131 372ccdbb 2018-06-10 stsp is_branch_point(struct got_commit_graph_node *node)
133 372ccdbb 2018-06-10 stsp return node->nchildren > 1;
137 372ccdbb 2018-06-10 stsp is_root_node(struct got_commit_graph_node *node)
139 b43fbaa0 2018-06-11 stsp return node->nparents == 0;
143 4626e416 2018-06-10 stsp static void
144 9ba79e04 2018-06-11 stsp add_node_to_iter_list(struct got_commit_graph *graph,
145 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *node,
146 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *child_node)
148 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
150 31920504 2018-06-11 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
151 31920504 2018-06-11 stsp TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
156 19137930 2018-06-11 stsp * If a child node is known, begin looping over the list there
157 19137930 2018-06-11 stsp * instead of beginning from the list head. Parent commits are
158 19137930 2018-06-11 stsp * traversed before their children.
160 9ba79e04 2018-06-11 stsp n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
162 d2312330 2018-06-11 stsp /* Ensure that an iteration in progress will see this new commit. */
163 d2312330 2018-06-11 stsp if (graph->iter_node) {
164 d2312330 2018-06-11 stsp n = graph->iter_node;
166 d2312330 2018-06-11 stsp next = TAILQ_NEXT(n, entry);
167 d2312330 2018-06-11 stsp if (next &&
168 d2312330 2018-06-11 stsp node->commit_timestamp >= next->commit_timestamp) {
169 d2312330 2018-06-11 stsp TAILQ_INSERT_BEFORE(next, node, entry);
173 d2312330 2018-06-11 stsp } while (next);
175 d2312330 2018-06-11 stsp TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
176 d2312330 2018-06-11 stsp node, entry);
180 d2312330 2018-06-11 stsp /* Insert into list based on committer timestamp. */
182 d2312330 2018-06-11 stsp if (node->commit_timestamp == n->commit_timestamp) {
183 d2312330 2018-06-11 stsp TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
185 d2312330 2018-06-11 stsp } else if (node->commit_timestamp < n->commit_timestamp) {
186 4bd3f2bb 2018-06-10 stsp next = TAILQ_NEXT(n, entry);
187 4bd3f2bb 2018-06-10 stsp if (next == NULL) {
188 31920504 2018-06-11 stsp TAILQ_INSERT_AFTER(&graph->iter_list, n,
189 4bd3f2bb 2018-06-10 stsp node, entry);
192 b43fbaa0 2018-06-11 stsp if (node->commit_timestamp >= next->commit_timestamp) {
193 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_BEFORE(next, node, entry);
197 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_BEFORE(n, node, entry);
200 9ba79e04 2018-06-11 stsp n = TAILQ_NEXT(n, entry);
201 9ba79e04 2018-06-11 stsp } while (n);
204 372ccdbb 2018-06-10 stsp static const struct got_error *
205 79f35eb3 2018-06-11 stsp add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
207 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
209 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
210 79f35eb3 2018-06-11 stsp if (qid == NULL)
211 b43fbaa0 2018-06-11 stsp return got_error_from_errno();
213 79f35eb3 2018-06-11 stsp qid->id = got_object_id_dup(id);
214 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
215 b43fbaa0 2018-06-11 stsp const struct got_error *err = got_error_from_errno();
217 b43fbaa0 2018-06-11 stsp return err;
220 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(ids, qid, entry);
221 b43fbaa0 2018-06-11 stsp return NULL;
224 b43fbaa0 2018-06-11 stsp static const struct got_error *
225 372ccdbb 2018-06-10 stsp add_node(struct got_commit_graph_node **new_node,
226 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
227 9ba79e04 2018-06-11 stsp struct got_commit_object *commit, struct got_commit_graph_node *child_node)
229 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
230 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node, *existing_node;
231 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
233 372ccdbb 2018-06-10 stsp *new_node = NULL;
235 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
236 372ccdbb 2018-06-10 stsp if (node == NULL)
237 372ccdbb 2018-06-10 stsp return got_error_from_errno();
239 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
240 b43fbaa0 2018-06-11 stsp SIMPLEQ_INIT(&node->parent_ids);
241 372ccdbb 2018-06-10 stsp SIMPLEQ_INIT(&node->child_ids);
242 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
243 79f35eb3 2018-06-11 stsp err = add_vertex(&node->parent_ids, qid->id);
245 b43fbaa0 2018-06-11 stsp return err;
246 b43fbaa0 2018-06-11 stsp node->nparents++;
248 b43fbaa0 2018-06-11 stsp node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
250 372ccdbb 2018-06-10 stsp err = got_object_idset_add((void **)(&existing_node),
251 372ccdbb 2018-06-10 stsp graph->node_ids, &node->id, node);
252 372ccdbb 2018-06-10 stsp if (err == NULL) {
253 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
255 9ba79e04 2018-06-11 stsp add_node_to_iter_list(graph, node, child_node);
256 372ccdbb 2018-06-10 stsp err = got_object_idset_remove(graph->open_branches, commit_id);
257 372ccdbb 2018-06-10 stsp if (err && err->code != GOT_ERR_NO_OBJ)
258 372ccdbb 2018-06-10 stsp return err;
259 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
260 79f35eb3 2018-06-11 stsp if (got_object_idset_get(graph->node_ids, qid->id))
261 372ccdbb 2018-06-10 stsp continue; /* parent already traversed */
262 372ccdbb 2018-06-10 stsp err = got_object_idset_add(NULL, graph->open_branches,
263 79f35eb3 2018-06-11 stsp qid->id, node);
264 372ccdbb 2018-06-10 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
265 372ccdbb 2018-06-10 stsp return err;
267 372ccdbb 2018-06-10 stsp *new_node = node;
268 372ccdbb 2018-06-10 stsp } else if (err->code == GOT_ERR_OBJ_EXISTS) {
269 372ccdbb 2018-06-10 stsp err = NULL;
270 372ccdbb 2018-06-10 stsp free(node);
271 372ccdbb 2018-06-10 stsp node = existing_node;
273 372ccdbb 2018-06-10 stsp free(node);
274 372ccdbb 2018-06-10 stsp return err;
277 9ba79e04 2018-06-11 stsp if (child_node) {
278 79f35eb3 2018-06-11 stsp struct got_object_qid *cid;
280 372ccdbb 2018-06-10 stsp /* Prevent linking to self. */
281 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(commit_id, &child_node->id) == 0)
282 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
284 372ccdbb 2018-06-10 stsp /* Prevent double-linking to the same child. */
285 372ccdbb 2018-06-10 stsp SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
286 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(cid->id, &child_node->id) == 0)
287 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
290 9ba79e04 2018-06-11 stsp err = add_vertex(&node->child_ids, &child_node->id);
292 372ccdbb 2018-06-10 stsp return err;
293 372ccdbb 2018-06-10 stsp node->nchildren++;
296 c4d7a9c4 2018-06-11 stsp return err;
299 c4d7a9c4 2018-06-11 stsp const struct got_error *
300 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
301 c4d7a9c4 2018-06-11 stsp struct got_object_id *commit_id, struct got_repository *repo)
303 c4d7a9c4 2018-06-11 stsp const struct got_error *err = NULL;
304 c4d7a9c4 2018-06-11 stsp struct got_commit_object *commit;
306 c4d7a9c4 2018-06-11 stsp *graph = NULL;
308 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
310 372ccdbb 2018-06-10 stsp return err;
312 372ccdbb 2018-06-10 stsp *graph = alloc_graph();
313 372ccdbb 2018-06-10 stsp if (*graph == NULL) {
314 372ccdbb 2018-06-10 stsp got_object_commit_close(commit);
315 372ccdbb 2018-06-10 stsp return got_error_from_errno();
318 372ccdbb 2018-06-10 stsp err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
319 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
321 372ccdbb 2018-06-10 stsp got_commit_graph_close(*graph);
322 372ccdbb 2018-06-10 stsp *graph = NULL;
323 372ccdbb 2018-06-10 stsp return err;
326 372ccdbb 2018-06-10 stsp return NULL;
329 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch {
330 372ccdbb 2018-06-10 stsp struct got_object_id parent_id;
331 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node;
334 372ccdbb 2018-06-10 stsp struct gather_branches_arg {
335 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch *branches;
336 372ccdbb 2018-06-10 stsp int nbranches;
339 372ccdbb 2018-06-10 stsp static void
340 372ccdbb 2018-06-10 stsp gather_branches(struct got_object_id *id, void *data, void *arg)
342 372ccdbb 2018-06-10 stsp struct gather_branches_arg *a = arg;
343 372ccdbb 2018-06-10 stsp memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
344 372ccdbb 2018-06-10 stsp a->branches[a->nbranches].node = data;
345 372ccdbb 2018-06-10 stsp a->nbranches++;
348 1142eae9 2018-06-11 stsp static const struct got_error *
349 1142eae9 2018-06-11 stsp fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
350 1142eae9 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo,
351 1142eae9 2018-06-11 stsp struct got_object_id *wanted_id)
353 372ccdbb 2018-06-10 stsp const struct got_error *err;
354 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch *branches;
355 372ccdbb 2018-06-10 stsp struct gather_branches_arg arg;
358 372ccdbb 2018-06-10 stsp *ncommits = 0;
359 1142eae9 2018-06-11 stsp if (wanted_id_added)
360 1142eae9 2018-06-11 stsp *wanted_id_added = 0;
362 372ccdbb 2018-06-10 stsp arg.nbranches = got_object_idset_num_elements(graph->open_branches);
363 372ccdbb 2018-06-10 stsp if (arg.nbranches == 0)
364 372ccdbb 2018-06-10 stsp return NULL;
367 372ccdbb 2018-06-10 stsp * Adding nodes to the graph might change the graph's open
368 372ccdbb 2018-06-10 stsp * branches state. Create a local copy of the current state.
370 372ccdbb 2018-06-10 stsp branches = calloc(arg.nbranches, sizeof(*branches));
371 372ccdbb 2018-06-10 stsp if (branches == NULL)
372 372ccdbb 2018-06-10 stsp return got_error_from_errno();
373 372ccdbb 2018-06-10 stsp arg.nbranches = 0; /* reset; gather_branches() will increment */
374 372ccdbb 2018-06-10 stsp arg.branches = branches;
375 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
377 372ccdbb 2018-06-10 stsp for (i = 0; i < arg.nbranches; i++) {
378 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
379 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *child_node, *new_node;
380 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
382 372ccdbb 2018-06-10 stsp commit_id = &branches[i].parent_id;
383 372ccdbb 2018-06-10 stsp child_node = branches[i].node;
385 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
389 9ba79e04 2018-06-11 stsp err = add_node(&new_node, graph, commit_id, commit, child_node);
390 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
392 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_OBJ_EXISTS)
394 372ccdbb 2018-06-10 stsp err = NULL;
396 372ccdbb 2018-06-10 stsp if (new_node)
397 372ccdbb 2018-06-10 stsp (*ncommits)++;
398 1142eae9 2018-06-11 stsp if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
399 1142eae9 2018-06-11 stsp *wanted_id_added = 1;
402 372ccdbb 2018-06-10 stsp free(branches);
403 372ccdbb 2018-06-10 stsp return err;
406 372ccdbb 2018-06-10 stsp const struct got_error *
407 372ccdbb 2018-06-10 stsp got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
408 372ccdbb 2018-06-10 stsp int limit, struct got_repository *repo)
410 372ccdbb 2018-06-10 stsp const struct got_error *err;
411 1142eae9 2018-06-11 stsp int ncommits;
413 372ccdbb 2018-06-10 stsp *nfetched = 0;
415 1142eae9 2018-06-11 stsp while (*nfetched < limit) {
416 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits, NULL,
417 1142eae9 2018-06-11 stsp graph, repo, NULL);
419 372ccdbb 2018-06-10 stsp return err;
420 372ccdbb 2018-06-10 stsp if (ncommits == 0)
422 1142eae9 2018-06-11 stsp *nfetched += ncommits;
425 372ccdbb 2018-06-10 stsp return NULL;
428 1142eae9 2018-06-11 stsp const struct got_error *
429 1142eae9 2018-06-11 stsp got_commit_graph_fetch_commits_up_to(int *nfetched,
430 1142eae9 2018-06-11 stsp struct got_commit_graph *graph, struct got_object_id *wanted_id,
431 1142eae9 2018-06-11 stsp struct got_repository *repo)
433 1142eae9 2018-06-11 stsp const struct got_error *err;
434 1142eae9 2018-06-11 stsp int ncommits, wanted_id_added = 0;
436 1142eae9 2018-06-11 stsp *nfetched = 0;
438 6e0c0f9a 2018-06-11 stsp if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
439 6e0c0f9a 2018-06-11 stsp return NULL;
441 1142eae9 2018-06-11 stsp while (!wanted_id_added) {
442 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits,
443 1142eae9 2018-06-11 stsp &wanted_id_added, graph, repo, wanted_id);
445 1142eae9 2018-06-11 stsp return err;
446 1142eae9 2018-06-11 stsp if (ncommits == 0)
447 1142eae9 2018-06-11 stsp return NULL;
448 1142eae9 2018-06-11 stsp *nfetched += ncommits;
451 1142eae9 2018-06-11 stsp return NULL;
454 372ccdbb 2018-06-10 stsp static void
455 372ccdbb 2018-06-10 stsp free_graph_node(struct got_object_id *id, void *data, void *arg)
457 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node = data;
458 372ccdbb 2018-06-10 stsp while (!SIMPLEQ_EMPTY(&node->child_ids)) {
459 79f35eb3 2018-06-11 stsp struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
460 372ccdbb 2018-06-10 stsp SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
461 372ccdbb 2018-06-10 stsp free(child);
463 372ccdbb 2018-06-10 stsp free(node);
467 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
469 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->open_branches);
470 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
471 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
472 372ccdbb 2018-06-10 stsp free(graph);
475 372ccdbb 2018-06-10 stsp const struct got_error *
476 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
477 372ccdbb 2018-06-10 stsp struct got_object_id *id)
479 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *start_node;
481 372ccdbb 2018-06-10 stsp start_node = got_object_idset_get(graph->node_ids, id);
482 372ccdbb 2018-06-10 stsp if (start_node == NULL)
483 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_NO_OBJ);
485 372ccdbb 2018-06-10 stsp graph->iter_node = start_node;
486 372ccdbb 2018-06-10 stsp return NULL;
489 372ccdbb 2018-06-10 stsp const struct got_error *
490 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
491 b43fbaa0 2018-06-11 stsp struct got_commit_graph *graph)
493 9ba79e04 2018-06-11 stsp *id = NULL;
495 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
496 372ccdbb 2018-06-10 stsp /* We are done interating, or iteration was not started. */
497 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
500 9ba79e04 2018-06-11 stsp if (graph->iter_node ==
501 9ba79e04 2018-06-11 stsp TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
502 9ba79e04 2018-06-11 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
503 9ba79e04 2018-06-11 stsp *id = &graph->iter_node->id;
504 9ba79e04 2018-06-11 stsp /* We are done interating. */
505 9ba79e04 2018-06-11 stsp graph->iter_node = NULL;
506 372ccdbb 2018-06-10 stsp return NULL;
509 9ba79e04 2018-06-11 stsp if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
510 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_ITER_NEED_MORE);
512 372ccdbb 2018-06-10 stsp *id = &graph->iter_node->id;
513 9ba79e04 2018-06-11 stsp graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
514 372ccdbb 2018-06-10 stsp return NULL;