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 0ed6ed4c 2018-06-13 stsp #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
71 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
72 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
73 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
74 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
75 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
77 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
78 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
79 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
81 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
82 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
83 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
85 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
87 372ccdbb 2018-06-10 stsp /* The next commit to return when the API user asks for one. */
88 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *iter_node;
90 9ba79e04 2018-06-11 stsp /* The graph iteration list contains all nodes in sorted order. */
91 9ba79e04 2018-06-11 stsp struct got_commit_graph_iter_list iter_list;
94 372ccdbb 2018-06-10 stsp static struct got_commit_graph *
95 372ccdbb 2018-06-10 stsp alloc_graph(void)
97 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
99 372ccdbb 2018-06-10 stsp graph = calloc(1, sizeof(*graph));
100 372ccdbb 2018-06-10 stsp if (graph == NULL)
101 372ccdbb 2018-06-10 stsp return NULL;
103 d54f52f4 2018-07-07 stsp graph->node_ids = got_object_idset_alloc(
104 d54f52f4 2018-07-07 stsp GOT_OBJECT_IDSET_ITERATE_BY_OBJECT_ID);
105 372ccdbb 2018-06-10 stsp if (graph->node_ids == NULL) {
106 372ccdbb 2018-06-10 stsp free(graph);
107 372ccdbb 2018-06-10 stsp return NULL;
110 d54f52f4 2018-07-07 stsp graph->open_branches = got_object_idset_alloc(
111 d54f52f4 2018-07-07 stsp GOT_OBJECT_IDSET_ITERATE_BY_OBJECT_ID);
112 372ccdbb 2018-06-10 stsp if (graph->open_branches == NULL) {
113 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
114 372ccdbb 2018-06-10 stsp free(graph);
115 372ccdbb 2018-06-10 stsp return NULL;
118 31920504 2018-06-11 stsp TAILQ_INIT(&graph->iter_list);
119 372ccdbb 2018-06-10 stsp return graph;
124 372ccdbb 2018-06-10 stsp is_head_node(struct got_commit_graph_node *node)
126 372ccdbb 2018-06-10 stsp return node->nchildren == 0;
130 372ccdbb 2018-06-10 stsp is_merge_point(struct got_commit_graph_node *node)
132 b43fbaa0 2018-06-11 stsp return node->nparents > 1;
136 372ccdbb 2018-06-10 stsp is_branch_point(struct got_commit_graph_node *node)
138 372ccdbb 2018-06-10 stsp return node->nchildren > 1;
142 372ccdbb 2018-06-10 stsp is_root_node(struct got_commit_graph_node *node)
144 b43fbaa0 2018-06-11 stsp return node->nparents == 0;
148 4626e416 2018-06-10 stsp static void
149 9ba79e04 2018-06-11 stsp add_node_to_iter_list(struct got_commit_graph *graph,
150 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *node,
151 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *child_node)
153 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
155 31920504 2018-06-11 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
156 31920504 2018-06-11 stsp TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
160 fe8df4c2 2018-06-16 stsp /* Ensure that an iteration in progress will see this new commit. */
161 fe8df4c2 2018-06-16 stsp if (graph->iter_node) {
162 fe8df4c2 2018-06-16 stsp n = graph->iter_node;
163 fe8df4c2 2018-06-16 stsp while (n) {
164 fe8df4c2 2018-06-16 stsp next = TAILQ_NEXT(n, entry);
165 fe8df4c2 2018-06-16 stsp if (next &&
166 fe8df4c2 2018-06-16 stsp node->commit_timestamp >= next->commit_timestamp) {
167 fe8df4c2 2018-06-16 stsp TAILQ_INSERT_BEFORE(next, node, entry);
172 fe8df4c2 2018-06-16 stsp TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
173 fe8df4c2 2018-06-16 stsp node, entry);
178 58e5f354 2018-06-11 stsp * If a child node is known, begin looping over the list there
179 a37db869 2018-06-11 stsp * instead of beginning from the list head.
181 58e5f354 2018-06-11 stsp n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
183 d2312330 2018-06-11 stsp /* Insert into list based on committer timestamp. */
185 d2312330 2018-06-11 stsp if (node->commit_timestamp == n->commit_timestamp) {
186 d2312330 2018-06-11 stsp TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
188 d2312330 2018-06-11 stsp } else if (node->commit_timestamp < n->commit_timestamp) {
189 4bd3f2bb 2018-06-10 stsp next = TAILQ_NEXT(n, entry);
190 4bd3f2bb 2018-06-10 stsp if (next == NULL) {
191 31920504 2018-06-11 stsp TAILQ_INSERT_AFTER(&graph->iter_list, n,
192 4bd3f2bb 2018-06-10 stsp node, entry);
195 b43fbaa0 2018-06-11 stsp if (node->commit_timestamp >= next->commit_timestamp) {
196 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_BEFORE(next, node, entry);
200 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_BEFORE(n, node, entry);
203 9ba79e04 2018-06-11 stsp n = TAILQ_NEXT(n, entry);
204 9ba79e04 2018-06-11 stsp } while (n);
207 372ccdbb 2018-06-10 stsp static const struct got_error *
208 79f35eb3 2018-06-11 stsp add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
210 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
212 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
213 79f35eb3 2018-06-11 stsp if (qid == NULL)
214 b43fbaa0 2018-06-11 stsp return got_error_from_errno();
216 79f35eb3 2018-06-11 stsp qid->id = got_object_id_dup(id);
217 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
218 b43fbaa0 2018-06-11 stsp const struct got_error *err = got_error_from_errno();
220 b43fbaa0 2018-06-11 stsp return err;
223 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(ids, qid, entry);
224 0ed6ed4c 2018-06-13 stsp return NULL;
227 0ed6ed4c 2018-06-13 stsp static const struct got_error *
228 0ed6ed4c 2018-06-13 stsp advance_open_branches(struct got_commit_graph *graph,
229 0ed6ed4c 2018-06-13 stsp struct got_commit_graph_node *node,
230 0ed6ed4c 2018-06-13 stsp struct got_object_id *commit_id, struct got_commit_object *commit)
232 0ed6ed4c 2018-06-13 stsp const struct got_error *err;
233 0ed6ed4c 2018-06-13 stsp struct got_object_qid *qid;
235 e7c810ea 2018-06-22 stsp err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
236 0ed6ed4c 2018-06-13 stsp if (err && err->code != GOT_ERR_NO_OBJ)
237 0ed6ed4c 2018-06-13 stsp return err;
239 0ed6ed4c 2018-06-13 stsp if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
240 0ed6ed4c 2018-06-13 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
241 0ed6ed4c 2018-06-13 stsp if (qid == NULL)
242 0ed6ed4c 2018-06-13 stsp return NULL;
243 0ed6ed4c 2018-06-13 stsp err = got_object_idset_add(NULL, graph->open_branches, qid->id,
245 0ed6ed4c 2018-06-13 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
246 0ed6ed4c 2018-06-13 stsp return err;
247 0ed6ed4c 2018-06-13 stsp return NULL;
250 0ed6ed4c 2018-06-13 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
251 0ed6ed4c 2018-06-13 stsp if (got_object_idset_get(graph->node_ids, qid->id))
252 0ed6ed4c 2018-06-13 stsp continue; /* parent already traversed */
253 0ed6ed4c 2018-06-13 stsp err = got_object_idset_add(NULL, graph->open_branches,
254 0ed6ed4c 2018-06-13 stsp qid->id, node);
255 0ed6ed4c 2018-06-13 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
256 0ed6ed4c 2018-06-13 stsp return err;
259 b43fbaa0 2018-06-11 stsp return NULL;
262 b43fbaa0 2018-06-11 stsp static const struct got_error *
263 372ccdbb 2018-06-10 stsp add_node(struct got_commit_graph_node **new_node,
264 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
265 9ba79e04 2018-06-11 stsp struct got_commit_object *commit, struct got_commit_graph_node *child_node)
267 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
268 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node, *existing_node;
269 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
271 372ccdbb 2018-06-10 stsp *new_node = NULL;
273 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
274 372ccdbb 2018-06-10 stsp if (node == NULL)
275 372ccdbb 2018-06-10 stsp return got_error_from_errno();
277 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
278 b43fbaa0 2018-06-11 stsp SIMPLEQ_INIT(&node->parent_ids);
279 372ccdbb 2018-06-10 stsp SIMPLEQ_INIT(&node->child_ids);
280 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
281 79f35eb3 2018-06-11 stsp err = add_vertex(&node->parent_ids, qid->id);
283 b43fbaa0 2018-06-11 stsp return err;
284 b43fbaa0 2018-06-11 stsp node->nparents++;
286 788c352e 2018-06-16 stsp node->commit_timestamp = mktime(&commit->tm_committer);
287 788c352e 2018-06-16 stsp if (node->commit_timestamp == -1)
288 788c352e 2018-06-16 stsp return got_error_from_errno();
290 372ccdbb 2018-06-10 stsp err = got_object_idset_add((void **)(&existing_node),
291 372ccdbb 2018-06-10 stsp graph->node_ids, &node->id, node);
292 372ccdbb 2018-06-10 stsp if (err == NULL) {
293 9ba79e04 2018-06-11 stsp add_node_to_iter_list(graph, node, child_node);
294 0ed6ed4c 2018-06-13 stsp err = advance_open_branches(graph, node, commit_id, commit);
295 372ccdbb 2018-06-10 stsp *new_node = node;
296 372ccdbb 2018-06-10 stsp } else if (err->code == GOT_ERR_OBJ_EXISTS) {
297 372ccdbb 2018-06-10 stsp err = NULL;
298 372ccdbb 2018-06-10 stsp free(node);
299 372ccdbb 2018-06-10 stsp node = existing_node;
301 372ccdbb 2018-06-10 stsp free(node);
302 372ccdbb 2018-06-10 stsp return err;
305 9ba79e04 2018-06-11 stsp if (child_node) {
306 79f35eb3 2018-06-11 stsp struct got_object_qid *cid;
308 372ccdbb 2018-06-10 stsp /* Prevent linking to self. */
309 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(commit_id, &child_node->id) == 0)
310 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
312 372ccdbb 2018-06-10 stsp /* Prevent double-linking to the same child. */
313 372ccdbb 2018-06-10 stsp SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
314 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(cid->id, &child_node->id) == 0)
315 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
318 9ba79e04 2018-06-11 stsp err = add_vertex(&node->child_ids, &child_node->id);
320 372ccdbb 2018-06-10 stsp return err;
321 372ccdbb 2018-06-10 stsp node->nchildren++;
324 c4d7a9c4 2018-06-11 stsp return err;
327 c4d7a9c4 2018-06-11 stsp const struct got_error *
328 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
329 0ed6ed4c 2018-06-13 stsp struct got_object_id *commit_id, int first_parent_traversal,
330 0ed6ed4c 2018-06-13 stsp struct got_repository *repo)
332 c4d7a9c4 2018-06-11 stsp const struct got_error *err = NULL;
333 c4d7a9c4 2018-06-11 stsp struct got_commit_object *commit;
335 c4d7a9c4 2018-06-11 stsp *graph = NULL;
337 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
339 372ccdbb 2018-06-10 stsp return err;
341 372ccdbb 2018-06-10 stsp *graph = alloc_graph();
342 372ccdbb 2018-06-10 stsp if (*graph == NULL) {
343 372ccdbb 2018-06-10 stsp got_object_commit_close(commit);
344 372ccdbb 2018-06-10 stsp return got_error_from_errno();
347 0ed6ed4c 2018-06-13 stsp if (first_parent_traversal)
348 0ed6ed4c 2018-06-13 stsp (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
350 372ccdbb 2018-06-10 stsp err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
351 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
353 372ccdbb 2018-06-10 stsp got_commit_graph_close(*graph);
354 372ccdbb 2018-06-10 stsp *graph = NULL;
355 372ccdbb 2018-06-10 stsp return err;
358 372ccdbb 2018-06-10 stsp return NULL;
361 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch {
362 372ccdbb 2018-06-10 stsp struct got_object_id parent_id;
363 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node;
366 372ccdbb 2018-06-10 stsp struct gather_branches_arg {
367 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch *branches;
368 372ccdbb 2018-06-10 stsp int nbranches;
371 372ccdbb 2018-06-10 stsp static void
372 372ccdbb 2018-06-10 stsp gather_branches(struct got_object_id *id, void *data, void *arg)
374 372ccdbb 2018-06-10 stsp struct gather_branches_arg *a = arg;
375 372ccdbb 2018-06-10 stsp memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
376 372ccdbb 2018-06-10 stsp a->branches[a->nbranches].node = data;
377 372ccdbb 2018-06-10 stsp a->nbranches++;
380 1142eae9 2018-06-11 stsp static const struct got_error *
381 1142eae9 2018-06-11 stsp fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
382 1142eae9 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo,
383 1142eae9 2018-06-11 stsp struct got_object_id *wanted_id)
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;
391 1142eae9 2018-06-11 stsp if (wanted_id_added)
392 1142eae9 2018-06-11 stsp *wanted_id_added = 0;
394 372ccdbb 2018-06-10 stsp arg.nbranches = got_object_idset_num_elements(graph->open_branches);
395 372ccdbb 2018-06-10 stsp if (arg.nbranches == 0)
396 372ccdbb 2018-06-10 stsp return NULL;
399 372ccdbb 2018-06-10 stsp * Adding nodes to the graph might change the graph's open
400 372ccdbb 2018-06-10 stsp * branches state. Create a local copy of the current state.
402 372ccdbb 2018-06-10 stsp branches = calloc(arg.nbranches, sizeof(*branches));
403 372ccdbb 2018-06-10 stsp if (branches == NULL)
404 372ccdbb 2018-06-10 stsp return got_error_from_errno();
405 372ccdbb 2018-06-10 stsp arg.nbranches = 0; /* reset; gather_branches() will increment */
406 372ccdbb 2018-06-10 stsp arg.branches = branches;
407 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
409 372ccdbb 2018-06-10 stsp for (i = 0; i < arg.nbranches; i++) {
410 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
411 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *child_node, *new_node;
412 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
414 372ccdbb 2018-06-10 stsp commit_id = &branches[i].parent_id;
415 372ccdbb 2018-06-10 stsp child_node = branches[i].node;
417 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
421 9ba79e04 2018-06-11 stsp err = add_node(&new_node, graph, commit_id, commit, child_node);
422 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
424 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_OBJ_EXISTS)
426 372ccdbb 2018-06-10 stsp err = NULL;
428 372ccdbb 2018-06-10 stsp if (new_node)
429 372ccdbb 2018-06-10 stsp (*ncommits)++;
430 1142eae9 2018-06-11 stsp if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
431 1142eae9 2018-06-11 stsp *wanted_id_added = 1;
434 372ccdbb 2018-06-10 stsp free(branches);
435 372ccdbb 2018-06-10 stsp return err;
438 372ccdbb 2018-06-10 stsp const struct got_error *
439 372ccdbb 2018-06-10 stsp got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
440 372ccdbb 2018-06-10 stsp int limit, struct got_repository *repo)
442 372ccdbb 2018-06-10 stsp const struct got_error *err;
443 1142eae9 2018-06-11 stsp int ncommits;
445 372ccdbb 2018-06-10 stsp *nfetched = 0;
447 1142eae9 2018-06-11 stsp while (*nfetched < limit) {
448 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits, NULL,
449 1142eae9 2018-06-11 stsp graph, repo, NULL);
451 372ccdbb 2018-06-10 stsp return err;
452 372ccdbb 2018-06-10 stsp if (ncommits == 0)
454 1142eae9 2018-06-11 stsp *nfetched += ncommits;
457 372ccdbb 2018-06-10 stsp return NULL;
460 1142eae9 2018-06-11 stsp const struct got_error *
461 1142eae9 2018-06-11 stsp got_commit_graph_fetch_commits_up_to(int *nfetched,
462 1142eae9 2018-06-11 stsp struct got_commit_graph *graph, struct got_object_id *wanted_id,
463 1142eae9 2018-06-11 stsp struct got_repository *repo)
465 1142eae9 2018-06-11 stsp const struct got_error *err;
466 1142eae9 2018-06-11 stsp int ncommits, wanted_id_added = 0;
468 1142eae9 2018-06-11 stsp *nfetched = 0;
470 6e0c0f9a 2018-06-11 stsp if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
471 6e0c0f9a 2018-06-11 stsp return NULL;
473 1142eae9 2018-06-11 stsp while (!wanted_id_added) {
474 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits,
475 1142eae9 2018-06-11 stsp &wanted_id_added, graph, repo, wanted_id);
477 1142eae9 2018-06-11 stsp return err;
478 1142eae9 2018-06-11 stsp if (ncommits == 0)
479 1142eae9 2018-06-11 stsp return NULL;
480 1142eae9 2018-06-11 stsp *nfetched += ncommits;
483 1142eae9 2018-06-11 stsp return NULL;
486 372ccdbb 2018-06-10 stsp static void
487 372ccdbb 2018-06-10 stsp free_graph_node(struct got_object_id *id, void *data, void *arg)
489 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node = data;
490 372ccdbb 2018-06-10 stsp while (!SIMPLEQ_EMPTY(&node->child_ids)) {
491 79f35eb3 2018-06-11 stsp struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
492 372ccdbb 2018-06-10 stsp SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
493 372ccdbb 2018-06-10 stsp free(child);
495 372ccdbb 2018-06-10 stsp free(node);
499 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
501 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->open_branches);
502 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
503 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
504 372ccdbb 2018-06-10 stsp free(graph);
507 372ccdbb 2018-06-10 stsp const struct got_error *
508 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
509 372ccdbb 2018-06-10 stsp struct got_object_id *id)
511 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *start_node;
513 372ccdbb 2018-06-10 stsp start_node = got_object_idset_get(graph->node_ids, id);
514 372ccdbb 2018-06-10 stsp if (start_node == NULL)
515 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_NO_OBJ);
517 372ccdbb 2018-06-10 stsp graph->iter_node = start_node;
518 372ccdbb 2018-06-10 stsp return NULL;
521 372ccdbb 2018-06-10 stsp const struct got_error *
522 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
523 b43fbaa0 2018-06-11 stsp struct got_commit_graph *graph)
525 9ba79e04 2018-06-11 stsp *id = NULL;
527 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
528 372ccdbb 2018-06-10 stsp /* We are done interating, or iteration was not started. */
529 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
532 9ba79e04 2018-06-11 stsp if (graph->iter_node ==
533 9ba79e04 2018-06-11 stsp TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
534 9ba79e04 2018-06-11 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
535 9ba79e04 2018-06-11 stsp *id = &graph->iter_node->id;
536 9ba79e04 2018-06-11 stsp /* We are done interating. */
537 9ba79e04 2018-06-11 stsp graph->iter_node = NULL;
538 372ccdbb 2018-06-10 stsp return NULL;
541 9ba79e04 2018-06-11 stsp if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
542 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_ITER_NEED_MORE);
544 372ccdbb 2018-06-10 stsp *id = &graph->iter_node->id;
545 9ba79e04 2018-06-11 stsp graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
546 372ccdbb 2018-06-10 stsp return NULL;