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);
155 d2312330 2018-06-11 stsp /* Ensure that an iteration in progress will see this new commit. */
156 d2312330 2018-06-11 stsp if (graph->iter_node) {
157 d2312330 2018-06-11 stsp n = graph->iter_node;
158 58e5f354 2018-06-11 stsp while (n) {
159 d2312330 2018-06-11 stsp next = TAILQ_NEXT(n, entry);
160 d2312330 2018-06-11 stsp if (next &&
161 d2312330 2018-06-11 stsp node->commit_timestamp >= next->commit_timestamp) {
162 d2312330 2018-06-11 stsp TAILQ_INSERT_BEFORE(next, node, entry);
167 d2312330 2018-06-11 stsp TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
168 d2312330 2018-06-11 stsp node, entry);
173 58e5f354 2018-06-11 stsp * If a child node is known, begin looping over the list there
174 58e5f354 2018-06-11 stsp * instead of beginning from the list head. Parent commits are
175 58e5f354 2018-06-11 stsp * traversed before their children.
177 58e5f354 2018-06-11 stsp n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
179 d2312330 2018-06-11 stsp /* Insert into list based on committer timestamp. */
181 d2312330 2018-06-11 stsp if (node->commit_timestamp == n->commit_timestamp) {
182 d2312330 2018-06-11 stsp TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
184 d2312330 2018-06-11 stsp } else if (node->commit_timestamp < n->commit_timestamp) {
185 4bd3f2bb 2018-06-10 stsp next = TAILQ_NEXT(n, entry);
186 4bd3f2bb 2018-06-10 stsp if (next == NULL) {
187 31920504 2018-06-11 stsp TAILQ_INSERT_AFTER(&graph->iter_list, n,
188 4bd3f2bb 2018-06-10 stsp node, entry);
191 b43fbaa0 2018-06-11 stsp if (node->commit_timestamp >= next->commit_timestamp) {
192 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_BEFORE(next, node, entry);
196 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_BEFORE(n, node, entry);
199 9ba79e04 2018-06-11 stsp n = TAILQ_NEXT(n, entry);
200 9ba79e04 2018-06-11 stsp } while (n);
203 372ccdbb 2018-06-10 stsp static const struct got_error *
204 79f35eb3 2018-06-11 stsp add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
206 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
208 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
209 79f35eb3 2018-06-11 stsp if (qid == NULL)
210 b43fbaa0 2018-06-11 stsp return got_error_from_errno();
212 79f35eb3 2018-06-11 stsp qid->id = got_object_id_dup(id);
213 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
214 b43fbaa0 2018-06-11 stsp const struct got_error *err = got_error_from_errno();
216 b43fbaa0 2018-06-11 stsp return err;
219 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(ids, qid, entry);
220 b43fbaa0 2018-06-11 stsp return NULL;
223 b43fbaa0 2018-06-11 stsp static const struct got_error *
224 372ccdbb 2018-06-10 stsp add_node(struct got_commit_graph_node **new_node,
225 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
226 9ba79e04 2018-06-11 stsp struct got_commit_object *commit, struct got_commit_graph_node *child_node)
228 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
229 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node, *existing_node;
230 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
232 372ccdbb 2018-06-10 stsp *new_node = NULL;
234 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
235 372ccdbb 2018-06-10 stsp if (node == NULL)
236 372ccdbb 2018-06-10 stsp return got_error_from_errno();
238 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
239 b43fbaa0 2018-06-11 stsp SIMPLEQ_INIT(&node->parent_ids);
240 372ccdbb 2018-06-10 stsp SIMPLEQ_INIT(&node->child_ids);
241 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
242 79f35eb3 2018-06-11 stsp err = add_vertex(&node->parent_ids, qid->id);
244 b43fbaa0 2018-06-11 stsp return err;
245 b43fbaa0 2018-06-11 stsp node->nparents++;
247 b43fbaa0 2018-06-11 stsp node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
249 372ccdbb 2018-06-10 stsp err = got_object_idset_add((void **)(&existing_node),
250 372ccdbb 2018-06-10 stsp graph->node_ids, &node->id, node);
251 372ccdbb 2018-06-10 stsp if (err == NULL) {
252 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
254 9ba79e04 2018-06-11 stsp add_node_to_iter_list(graph, node, child_node);
255 372ccdbb 2018-06-10 stsp err = got_object_idset_remove(graph->open_branches, commit_id);
256 372ccdbb 2018-06-10 stsp if (err && err->code != GOT_ERR_NO_OBJ)
257 372ccdbb 2018-06-10 stsp return err;
258 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
259 79f35eb3 2018-06-11 stsp if (got_object_idset_get(graph->node_ids, qid->id))
260 372ccdbb 2018-06-10 stsp continue; /* parent already traversed */
261 372ccdbb 2018-06-10 stsp err = got_object_idset_add(NULL, graph->open_branches,
262 79f35eb3 2018-06-11 stsp qid->id, node);
263 372ccdbb 2018-06-10 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
264 372ccdbb 2018-06-10 stsp return err;
266 372ccdbb 2018-06-10 stsp *new_node = node;
267 372ccdbb 2018-06-10 stsp } else if (err->code == GOT_ERR_OBJ_EXISTS) {
268 372ccdbb 2018-06-10 stsp err = NULL;
269 372ccdbb 2018-06-10 stsp free(node);
270 372ccdbb 2018-06-10 stsp node = existing_node;
272 372ccdbb 2018-06-10 stsp free(node);
273 372ccdbb 2018-06-10 stsp return err;
276 9ba79e04 2018-06-11 stsp if (child_node) {
277 79f35eb3 2018-06-11 stsp struct got_object_qid *cid;
279 372ccdbb 2018-06-10 stsp /* Prevent linking to self. */
280 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(commit_id, &child_node->id) == 0)
281 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
283 372ccdbb 2018-06-10 stsp /* Prevent double-linking to the same child. */
284 372ccdbb 2018-06-10 stsp SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
285 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(cid->id, &child_node->id) == 0)
286 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
289 9ba79e04 2018-06-11 stsp err = add_vertex(&node->child_ids, &child_node->id);
291 372ccdbb 2018-06-10 stsp return err;
292 372ccdbb 2018-06-10 stsp node->nchildren++;
295 c4d7a9c4 2018-06-11 stsp return err;
298 c4d7a9c4 2018-06-11 stsp const struct got_error *
299 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
300 c4d7a9c4 2018-06-11 stsp struct got_object_id *commit_id, struct got_repository *repo)
302 c4d7a9c4 2018-06-11 stsp const struct got_error *err = NULL;
303 c4d7a9c4 2018-06-11 stsp struct got_commit_object *commit;
305 c4d7a9c4 2018-06-11 stsp *graph = NULL;
307 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
309 372ccdbb 2018-06-10 stsp return err;
311 372ccdbb 2018-06-10 stsp *graph = alloc_graph();
312 372ccdbb 2018-06-10 stsp if (*graph == NULL) {
313 372ccdbb 2018-06-10 stsp got_object_commit_close(commit);
314 372ccdbb 2018-06-10 stsp return got_error_from_errno();
317 372ccdbb 2018-06-10 stsp err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
318 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
320 372ccdbb 2018-06-10 stsp got_commit_graph_close(*graph);
321 372ccdbb 2018-06-10 stsp *graph = NULL;
322 372ccdbb 2018-06-10 stsp return err;
325 372ccdbb 2018-06-10 stsp return NULL;
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 1142eae9 2018-06-11 stsp static const struct got_error *
348 1142eae9 2018-06-11 stsp fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
349 1142eae9 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo,
350 1142eae9 2018-06-11 stsp struct got_object_id *wanted_id)
352 372ccdbb 2018-06-10 stsp const struct got_error *err;
353 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch *branches;
354 372ccdbb 2018-06-10 stsp struct gather_branches_arg arg;
357 372ccdbb 2018-06-10 stsp *ncommits = 0;
358 1142eae9 2018-06-11 stsp if (wanted_id_added)
359 1142eae9 2018-06-11 stsp *wanted_id_added = 0;
361 372ccdbb 2018-06-10 stsp arg.nbranches = got_object_idset_num_elements(graph->open_branches);
362 372ccdbb 2018-06-10 stsp if (arg.nbranches == 0)
363 372ccdbb 2018-06-10 stsp return NULL;
366 372ccdbb 2018-06-10 stsp * Adding nodes to the graph might change the graph's open
367 372ccdbb 2018-06-10 stsp * branches state. Create a local copy of the current state.
369 372ccdbb 2018-06-10 stsp branches = calloc(arg.nbranches, sizeof(*branches));
370 372ccdbb 2018-06-10 stsp if (branches == NULL)
371 372ccdbb 2018-06-10 stsp return got_error_from_errno();
372 372ccdbb 2018-06-10 stsp arg.nbranches = 0; /* reset; gather_branches() will increment */
373 372ccdbb 2018-06-10 stsp arg.branches = branches;
374 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
376 372ccdbb 2018-06-10 stsp for (i = 0; i < arg.nbranches; i++) {
377 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
378 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *child_node, *new_node;
379 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
381 372ccdbb 2018-06-10 stsp commit_id = &branches[i].parent_id;
382 372ccdbb 2018-06-10 stsp child_node = branches[i].node;
384 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
388 9ba79e04 2018-06-11 stsp err = add_node(&new_node, graph, commit_id, commit, child_node);
389 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
391 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_OBJ_EXISTS)
393 372ccdbb 2018-06-10 stsp err = NULL;
395 372ccdbb 2018-06-10 stsp if (new_node)
396 372ccdbb 2018-06-10 stsp (*ncommits)++;
397 1142eae9 2018-06-11 stsp if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
398 1142eae9 2018-06-11 stsp *wanted_id_added = 1;
401 372ccdbb 2018-06-10 stsp free(branches);
402 372ccdbb 2018-06-10 stsp return err;
405 372ccdbb 2018-06-10 stsp const struct got_error *
406 372ccdbb 2018-06-10 stsp got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
407 372ccdbb 2018-06-10 stsp int limit, struct got_repository *repo)
409 372ccdbb 2018-06-10 stsp const struct got_error *err;
410 1142eae9 2018-06-11 stsp int ncommits;
412 372ccdbb 2018-06-10 stsp *nfetched = 0;
414 1142eae9 2018-06-11 stsp while (*nfetched < limit) {
415 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits, NULL,
416 1142eae9 2018-06-11 stsp graph, repo, NULL);
418 372ccdbb 2018-06-10 stsp return err;
419 372ccdbb 2018-06-10 stsp if (ncommits == 0)
421 1142eae9 2018-06-11 stsp *nfetched += ncommits;
424 372ccdbb 2018-06-10 stsp return NULL;
427 1142eae9 2018-06-11 stsp const struct got_error *
428 1142eae9 2018-06-11 stsp got_commit_graph_fetch_commits_up_to(int *nfetched,
429 1142eae9 2018-06-11 stsp struct got_commit_graph *graph, struct got_object_id *wanted_id,
430 1142eae9 2018-06-11 stsp struct got_repository *repo)
432 1142eae9 2018-06-11 stsp const struct got_error *err;
433 1142eae9 2018-06-11 stsp int ncommits, wanted_id_added = 0;
435 1142eae9 2018-06-11 stsp *nfetched = 0;
437 6e0c0f9a 2018-06-11 stsp if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
438 6e0c0f9a 2018-06-11 stsp return NULL;
440 1142eae9 2018-06-11 stsp while (!wanted_id_added) {
441 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits,
442 1142eae9 2018-06-11 stsp &wanted_id_added, graph, repo, wanted_id);
444 1142eae9 2018-06-11 stsp return err;
445 1142eae9 2018-06-11 stsp if (ncommits == 0)
446 1142eae9 2018-06-11 stsp return NULL;
447 1142eae9 2018-06-11 stsp *nfetched += ncommits;
450 1142eae9 2018-06-11 stsp return NULL;
453 372ccdbb 2018-06-10 stsp static void
454 372ccdbb 2018-06-10 stsp free_graph_node(struct got_object_id *id, void *data, void *arg)
456 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node = data;
457 372ccdbb 2018-06-10 stsp while (!SIMPLEQ_EMPTY(&node->child_ids)) {
458 79f35eb3 2018-06-11 stsp struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
459 372ccdbb 2018-06-10 stsp SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
460 372ccdbb 2018-06-10 stsp free(child);
462 372ccdbb 2018-06-10 stsp free(node);
466 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
468 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->open_branches);
469 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
470 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
471 372ccdbb 2018-06-10 stsp free(graph);
474 372ccdbb 2018-06-10 stsp const struct got_error *
475 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
476 372ccdbb 2018-06-10 stsp struct got_object_id *id)
478 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *start_node;
480 372ccdbb 2018-06-10 stsp start_node = got_object_idset_get(graph->node_ids, id);
481 372ccdbb 2018-06-10 stsp if (start_node == NULL)
482 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_NO_OBJ);
484 372ccdbb 2018-06-10 stsp graph->iter_node = start_node;
485 372ccdbb 2018-06-10 stsp return NULL;
488 372ccdbb 2018-06-10 stsp const struct got_error *
489 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
490 b43fbaa0 2018-06-11 stsp struct got_commit_graph *graph)
492 9ba79e04 2018-06-11 stsp *id = NULL;
494 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
495 372ccdbb 2018-06-10 stsp /* We are done interating, or iteration was not started. */
496 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
499 9ba79e04 2018-06-11 stsp if (graph->iter_node ==
500 9ba79e04 2018-06-11 stsp TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
501 9ba79e04 2018-06-11 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
502 9ba79e04 2018-06-11 stsp *id = &graph->iter_node->id;
503 9ba79e04 2018-06-11 stsp /* We are done interating. */
504 9ba79e04 2018-06-11 stsp graph->iter_node = NULL;
505 372ccdbb 2018-06-10 stsp return NULL;
508 9ba79e04 2018-06-11 stsp if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
509 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_ITER_NEED_MORE);
511 372ccdbb 2018-06-10 stsp *id = &graph->iter_node->id;
512 9ba79e04 2018-06-11 stsp graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
513 372ccdbb 2018-06-10 stsp return NULL;