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 372ccdbb 2018-06-10 stsp struct got_commit_object *commit; /* contains list of parents */
48 372ccdbb 2018-06-10 stsp int nchildren;
49 372ccdbb 2018-06-10 stsp SIMPLEQ_HEAD(, got_parent_id) child_ids;
51 372ccdbb 2018-06-10 stsp /* Used during graph iteration. */
52 372ccdbb 2018-06-10 stsp TAILQ_ENTRY(got_commit_graph_node) entry;
55 372ccdbb 2018-06-10 stsp struct got_commit_graph {
56 372ccdbb 2018-06-10 stsp /* The set of all commits we have traversed. */
57 372ccdbb 2018-06-10 stsp struct got_object_idset *node_ids;
59 372ccdbb 2018-06-10 stsp /* The commit at which traversal began (youngest commit in node_ids). */
60 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *head_node;
63 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
64 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
65 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
66 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
67 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
69 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
70 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
71 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
73 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
74 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
75 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
77 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
79 372ccdbb 2018-06-10 stsp /* The next commit to return when the API user asks for one. */
80 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *iter_node;
82 372ccdbb 2018-06-10 stsp TAILQ_HEAD(, got_commit_graph_node) iter_candidates;
85 372ccdbb 2018-06-10 stsp static struct got_commit_graph *
86 372ccdbb 2018-06-10 stsp alloc_graph(void)
88 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
90 372ccdbb 2018-06-10 stsp graph = calloc(1, sizeof(*graph));
91 372ccdbb 2018-06-10 stsp if (graph == NULL)
92 372ccdbb 2018-06-10 stsp return NULL;
94 372ccdbb 2018-06-10 stsp graph->node_ids = got_object_idset_alloc();
95 372ccdbb 2018-06-10 stsp if (graph->node_ids == NULL) {
96 372ccdbb 2018-06-10 stsp free(graph);
97 372ccdbb 2018-06-10 stsp return NULL;
100 372ccdbb 2018-06-10 stsp graph->open_branches = got_object_idset_alloc();
101 372ccdbb 2018-06-10 stsp if (graph->open_branches == NULL) {
102 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
103 372ccdbb 2018-06-10 stsp free(graph);
104 372ccdbb 2018-06-10 stsp return NULL;
107 372ccdbb 2018-06-10 stsp TAILQ_INIT(&graph->iter_candidates);
108 372ccdbb 2018-06-10 stsp return graph;
113 372ccdbb 2018-06-10 stsp is_head_node(struct got_commit_graph_node *node)
115 372ccdbb 2018-06-10 stsp return node->nchildren == 0;
119 372ccdbb 2018-06-10 stsp is_merge_point(struct got_commit_graph_node *node)
121 372ccdbb 2018-06-10 stsp return node->commit->nparents > 1;
125 372ccdbb 2018-06-10 stsp is_branch_point(struct got_commit_graph_node *node)
127 372ccdbb 2018-06-10 stsp return node->nchildren > 1;
132 372ccdbb 2018-06-10 stsp is_root_node(struct got_commit_graph_node *node)
134 372ccdbb 2018-06-10 stsp return node->commit->nparents == 0;
137 372ccdbb 2018-06-10 stsp static const struct got_error *
138 372ccdbb 2018-06-10 stsp parse_commit_time(int64_t *time, struct got_commit_object *commit)
140 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
141 372ccdbb 2018-06-10 stsp const char *errstr;
142 372ccdbb 2018-06-10 stsp char *committer, *space;
146 372ccdbb 2018-06-10 stsp committer = strdup(commit->committer);
147 372ccdbb 2018-06-10 stsp if (committer == NULL)
148 372ccdbb 2018-06-10 stsp return got_error_from_errno();
150 372ccdbb 2018-06-10 stsp /* Strip off trailing timezone indicator. */
151 372ccdbb 2018-06-10 stsp space = strrchr(committer, ' ');
152 372ccdbb 2018-06-10 stsp if (space == NULL) {
153 372ccdbb 2018-06-10 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
156 372ccdbb 2018-06-10 stsp *space = '\0';
158 372ccdbb 2018-06-10 stsp /* Timestamp is separated from committer name + email by space. */
159 372ccdbb 2018-06-10 stsp space = strrchr(committer, ' ');
160 372ccdbb 2018-06-10 stsp if (space == NULL) {
161 372ccdbb 2018-06-10 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
165 372ccdbb 2018-06-10 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
166 372ccdbb 2018-06-10 stsp if (errstr)
167 372ccdbb 2018-06-10 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
170 372ccdbb 2018-06-10 stsp free(committer);
171 372ccdbb 2018-06-10 stsp return err;
174 372ccdbb 2018-06-10 stsp static const struct got_error *
175 372ccdbb 2018-06-10 stsp compare_commits(int *cmp, struct got_commit_object *c1,
176 372ccdbb 2018-06-10 stsp struct got_commit_object *c2)
178 372ccdbb 2018-06-10 stsp const struct got_error *err;
179 372ccdbb 2018-06-10 stsp int64_t t1, t2;
181 372ccdbb 2018-06-10 stsp err = parse_commit_time(&t1, c1);
183 372ccdbb 2018-06-10 stsp return err;
184 372ccdbb 2018-06-10 stsp err = parse_commit_time(&t2, c2);
186 372ccdbb 2018-06-10 stsp return err;
188 372ccdbb 2018-06-10 stsp if (t1 == t2)
190 372ccdbb 2018-06-10 stsp else if (t1 < t2)
195 372ccdbb 2018-06-10 stsp return NULL;
198 372ccdbb 2018-06-10 stsp static const struct got_error *
199 372ccdbb 2018-06-10 stsp add_iteration_candidate(struct got_commit_graph *graph,
200 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node)
202 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *n;
204 372ccdbb 2018-06-10 stsp if (TAILQ_EMPTY(&graph->iter_candidates)) {
205 372ccdbb 2018-06-10 stsp TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
206 372ccdbb 2018-06-10 stsp return NULL;
209 372ccdbb 2018-06-10 stsp TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
210 372ccdbb 2018-06-10 stsp const struct got_error *err;
212 372ccdbb 2018-06-10 stsp err = compare_commits(&cmp, node->commit, n->commit);
214 372ccdbb 2018-06-10 stsp return err;
215 372ccdbb 2018-06-10 stsp if (cmp <= 0)
217 372ccdbb 2018-06-10 stsp TAILQ_INSERT_BEFORE(n, node, entry);
221 372ccdbb 2018-06-10 stsp return NULL;
224 372ccdbb 2018-06-10 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 372ccdbb 2018-06-10 stsp struct got_commit_object *commit, struct got_object_id *child_commit_id)
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;
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 372ccdbb 2018-06-10 stsp node->commit = commit;
240 372ccdbb 2018-06-10 stsp SIMPLEQ_INIT(&node->child_ids);
242 372ccdbb 2018-06-10 stsp err = got_object_idset_add((void **)(&existing_node),
243 372ccdbb 2018-06-10 stsp graph->node_ids, &node->id, node);
244 372ccdbb 2018-06-10 stsp if (err == NULL) {
245 372ccdbb 2018-06-10 stsp struct got_parent_id *pid;
247 372ccdbb 2018-06-10 stsp err = add_iteration_candidate(graph, node);
249 372ccdbb 2018-06-10 stsp return err;
251 372ccdbb 2018-06-10 stsp err = got_object_idset_remove(graph->open_branches, commit_id);
252 372ccdbb 2018-06-10 stsp if (err && err->code != GOT_ERR_NO_OBJ)
253 372ccdbb 2018-06-10 stsp return err;
254 372ccdbb 2018-06-10 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
255 372ccdbb 2018-06-10 stsp if (got_object_idset_get(graph->node_ids, pid->id))
256 372ccdbb 2018-06-10 stsp continue; /* parent already traversed */
257 372ccdbb 2018-06-10 stsp err = got_object_idset_add(NULL, graph->open_branches,
258 372ccdbb 2018-06-10 stsp pid->id, node);
259 372ccdbb 2018-06-10 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
260 372ccdbb 2018-06-10 stsp return err;
262 372ccdbb 2018-06-10 stsp *new_node = node;
263 372ccdbb 2018-06-10 stsp } else if (err->code == GOT_ERR_OBJ_EXISTS) {
264 372ccdbb 2018-06-10 stsp err = NULL;
265 372ccdbb 2018-06-10 stsp free(node);
266 372ccdbb 2018-06-10 stsp node = existing_node;
268 372ccdbb 2018-06-10 stsp free(node);
269 372ccdbb 2018-06-10 stsp return err;
272 372ccdbb 2018-06-10 stsp if (child_commit_id) {
273 372ccdbb 2018-06-10 stsp struct got_parent_id *child, *cid;
275 372ccdbb 2018-06-10 stsp /* Prevent linking to self. */
276 372ccdbb 2018-06-10 stsp if (got_object_id_cmp(commit_id, child_commit_id) == 0)
277 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
279 372ccdbb 2018-06-10 stsp /* Prevent double-linking to the same child. */
280 372ccdbb 2018-06-10 stsp SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
281 372ccdbb 2018-06-10 stsp if (got_object_id_cmp(cid->id, child_commit_id) == 0)
282 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
285 372ccdbb 2018-06-10 stsp child = calloc(1, sizeof(*child));
286 372ccdbb 2018-06-10 stsp if (child == NULL)
287 372ccdbb 2018-06-10 stsp return got_error_from_errno();
288 372ccdbb 2018-06-10 stsp child->id = got_object_id_dup(child_commit_id);
289 372ccdbb 2018-06-10 stsp if (child->id == NULL) {
290 372ccdbb 2018-06-10 stsp err = got_error_from_errno();
291 372ccdbb 2018-06-10 stsp free(child);
292 372ccdbb 2018-06-10 stsp return err;
294 372ccdbb 2018-06-10 stsp SIMPLEQ_INSERT_TAIL(&node->child_ids, child, entry);
295 372ccdbb 2018-06-10 stsp node->nchildren++;
298 372ccdbb 2018-06-10 stsp return err;
301 372ccdbb 2018-06-10 stsp const struct got_error *
302 372ccdbb 2018-06-10 stsp got_commit_graph_open(struct got_commit_graph **graph,
303 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id, struct got_repository *repo)
305 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
306 372ccdbb 2018-06-10 stsp struct got_object *obj;
307 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
309 372ccdbb 2018-06-10 stsp *graph = NULL;
311 372ccdbb 2018-06-10 stsp err = got_object_open(&obj, repo, commit_id);
313 372ccdbb 2018-06-10 stsp return err;
314 372ccdbb 2018-06-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
315 372ccdbb 2018-06-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
316 372ccdbb 2018-06-10 stsp got_object_close(obj);
317 372ccdbb 2018-06-10 stsp return err;
320 372ccdbb 2018-06-10 stsp err = got_object_commit_open(&commit, repo, obj);
321 372ccdbb 2018-06-10 stsp got_object_close(obj);
323 372ccdbb 2018-06-10 stsp return err;
325 372ccdbb 2018-06-10 stsp *graph = alloc_graph();
326 372ccdbb 2018-06-10 stsp if (*graph == NULL) {
327 372ccdbb 2018-06-10 stsp got_object_commit_close(commit);
328 372ccdbb 2018-06-10 stsp return got_error_from_errno();
331 372ccdbb 2018-06-10 stsp err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
333 372ccdbb 2018-06-10 stsp got_commit_graph_close(*graph);
334 372ccdbb 2018-06-10 stsp *graph = NULL;
335 372ccdbb 2018-06-10 stsp return err;
338 372ccdbb 2018-06-10 stsp return NULL;
341 372ccdbb 2018-06-10 stsp static const struct got_error *
342 372ccdbb 2018-06-10 stsp open_commit(struct got_commit_object **commit, struct got_object_id *id,
343 372ccdbb 2018-06-10 stsp struct got_repository *repo)
345 372ccdbb 2018-06-10 stsp const struct got_error *err;
346 372ccdbb 2018-06-10 stsp struct got_object *obj;
348 372ccdbb 2018-06-10 stsp err = got_object_open(&obj, repo, id);
350 372ccdbb 2018-06-10 stsp return err;
351 372ccdbb 2018-06-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
352 372ccdbb 2018-06-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
356 372ccdbb 2018-06-10 stsp err = got_object_commit_open(commit, repo, obj);
358 372ccdbb 2018-06-10 stsp got_object_close(obj);
359 372ccdbb 2018-06-10 stsp return err;
362 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch {
363 372ccdbb 2018-06-10 stsp struct got_object_id parent_id;
364 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node;
367 372ccdbb 2018-06-10 stsp struct gather_branches_arg {
368 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch *branches;
369 372ccdbb 2018-06-10 stsp int nbranches;
372 372ccdbb 2018-06-10 stsp static void
373 372ccdbb 2018-06-10 stsp gather_branches(struct got_object_id *id, void *data, void *arg)
375 372ccdbb 2018-06-10 stsp struct gather_branches_arg *a = arg;
376 372ccdbb 2018-06-10 stsp memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
377 372ccdbb 2018-06-10 stsp a->branches[a->nbranches].node = data;
378 372ccdbb 2018-06-10 stsp a->nbranches++;
381 372ccdbb 2018-06-10 stsp const struct got_error *
382 372ccdbb 2018-06-10 stsp fetch_commits_from_open_branches(int *ncommits,
383 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph, struct got_repository *repo)
385 372ccdbb 2018-06-10 stsp const struct got_error *err;
386 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch *branches;
387 372ccdbb 2018-06-10 stsp struct gather_branches_arg arg;
390 372ccdbb 2018-06-10 stsp *ncommits = 0;
392 372ccdbb 2018-06-10 stsp arg.nbranches = got_object_idset_num_elements(graph->open_branches);
393 372ccdbb 2018-06-10 stsp if (arg.nbranches == 0)
394 372ccdbb 2018-06-10 stsp return NULL;
397 372ccdbb 2018-06-10 stsp * Adding nodes to the graph might change the graph's open
398 372ccdbb 2018-06-10 stsp * branches state. Create a local copy of the current state.
400 372ccdbb 2018-06-10 stsp branches = calloc(arg.nbranches, sizeof(*branches));
401 372ccdbb 2018-06-10 stsp if (branches == NULL)
402 372ccdbb 2018-06-10 stsp return got_error_from_errno();
403 372ccdbb 2018-06-10 stsp arg.nbranches = 0; /* reset; gather_branches() will increment */
404 372ccdbb 2018-06-10 stsp arg.branches = branches;
405 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
407 372ccdbb 2018-06-10 stsp for (i = 0; i < arg.nbranches; i++) {
408 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
409 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *child_node, *new_node;
410 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
412 372ccdbb 2018-06-10 stsp commit_id = &branches[i].parent_id;
413 372ccdbb 2018-06-10 stsp child_node = branches[i].node;
415 372ccdbb 2018-06-10 stsp err = open_commit(&commit, commit_id, repo);
419 372ccdbb 2018-06-10 stsp err = add_node(&new_node, graph, commit_id, commit,
420 372ccdbb 2018-06-10 stsp &child_node->id);
422 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_OBJ_EXISTS)
424 372ccdbb 2018-06-10 stsp err = NULL;
426 372ccdbb 2018-06-10 stsp if (new_node)
427 372ccdbb 2018-06-10 stsp (*ncommits)++;
429 372ccdbb 2018-06-10 stsp got_object_commit_close(commit);
432 372ccdbb 2018-06-10 stsp free(branches);
433 372ccdbb 2018-06-10 stsp return err;
436 372ccdbb 2018-06-10 stsp const struct got_error *
437 372ccdbb 2018-06-10 stsp got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
438 372ccdbb 2018-06-10 stsp int limit, struct got_repository *repo)
440 372ccdbb 2018-06-10 stsp const struct got_error *err;
441 372ccdbb 2018-06-10 stsp int total = 0, ncommits;
443 372ccdbb 2018-06-10 stsp *nfetched = 0;
445 372ccdbb 2018-06-10 stsp while (total < limit) {
446 372ccdbb 2018-06-10 stsp err = fetch_commits_from_open_branches(&ncommits, graph, repo);
448 372ccdbb 2018-06-10 stsp return err;
449 372ccdbb 2018-06-10 stsp if (ncommits == 0)
451 372ccdbb 2018-06-10 stsp total += ncommits;
454 372ccdbb 2018-06-10 stsp *nfetched = total;
455 372ccdbb 2018-06-10 stsp return NULL;
458 372ccdbb 2018-06-10 stsp static void
459 372ccdbb 2018-06-10 stsp free_graph_node(struct got_object_id *id, void *data, void *arg)
461 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node = data;
462 372ccdbb 2018-06-10 stsp got_object_commit_close(node->commit);
463 372ccdbb 2018-06-10 stsp while (!SIMPLEQ_EMPTY(&node->child_ids)) {
464 372ccdbb 2018-06-10 stsp struct got_parent_id *child = SIMPLEQ_FIRST(&node->child_ids);
465 372ccdbb 2018-06-10 stsp SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
466 372ccdbb 2018-06-10 stsp free(child);
468 372ccdbb 2018-06-10 stsp free(node);
472 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
474 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->open_branches);
475 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
476 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
477 372ccdbb 2018-06-10 stsp free(graph);
480 372ccdbb 2018-06-10 stsp const struct got_error *
481 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
482 372ccdbb 2018-06-10 stsp struct got_object_id *id)
484 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *start_node, *node;
485 372ccdbb 2018-06-10 stsp struct got_parent_id *pid;
487 372ccdbb 2018-06-10 stsp start_node = got_object_idset_get(graph->node_ids, id);
488 372ccdbb 2018-06-10 stsp if (start_node == NULL)
489 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_NO_OBJ);
491 372ccdbb 2018-06-10 stsp graph->iter_node = start_node;
493 372ccdbb 2018-06-10 stsp while (!TAILQ_EMPTY(&graph->iter_candidates)) {
494 372ccdbb 2018-06-10 stsp node = TAILQ_FIRST(&graph->iter_candidates);
495 372ccdbb 2018-06-10 stsp TAILQ_REMOVE(&graph->iter_candidates, node, entry);
498 372ccdbb 2018-06-10 stsp /* Put all known parents of this commit on the candidate list. */
499 372ccdbb 2018-06-10 stsp SIMPLEQ_FOREACH(pid, &start_node->commit->parent_ids, entry) {
500 372ccdbb 2018-06-10 stsp node = got_object_idset_get(graph->node_ids, pid->id);
501 372ccdbb 2018-06-10 stsp if (node) {
502 372ccdbb 2018-06-10 stsp const struct got_error *err;
503 372ccdbb 2018-06-10 stsp err = add_iteration_candidate(graph, node);
505 372ccdbb 2018-06-10 stsp return err;
509 372ccdbb 2018-06-10 stsp return NULL;
512 372ccdbb 2018-06-10 stsp const struct got_error *
513 372ccdbb 2018-06-10 stsp got_commit_graph_iter_next(struct got_commit_object **commit,
514 372ccdbb 2018-06-10 stsp struct got_object_id **id, struct got_commit_graph *graph)
516 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node;
518 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
519 372ccdbb 2018-06-10 stsp /* We are done interating, or iteration was not started. */
520 372ccdbb 2018-06-10 stsp *commit = NULL;
521 372ccdbb 2018-06-10 stsp *id = NULL;
522 372ccdbb 2018-06-10 stsp return NULL;
525 372ccdbb 2018-06-10 stsp if (TAILQ_EMPTY(&graph->iter_candidates)) {
526 372ccdbb 2018-06-10 stsp if (is_root_node(graph->iter_node) &&
527 372ccdbb 2018-06-10 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
528 372ccdbb 2018-06-10 stsp *commit = graph->iter_node->commit;
529 372ccdbb 2018-06-10 stsp *id = &graph->iter_node->id;
530 372ccdbb 2018-06-10 stsp /* We are done interating. */
531 372ccdbb 2018-06-10 stsp graph->iter_node = NULL;
532 372ccdbb 2018-06-10 stsp return NULL;
534 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_ITER_NEED_MORE);
537 372ccdbb 2018-06-10 stsp *commit = graph->iter_node->commit;
538 372ccdbb 2018-06-10 stsp *id = &graph->iter_node->id;
539 372ccdbb 2018-06-10 stsp node = TAILQ_FIRST(&graph->iter_candidates);
540 372ccdbb 2018-06-10 stsp TAILQ_REMOVE(&graph->iter_candidates, node, entry);
541 372ccdbb 2018-06-10 stsp graph->iter_node = node;
542 372ccdbb 2018-06-10 stsp return NULL;
545 372ccdbb 2018-06-10 stsp const struct got_commit_object *
546 372ccdbb 2018-06-10 stsp got_commit_graph_get_commit(struct got_commit_graph *graph,
547 372ccdbb 2018-06-10 stsp struct got_object_id *id)
549 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node;
550 372ccdbb 2018-06-10 stsp node = got_object_idset_get(graph->node_ids, id);
551 372ccdbb 2018-06-10 stsp return node ? node->commit : NULL;