Blame


1 372ccdbb 2018-06-10 stsp /*
2 372ccdbb 2018-06-10 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 372ccdbb 2018-06-10 stsp *
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.
7 372ccdbb 2018-06-10 stsp *
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.
15 372ccdbb 2018-06-10 stsp */
16 372ccdbb 2018-06-10 stsp
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>
21 372ccdbb 2018-06-10 stsp
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>
28 372ccdbb 2018-06-10 stsp
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"
32 372ccdbb 2018-06-10 stsp
33 372ccdbb 2018-06-10 stsp #include "got_lib_delta.h"
34 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
35 372ccdbb 2018-06-10 stsp #include "got_lib_object.h"
36 372ccdbb 2018-06-10 stsp #include "got_lib_object_idset.h"
37 31cedeaf 2018-09-15 stsp #include "got_lib_path.h"
38 372ccdbb 2018-06-10 stsp
39 372ccdbb 2018-06-10 stsp struct got_commit_graph_node {
40 372ccdbb 2018-06-10 stsp struct got_object_id id;
41 372ccdbb 2018-06-10 stsp
42 372ccdbb 2018-06-10 stsp /*
43 372ccdbb 2018-06-10 stsp * Each graph node corresponds to a commit object.
44 f4ceb45e 2018-07-23 stsp * Graph vertices are modelled with an adjacency list.
45 f4ceb45e 2018-07-23 stsp * Adjacencies of a graph node are parent (older) commits.
46 372ccdbb 2018-06-10 stsp */
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
50 b43fbaa0 2018-06-11 stsp time_t commit_timestamp;
51 b43fbaa0 2018-06-11 stsp
52 372ccdbb 2018-06-10 stsp /* Used during graph iteration. */
53 372ccdbb 2018-06-10 stsp TAILQ_ENTRY(got_commit_graph_node) entry;
54 372ccdbb 2018-06-10 stsp };
55 372ccdbb 2018-06-10 stsp
56 9ba79e04 2018-06-11 stsp TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
57 9ba79e04 2018-06-11 stsp
58 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip {
59 b565f6f8 2018-07-23 stsp struct got_object_id id;
60 b565f6f8 2018-07-23 stsp struct got_commit_graph_node *node;
61 b565f6f8 2018-07-23 stsp };
62 b565f6f8 2018-07-23 stsp
63 372ccdbb 2018-06-10 stsp struct got_commit_graph {
64 372ccdbb 2018-06-10 stsp /* The set of all commits we have traversed. */
65 372ccdbb 2018-06-10 stsp struct got_object_idset *node_ids;
66 372ccdbb 2018-06-10 stsp
67 372ccdbb 2018-06-10 stsp /* The commit at which traversal began (youngest commit in node_ids). */
68 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *head_node;
69 372ccdbb 2018-06-10 stsp
70 0ed6ed4c 2018-06-13 stsp int flags;
71 0ed6ed4c 2018-06-13 stsp #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
72 0ed6ed4c 2018-06-13 stsp
73 372ccdbb 2018-06-10 stsp /*
74 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
75 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
76 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
77 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
78 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
79 372ccdbb 2018-06-10 stsp *
80 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
81 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
82 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
83 372ccdbb 2018-06-10 stsp *
84 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
85 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
86 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
87 372ccdbb 2018-06-10 stsp */
88 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
89 b565f6f8 2018-07-23 stsp
90 b565f6f8 2018-07-23 stsp /* Copy of known branch tips for fetch_commits_from_open_branches(). */
91 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
92 b565f6f8 2018-07-23 stsp size_t ntips;
93 8d49203e 2018-09-20 stsp #define GOT_COMMIT_GRAPH_MIN_TIPS 10 /* minimum amount of tips to allocate */
94 372ccdbb 2018-06-10 stsp
95 31cedeaf 2018-09-15 stsp /* Path of tree entry of interest to the API user. */
96 31cedeaf 2018-09-15 stsp char *path;
97 31cedeaf 2018-09-15 stsp
98 372ccdbb 2018-06-10 stsp /* The next commit to return when the API user asks for one. */
99 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *iter_node;
100 372ccdbb 2018-06-10 stsp
101 9ba79e04 2018-06-11 stsp /* The graph iteration list contains all nodes in sorted order. */
102 9ba79e04 2018-06-11 stsp struct got_commit_graph_iter_list iter_list;
103 372ccdbb 2018-06-10 stsp };
104 372ccdbb 2018-06-10 stsp
105 372ccdbb 2018-06-10 stsp static struct got_commit_graph *
106 31cedeaf 2018-09-15 stsp alloc_graph(const char *path)
107 372ccdbb 2018-06-10 stsp {
108 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
109 372ccdbb 2018-06-10 stsp
110 372ccdbb 2018-06-10 stsp graph = calloc(1, sizeof(*graph));
111 372ccdbb 2018-06-10 stsp if (graph == NULL)
112 372ccdbb 2018-06-10 stsp return NULL;
113 372ccdbb 2018-06-10 stsp
114 31cedeaf 2018-09-15 stsp graph->path = strdup(path);
115 31cedeaf 2018-09-15 stsp if (graph->path == NULL) {
116 31cedeaf 2018-09-15 stsp free(graph);
117 31cedeaf 2018-09-15 stsp return NULL;
118 31cedeaf 2018-09-15 stsp }
119 31cedeaf 2018-09-15 stsp
120 60f2eee1 2018-07-08 stsp graph->node_ids = got_object_idset_alloc();
121 372ccdbb 2018-06-10 stsp if (graph->node_ids == NULL) {
122 31cedeaf 2018-09-15 stsp free(graph->path);
123 372ccdbb 2018-06-10 stsp free(graph);
124 372ccdbb 2018-06-10 stsp return NULL;
125 372ccdbb 2018-06-10 stsp }
126 372ccdbb 2018-06-10 stsp
127 60f2eee1 2018-07-08 stsp graph->open_branches = got_object_idset_alloc();
128 372ccdbb 2018-06-10 stsp if (graph->open_branches == NULL) {
129 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
130 31cedeaf 2018-09-15 stsp free(graph->path);
131 372ccdbb 2018-06-10 stsp free(graph);
132 372ccdbb 2018-06-10 stsp return NULL;
133 372ccdbb 2018-06-10 stsp }
134 372ccdbb 2018-06-10 stsp
135 31920504 2018-06-11 stsp TAILQ_INIT(&graph->iter_list);
136 372ccdbb 2018-06-10 stsp return graph;
137 372ccdbb 2018-06-10 stsp }
138 372ccdbb 2018-06-10 stsp
139 372ccdbb 2018-06-10 stsp #if 0
140 372ccdbb 2018-06-10 stsp static int
141 372ccdbb 2018-06-10 stsp is_head_node(struct got_commit_graph_node *node)
142 372ccdbb 2018-06-10 stsp {
143 372ccdbb 2018-06-10 stsp return node->nchildren == 0;
144 372ccdbb 2018-06-10 stsp }
145 372ccdbb 2018-06-10 stsp
146 372ccdbb 2018-06-10 stsp int
147 372ccdbb 2018-06-10 stsp is_branch_point(struct got_commit_graph_node *node)
148 372ccdbb 2018-06-10 stsp {
149 372ccdbb 2018-06-10 stsp return node->nchildren > 1;
150 372ccdbb 2018-06-10 stsp }
151 372ccdbb 2018-06-10 stsp
152 372ccdbb 2018-06-10 stsp static int
153 372ccdbb 2018-06-10 stsp is_root_node(struct got_commit_graph_node *node)
154 372ccdbb 2018-06-10 stsp {
155 b43fbaa0 2018-06-11 stsp return node->nparents == 0;
156 372ccdbb 2018-06-10 stsp }
157 9ba79e04 2018-06-11 stsp #endif
158 31cedeaf 2018-09-15 stsp
159 bee6b577 2018-09-19 stsp static int
160 bee6b577 2018-09-19 stsp is_merge_point(struct got_commit_graph_node *node)
161 bee6b577 2018-09-19 stsp {
162 bee6b577 2018-09-19 stsp return node->nparents > 1;
163 bee6b577 2018-09-19 stsp }
164 bee6b577 2018-09-19 stsp
165 31cedeaf 2018-09-15 stsp static const struct got_error *
166 31cedeaf 2018-09-15 stsp detect_changed_path(int *changed, struct got_commit_object *commit,
167 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
168 31cedeaf 2018-09-15 stsp struct got_repository *repo)
169 31cedeaf 2018-09-15 stsp {
170 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
171 31cedeaf 2018-09-15 stsp struct got_commit_object *pcommit = NULL;
172 31cedeaf 2018-09-15 stsp struct got_tree_object *tree = NULL, *ptree = NULL;
173 31cedeaf 2018-09-15 stsp struct got_object_qid *pid;
174 31cedeaf 2018-09-15 stsp
175 31cedeaf 2018-09-15 stsp if (got_path_is_root_dir(path)) {
176 31cedeaf 2018-09-15 stsp *changed = 1;
177 31cedeaf 2018-09-15 stsp return NULL;
178 31cedeaf 2018-09-15 stsp }
179 31cedeaf 2018-09-15 stsp
180 31cedeaf 2018-09-15 stsp *changed = 0;
181 31cedeaf 2018-09-15 stsp
182 31cedeaf 2018-09-15 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
183 31cedeaf 2018-09-15 stsp if (err)
184 31cedeaf 2018-09-15 stsp return err;
185 31cedeaf 2018-09-15 stsp
186 31cedeaf 2018-09-15 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
187 31cedeaf 2018-09-15 stsp if (pid == NULL) {
188 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
189 31cedeaf 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
190 31cedeaf 2018-09-15 stsp if (err)
191 31cedeaf 2018-09-15 stsp err = NULL;
192 31cedeaf 2018-09-15 stsp else
193 31cedeaf 2018-09-15 stsp *changed = 1;
194 31cedeaf 2018-09-15 stsp free(obj_id);
195 31cedeaf 2018-09-15 stsp goto done;
196 31cedeaf 2018-09-15 stsp }
197 31cedeaf 2018-09-15 stsp
198 31cedeaf 2018-09-15 stsp err = got_object_open_as_commit(&pcommit, repo, pid->id);
199 31cedeaf 2018-09-15 stsp if (err)
200 31cedeaf 2018-09-15 stsp goto done;
201 31cedeaf 2018-09-15 stsp
202 31cedeaf 2018-09-15 stsp err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
203 31cedeaf 2018-09-15 stsp if (err)
204 31cedeaf 2018-09-15 stsp goto done;
205 372ccdbb 2018-06-10 stsp
206 31cedeaf 2018-09-15 stsp err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
207 31cedeaf 2018-09-15 stsp done:
208 31cedeaf 2018-09-15 stsp if (tree)
209 31cedeaf 2018-09-15 stsp got_object_tree_close(tree);
210 31cedeaf 2018-09-15 stsp if (ptree)
211 31cedeaf 2018-09-15 stsp got_object_tree_close(ptree);
212 31cedeaf 2018-09-15 stsp if (pcommit)
213 31cedeaf 2018-09-15 stsp got_object_commit_close(pcommit);
214 31cedeaf 2018-09-15 stsp return err;
215 31cedeaf 2018-09-15 stsp }
216 31cedeaf 2018-09-15 stsp
217 4626e416 2018-06-10 stsp static void
218 9ba79e04 2018-06-11 stsp add_node_to_iter_list(struct got_commit_graph *graph,
219 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *node,
220 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *child_node)
221 372ccdbb 2018-06-10 stsp {
222 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
223 1c7a5dcb 2018-09-15 stsp
224 31920504 2018-06-11 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
225 bee6b577 2018-09-19 stsp TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
226 bee6b577 2018-09-19 stsp graph->iter_node = node;
227 4626e416 2018-06-10 stsp return;
228 372ccdbb 2018-06-10 stsp }
229 372ccdbb 2018-06-10 stsp
230 bee6b577 2018-09-19 stsp n = graph->iter_node;
231 fe8df4c2 2018-06-16 stsp /* Ensure that an iteration in progress will see this new commit. */
232 bee6b577 2018-09-19 stsp while (n) {
233 bee6b577 2018-09-19 stsp next = TAILQ_NEXT(n, entry);
234 bee6b577 2018-09-19 stsp if (next && node->commit_timestamp >= next->commit_timestamp) {
235 bee6b577 2018-09-19 stsp TAILQ_INSERT_BEFORE(next, node, entry);
236 bee6b577 2018-09-19 stsp return;
237 fe8df4c2 2018-06-16 stsp }
238 bee6b577 2018-09-19 stsp n = next;
239 fe8df4c2 2018-06-16 stsp }
240 93e45b7c 2018-09-24 stsp TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
241 372ccdbb 2018-06-10 stsp }
242 372ccdbb 2018-06-10 stsp
243 372ccdbb 2018-06-10 stsp static const struct got_error *
244 79f35eb3 2018-06-11 stsp add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
245 b43fbaa0 2018-06-11 stsp {
246 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
247 b43fbaa0 2018-06-11 stsp
248 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
249 79f35eb3 2018-06-11 stsp if (qid == NULL)
250 b43fbaa0 2018-06-11 stsp return got_error_from_errno();
251 b43fbaa0 2018-06-11 stsp
252 79f35eb3 2018-06-11 stsp qid->id = got_object_id_dup(id);
253 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
254 b43fbaa0 2018-06-11 stsp const struct got_error *err = got_error_from_errno();
255 fa2f6902 2018-07-23 stsp got_object_qid_free(qid);
256 b43fbaa0 2018-06-11 stsp return err;
257 b43fbaa0 2018-06-11 stsp }
258 b43fbaa0 2018-06-11 stsp
259 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(ids, qid, entry);
260 0ed6ed4c 2018-06-13 stsp return NULL;
261 0ed6ed4c 2018-06-13 stsp }
262 0ed6ed4c 2018-06-13 stsp
263 0ed6ed4c 2018-06-13 stsp static const struct got_error *
264 31cedeaf 2018-09-15 stsp close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
265 31cedeaf 2018-09-15 stsp {
266 31cedeaf 2018-09-15 stsp const struct got_error *err;
267 31cedeaf 2018-09-15 stsp
268 31cedeaf 2018-09-15 stsp err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
269 31cedeaf 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
270 31cedeaf 2018-09-15 stsp return err;
271 31cedeaf 2018-09-15 stsp return NULL;
272 31cedeaf 2018-09-15 stsp }
273 31cedeaf 2018-09-15 stsp
274 31cedeaf 2018-09-15 stsp static const struct got_error *
275 31cedeaf 2018-09-15 stsp advance_branch(struct got_commit_graph *graph,
276 0ed6ed4c 2018-06-13 stsp struct got_commit_graph_node *node,
277 bee6b577 2018-09-19 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
278 bee6b577 2018-09-19 stsp struct got_repository *repo)
279 0ed6ed4c 2018-06-13 stsp {
280 0ed6ed4c 2018-06-13 stsp const struct got_error *err;
281 0ed6ed4c 2018-06-13 stsp struct got_object_qid *qid;
282 0ed6ed4c 2018-06-13 stsp
283 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
284 31cedeaf 2018-09-15 stsp if (err)
285 0ed6ed4c 2018-06-13 stsp return err;
286 0ed6ed4c 2018-06-13 stsp
287 0ed6ed4c 2018-06-13 stsp if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
288 0ed6ed4c 2018-06-13 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
289 0ed6ed4c 2018-06-13 stsp if (qid == NULL)
290 0ed6ed4c 2018-06-13 stsp return NULL;
291 0ed6ed4c 2018-06-13 stsp err = got_object_idset_add(NULL, graph->open_branches, qid->id,
292 0ed6ed4c 2018-06-13 stsp node);
293 0ed6ed4c 2018-06-13 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
294 0ed6ed4c 2018-06-13 stsp return err;
295 0ed6ed4c 2018-06-13 stsp return NULL;
296 bee6b577 2018-09-19 stsp }
297 bee6b577 2018-09-19 stsp
298 bee6b577 2018-09-19 stsp /*
299 bee6b577 2018-09-19 stsp * If we are graphing commits for a specific path, skip branches
300 bee6b577 2018-09-19 stsp * which do not contribute any content to this path.
301 bee6b577 2018-09-19 stsp */
302 bee6b577 2018-09-19 stsp if (is_merge_point(node) && !got_path_is_root_dir(graph->path)) {
303 bee6b577 2018-09-19 stsp struct got_object_id *id, *merged_id, *prev_id = NULL;
304 bee6b577 2018-09-19 stsp int branches_differ = 0;
305 bee6b577 2018-09-19 stsp
306 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&merged_id, repo, commit_id,
307 bee6b577 2018-09-19 stsp graph->path);
308 bee6b577 2018-09-19 stsp if (err)
309 bee6b577 2018-09-19 stsp return err;
310 bee6b577 2018-09-19 stsp
311 bee6b577 2018-09-19 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
312 bee6b577 2018-09-19 stsp if (got_object_idset_get(graph->node_ids, qid->id))
313 bee6b577 2018-09-19 stsp continue; /* parent already traversed */
314 bee6b577 2018-09-19 stsp
315 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&id, repo, qid->id,
316 bee6b577 2018-09-19 stsp graph->path);
317 bee6b577 2018-09-19 stsp if (err) {
318 bee6b577 2018-09-19 stsp if (err->code == GOT_ERR_NO_OBJ) {
319 bee6b577 2018-09-19 stsp branches_differ = 1;
320 bee6b577 2018-09-19 stsp continue;
321 bee6b577 2018-09-19 stsp }
322 bee6b577 2018-09-19 stsp return err;
323 bee6b577 2018-09-19 stsp }
324 bee6b577 2018-09-19 stsp
325 bee6b577 2018-09-19 stsp if (prev_id) {
326 bee6b577 2018-09-19 stsp if (!branches_differ &&
327 bee6b577 2018-09-19 stsp got_object_id_cmp(merged_id, prev_id) != 0)
328 bee6b577 2018-09-19 stsp branches_differ = 1;
329 bee6b577 2018-09-19 stsp } else
330 bee6b577 2018-09-19 stsp prev_id = id;
331 bee6b577 2018-09-19 stsp
332 bee6b577 2018-09-19 stsp /*
333 bee6b577 2018-09-19 stsp * If a branch has created the merged content we can
334 bee6b577 2018-09-19 stsp * skip any other branches.
335 bee6b577 2018-09-19 stsp */
336 bee6b577 2018-09-19 stsp if (got_object_id_cmp(merged_id, id) == 0) {
337 bee6b577 2018-09-19 stsp err = got_object_idset_add(NULL,
338 bee6b577 2018-09-19 stsp graph->open_branches, qid->id, node);
339 bee6b577 2018-09-19 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
340 bee6b577 2018-09-19 stsp return err;
341 bee6b577 2018-09-19 stsp return NULL;
342 bee6b577 2018-09-19 stsp }
343 bee6b577 2018-09-19 stsp }
344 bee6b577 2018-09-19 stsp
345 bee6b577 2018-09-19 stsp /*
346 bee6b577 2018-09-19 stsp * If the path's content is the same on all branches,
347 bee6b577 2018-09-19 stsp * follow the first parent only.
348 bee6b577 2018-09-19 stsp */
349 bee6b577 2018-09-19 stsp if (!branches_differ) {
350 bee6b577 2018-09-19 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
351 bee6b577 2018-09-19 stsp if (qid == NULL)
352 bee6b577 2018-09-19 stsp return NULL;
353 bee6b577 2018-09-19 stsp if (got_object_idset_get(graph->node_ids, qid->id))
354 bee6b577 2018-09-19 stsp return NULL; /* parent already traversed */
355 bee6b577 2018-09-19 stsp err = got_object_idset_add(NULL,
356 bee6b577 2018-09-19 stsp graph->open_branches, qid->id, node);
357 bee6b577 2018-09-19 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
358 bee6b577 2018-09-19 stsp return err;
359 bee6b577 2018-09-19 stsp return NULL;
360 bee6b577 2018-09-19 stsp }
361 0ed6ed4c 2018-06-13 stsp }
362 0ed6ed4c 2018-06-13 stsp
363 0ed6ed4c 2018-06-13 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
364 0ed6ed4c 2018-06-13 stsp if (got_object_idset_get(graph->node_ids, qid->id))
365 0ed6ed4c 2018-06-13 stsp continue; /* parent already traversed */
366 0ed6ed4c 2018-06-13 stsp err = got_object_idset_add(NULL, graph->open_branches,
367 0ed6ed4c 2018-06-13 stsp qid->id, node);
368 0ed6ed4c 2018-06-13 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
369 0ed6ed4c 2018-06-13 stsp return err;
370 0ed6ed4c 2018-06-13 stsp }
371 0ed6ed4c 2018-06-13 stsp
372 b43fbaa0 2018-06-11 stsp return NULL;
373 b43fbaa0 2018-06-11 stsp }
374 b43fbaa0 2018-06-11 stsp
375 fb5b9d5a 2018-07-23 stsp static void
376 fb5b9d5a 2018-07-23 stsp free_node(struct got_commit_graph_node *node)
377 fb5b9d5a 2018-07-23 stsp {
378 fb5b9d5a 2018-07-23 stsp while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
379 fb5b9d5a 2018-07-23 stsp struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
380 fb5b9d5a 2018-07-23 stsp SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
381 fa2f6902 2018-07-23 stsp got_object_qid_free(pid);
382 fb5b9d5a 2018-07-23 stsp }
383 fb5b9d5a 2018-07-23 stsp free(node);
384 fb5b9d5a 2018-07-23 stsp }
385 fb5b9d5a 2018-07-23 stsp
386 b43fbaa0 2018-06-11 stsp static const struct got_error *
387 372ccdbb 2018-06-10 stsp add_node(struct got_commit_graph_node **new_node,
388 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
389 31cedeaf 2018-09-15 stsp struct got_commit_object *commit, struct got_commit_graph_node *child_node,
390 31cedeaf 2018-09-15 stsp struct got_repository *repo)
391 372ccdbb 2018-06-10 stsp {
392 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
393 f4ceb45e 2018-07-23 stsp struct got_commit_graph_node *node;
394 fa2f6902 2018-07-23 stsp struct got_object_qid *pid;
395 31cedeaf 2018-09-15 stsp int changed = 0, branch_done = 0;
396 372ccdbb 2018-06-10 stsp
397 372ccdbb 2018-06-10 stsp *new_node = NULL;
398 372ccdbb 2018-06-10 stsp
399 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
400 372ccdbb 2018-06-10 stsp if (node == NULL)
401 372ccdbb 2018-06-10 stsp return got_error_from_errno();
402 372ccdbb 2018-06-10 stsp
403 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
404 b43fbaa0 2018-06-11 stsp SIMPLEQ_INIT(&node->parent_ids);
405 fa2f6902 2018-07-23 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
406 fa2f6902 2018-07-23 stsp err = add_vertex(&node->parent_ids, pid->id);
407 4e845d01 2018-07-22 stsp if (err) {
408 fb5b9d5a 2018-07-23 stsp free_node(node);
409 b43fbaa0 2018-06-11 stsp return err;
410 4e845d01 2018-07-22 stsp }
411 b43fbaa0 2018-06-11 stsp node->nparents++;
412 b43fbaa0 2018-06-11 stsp }
413 372ccdbb 2018-06-10 stsp
414 f4ceb45e 2018-07-23 stsp err = got_object_idset_add(NULL, graph->node_ids, &node->id, node);
415 f4ceb45e 2018-07-23 stsp if (err) {
416 f4ceb45e 2018-07-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS)
417 f4ceb45e 2018-07-23 stsp err = NULL;
418 fb5b9d5a 2018-07-23 stsp free_node(node);
419 372ccdbb 2018-06-10 stsp return err;
420 372ccdbb 2018-06-10 stsp }
421 372ccdbb 2018-06-10 stsp
422 31cedeaf 2018-09-15 stsp err = detect_changed_path(&changed, commit, commit_id, graph->path,
423 31cedeaf 2018-09-15 stsp repo);
424 31cedeaf 2018-09-15 stsp if (err) {
425 31cedeaf 2018-09-15 stsp if (err->code == GOT_ERR_NO_OBJ) {
426 31cedeaf 2018-09-15 stsp /*
427 31cedeaf 2018-09-15 stsp * History of the path stops here on the current
428 31cedeaf 2018-09-15 stsp * branch. Keep going on other branches.
429 31cedeaf 2018-09-15 stsp */
430 31cedeaf 2018-09-15 stsp err = NULL;
431 31cedeaf 2018-09-15 stsp branch_done = 1;
432 31cedeaf 2018-09-15 stsp } else {
433 31cedeaf 2018-09-15 stsp free_node(node);
434 31cedeaf 2018-09-15 stsp return err;
435 31cedeaf 2018-09-15 stsp }
436 31cedeaf 2018-09-15 stsp }
437 31cedeaf 2018-09-15 stsp
438 bee6b577 2018-09-19 stsp node->commit_timestamp = mktime(&commit->tm_committer);
439 bee6b577 2018-09-19 stsp if (node->commit_timestamp == -1) {
440 bee6b577 2018-09-19 stsp free_node(node);
441 bee6b577 2018-09-19 stsp return got_error_from_errno();
442 1c7a5dcb 2018-09-15 stsp }
443 31cedeaf 2018-09-15 stsp
444 bee6b577 2018-09-19 stsp if (changed && !is_merge_point(node))
445 bee6b577 2018-09-19 stsp add_node_to_iter_list(graph, node, child_node);
446 bee6b577 2018-09-19 stsp
447 31cedeaf 2018-09-15 stsp if (branch_done)
448 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
449 31cedeaf 2018-09-15 stsp else
450 bee6b577 2018-09-19 stsp err = advance_branch(graph, node, commit_id, commit, repo);
451 f4ceb45e 2018-07-23 stsp if (err)
452 f4ceb45e 2018-07-23 stsp free_node(node);
453 f4ceb45e 2018-07-23 stsp else
454 f4ceb45e 2018-07-23 stsp *new_node = node;
455 372ccdbb 2018-06-10 stsp
456 c4d7a9c4 2018-06-11 stsp return err;
457 c4d7a9c4 2018-06-11 stsp }
458 c4d7a9c4 2018-06-11 stsp
459 c4d7a9c4 2018-06-11 stsp const struct got_error *
460 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
461 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
462 31cedeaf 2018-09-15 stsp int first_parent_traversal, struct got_repository *repo)
463 c4d7a9c4 2018-06-11 stsp {
464 c4d7a9c4 2018-06-11 stsp const struct got_error *err = NULL;
465 c4d7a9c4 2018-06-11 stsp struct got_commit_object *commit;
466 c4d7a9c4 2018-06-11 stsp
467 c4d7a9c4 2018-06-11 stsp *graph = NULL;
468 c4d7a9c4 2018-06-11 stsp
469 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
470 372ccdbb 2018-06-10 stsp if (err)
471 372ccdbb 2018-06-10 stsp return err;
472 372ccdbb 2018-06-10 stsp
473 31cedeaf 2018-09-15 stsp /* The path must exist in our initial commit. */
474 31cedeaf 2018-09-15 stsp if (!got_path_is_root_dir(path)) {
475 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
476 31cedeaf 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
477 31cedeaf 2018-09-15 stsp if (err)
478 31cedeaf 2018-09-15 stsp return err;
479 31cedeaf 2018-09-15 stsp free(obj_id);
480 31cedeaf 2018-09-15 stsp }
481 31cedeaf 2018-09-15 stsp
482 31cedeaf 2018-09-15 stsp *graph = alloc_graph(path);
483 372ccdbb 2018-06-10 stsp if (*graph == NULL) {
484 372ccdbb 2018-06-10 stsp got_object_commit_close(commit);
485 372ccdbb 2018-06-10 stsp return got_error_from_errno();
486 372ccdbb 2018-06-10 stsp }
487 372ccdbb 2018-06-10 stsp
488 0ed6ed4c 2018-06-13 stsp if (first_parent_traversal)
489 0ed6ed4c 2018-06-13 stsp (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
490 0ed6ed4c 2018-06-13 stsp
491 31cedeaf 2018-09-15 stsp err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
492 31cedeaf 2018-09-15 stsp repo);
493 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
494 372ccdbb 2018-06-10 stsp if (err) {
495 372ccdbb 2018-06-10 stsp got_commit_graph_close(*graph);
496 372ccdbb 2018-06-10 stsp *graph = NULL;
497 372ccdbb 2018-06-10 stsp return err;
498 372ccdbb 2018-06-10 stsp }
499 372ccdbb 2018-06-10 stsp
500 372ccdbb 2018-06-10 stsp return NULL;
501 372ccdbb 2018-06-10 stsp }
502 372ccdbb 2018-06-10 stsp
503 b565f6f8 2018-07-23 stsp struct gather_branch_tips_arg {
504 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
505 b565f6f8 2018-07-23 stsp int ntips;
506 372ccdbb 2018-06-10 stsp };
507 372ccdbb 2018-06-10 stsp
508 372ccdbb 2018-06-10 stsp static void
509 b565f6f8 2018-07-23 stsp gather_branch_tips(struct got_object_id *id, void *data, void *arg)
510 372ccdbb 2018-06-10 stsp {
511 b565f6f8 2018-07-23 stsp struct gather_branch_tips_arg *a = arg;
512 b565f6f8 2018-07-23 stsp memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
513 b565f6f8 2018-07-23 stsp a->tips[a->ntips].node = data;
514 b565f6f8 2018-07-23 stsp a->ntips++;
515 372ccdbb 2018-06-10 stsp }
516 372ccdbb 2018-06-10 stsp
517 1142eae9 2018-06-11 stsp static const struct got_error *
518 1142eae9 2018-06-11 stsp fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
519 31cedeaf 2018-09-15 stsp struct got_object_id **changed_id, struct got_commit_graph *graph,
520 31cedeaf 2018-09-15 stsp struct got_repository *repo, struct got_object_id *wanted_id)
521 372ccdbb 2018-06-10 stsp {
522 372ccdbb 2018-06-10 stsp const struct got_error *err;
523 b565f6f8 2018-07-23 stsp struct gather_branch_tips_arg arg;
524 372ccdbb 2018-06-10 stsp int i;
525 372ccdbb 2018-06-10 stsp
526 372ccdbb 2018-06-10 stsp *ncommits = 0;
527 1142eae9 2018-06-11 stsp if (wanted_id_added)
528 1142eae9 2018-06-11 stsp *wanted_id_added = 0;
529 31cedeaf 2018-09-15 stsp if (changed_id)
530 31cedeaf 2018-09-15 stsp *changed_id = NULL;
531 372ccdbb 2018-06-10 stsp
532 b565f6f8 2018-07-23 stsp arg.ntips = got_object_idset_num_elements(graph->open_branches);
533 b565f6f8 2018-07-23 stsp if (arg.ntips == 0)
534 372ccdbb 2018-06-10 stsp return NULL;
535 372ccdbb 2018-06-10 stsp
536 372ccdbb 2018-06-10 stsp /*
537 372ccdbb 2018-06-10 stsp * Adding nodes to the graph might change the graph's open
538 372ccdbb 2018-06-10 stsp * branches state. Create a local copy of the current state.
539 372ccdbb 2018-06-10 stsp */
540 b565f6f8 2018-07-23 stsp if (graph->ntips < arg.ntips) {
541 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
542 b565f6f8 2018-07-23 stsp if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
543 b565f6f8 2018-07-23 stsp arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
544 b565f6f8 2018-07-23 stsp tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
545 b565f6f8 2018-07-23 stsp if (tips == NULL)
546 b565f6f8 2018-07-23 stsp return got_error_from_errno();
547 b565f6f8 2018-07-23 stsp graph->tips = tips;
548 b565f6f8 2018-07-23 stsp graph->ntips = arg.ntips;
549 b565f6f8 2018-07-23 stsp }
550 b565f6f8 2018-07-23 stsp arg.ntips = 0; /* reset; gather_branch_tips() will increment */
551 b565f6f8 2018-07-23 stsp arg.tips = graph->tips;
552 b565f6f8 2018-07-23 stsp got_object_idset_for_each(graph->open_branches,
553 b565f6f8 2018-07-23 stsp gather_branch_tips, &arg);
554 372ccdbb 2018-06-10 stsp
555 b565f6f8 2018-07-23 stsp for (i = 0; i < arg.ntips; i++) {
556 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
557 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *child_node, *new_node;
558 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
559 31cedeaf 2018-09-15 stsp int changed;
560 372ccdbb 2018-06-10 stsp
561 b565f6f8 2018-07-23 stsp commit_id = &graph->tips[i].id;
562 b565f6f8 2018-07-23 stsp child_node = graph->tips[i].node;
563 372ccdbb 2018-06-10 stsp
564 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
565 372ccdbb 2018-06-10 stsp if (err)
566 372ccdbb 2018-06-10 stsp break;
567 372ccdbb 2018-06-10 stsp
568 31cedeaf 2018-09-15 stsp err = detect_changed_path(&changed, commit, commit_id,
569 31cedeaf 2018-09-15 stsp graph->path, repo);
570 31cedeaf 2018-09-15 stsp if (err) {
571 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
572 31cedeaf 2018-09-15 stsp if (err->code != GOT_ERR_NO_OBJ)
573 31cedeaf 2018-09-15 stsp break;
574 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
575 31cedeaf 2018-09-15 stsp if (err)
576 31cedeaf 2018-09-15 stsp break;
577 31cedeaf 2018-09-15 stsp continue;
578 31cedeaf 2018-09-15 stsp }
579 31cedeaf 2018-09-15 stsp if (changed && changed_id && *changed_id == NULL)
580 31cedeaf 2018-09-15 stsp *changed_id = commit_id;
581 31cedeaf 2018-09-15 stsp err = add_node(&new_node, graph, commit_id, commit, child_node,
582 31cedeaf 2018-09-15 stsp repo);
583 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
584 cb352812 2018-07-22 stsp if (err)
585 cb352812 2018-07-22 stsp break;
586 372ccdbb 2018-06-10 stsp if (new_node)
587 372ccdbb 2018-06-10 stsp (*ncommits)++;
588 1142eae9 2018-06-11 stsp if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
589 1142eae9 2018-06-11 stsp *wanted_id_added = 1;
590 372ccdbb 2018-06-10 stsp }
591 372ccdbb 2018-06-10 stsp
592 372ccdbb 2018-06-10 stsp return err;
593 372ccdbb 2018-06-10 stsp }
594 372ccdbb 2018-06-10 stsp
595 372ccdbb 2018-06-10 stsp const struct got_error *
596 31cedeaf 2018-09-15 stsp got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
597 31cedeaf 2018-09-15 stsp struct got_repository *repo)
598 372ccdbb 2018-06-10 stsp {
599 372ccdbb 2018-06-10 stsp const struct got_error *err;
600 31cedeaf 2018-09-15 stsp int nfetched = 0, ncommits;
601 31cedeaf 2018-09-15 stsp struct got_object_id *changed_id = NULL;
602 372ccdbb 2018-06-10 stsp
603 31cedeaf 2018-09-15 stsp while (nfetched < limit) {
604 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits, NULL,
605 31cedeaf 2018-09-15 stsp &changed_id, graph, repo, NULL);
606 372ccdbb 2018-06-10 stsp if (err)
607 372ccdbb 2018-06-10 stsp return err;
608 372ccdbb 2018-06-10 stsp if (ncommits == 0)
609 372ccdbb 2018-06-10 stsp break;
610 31cedeaf 2018-09-15 stsp if (changed_id)
611 31cedeaf 2018-09-15 stsp nfetched += ncommits;
612 372ccdbb 2018-06-10 stsp }
613 372ccdbb 2018-06-10 stsp
614 372ccdbb 2018-06-10 stsp return NULL;
615 372ccdbb 2018-06-10 stsp }
616 1142eae9 2018-06-11 stsp
617 372ccdbb 2018-06-10 stsp static void
618 fb5b9d5a 2018-07-23 stsp free_node_iter(struct got_object_id *id, void *data, void *arg)
619 372ccdbb 2018-06-10 stsp {
620 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node = data;
621 fb5b9d5a 2018-07-23 stsp free_node(node);
622 372ccdbb 2018-06-10 stsp }
623 372ccdbb 2018-06-10 stsp
624 372ccdbb 2018-06-10 stsp void
625 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
626 372ccdbb 2018-06-10 stsp {
627 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->open_branches);
628 fb5b9d5a 2018-07-23 stsp got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
629 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
630 b565f6f8 2018-07-23 stsp free(graph->tips);
631 31cedeaf 2018-09-15 stsp free(graph->path);
632 372ccdbb 2018-06-10 stsp free(graph);
633 372ccdbb 2018-06-10 stsp }
634 372ccdbb 2018-06-10 stsp
635 372ccdbb 2018-06-10 stsp const struct got_error *
636 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
637 31cedeaf 2018-09-15 stsp struct got_object_id *id, struct got_repository *repo)
638 372ccdbb 2018-06-10 stsp {
639 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
640 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *start_node;
641 31cedeaf 2018-09-15 stsp struct got_commit_object *commit;
642 31cedeaf 2018-09-15 stsp int changed;
643 372ccdbb 2018-06-10 stsp
644 372ccdbb 2018-06-10 stsp start_node = got_object_idset_get(graph->node_ids, id);
645 372ccdbb 2018-06-10 stsp if (start_node == NULL)
646 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_NO_OBJ);
647 372ccdbb 2018-06-10 stsp
648 31cedeaf 2018-09-15 stsp err = got_object_open_as_commit(&commit, repo, &start_node->id);
649 31cedeaf 2018-09-15 stsp if (err)
650 31cedeaf 2018-09-15 stsp return err;
651 31cedeaf 2018-09-15 stsp
652 31cedeaf 2018-09-15 stsp err = detect_changed_path(&changed, commit, &start_node->id,
653 31cedeaf 2018-09-15 stsp graph->path, repo);
654 31cedeaf 2018-09-15 stsp if (err) {
655 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
656 31cedeaf 2018-09-15 stsp return err;
657 31cedeaf 2018-09-15 stsp }
658 31cedeaf 2018-09-15 stsp
659 31cedeaf 2018-09-15 stsp if (!changed) {
660 31cedeaf 2018-09-15 stsp /* Locate first commit which changed graph->path. */
661 31cedeaf 2018-09-15 stsp struct got_object_id *changed_id = NULL;
662 31cedeaf 2018-09-15 stsp while (changed_id == NULL) {
663 31cedeaf 2018-09-15 stsp int ncommits;
664 31cedeaf 2018-09-15 stsp err = fetch_commits_from_open_branches(&ncommits, NULL,
665 31cedeaf 2018-09-15 stsp &changed_id, graph, repo, NULL);
666 31cedeaf 2018-09-15 stsp if (err) {
667 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
668 31cedeaf 2018-09-15 stsp return err;
669 31cedeaf 2018-09-15 stsp }
670 31cedeaf 2018-09-15 stsp }
671 31cedeaf 2018-09-15 stsp start_node = got_object_idset_get(graph->node_ids, changed_id);
672 31cedeaf 2018-09-15 stsp }
673 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
674 31cedeaf 2018-09-15 stsp
675 372ccdbb 2018-06-10 stsp graph->iter_node = start_node;
676 372ccdbb 2018-06-10 stsp return NULL;
677 372ccdbb 2018-06-10 stsp }
678 372ccdbb 2018-06-10 stsp
679 372ccdbb 2018-06-10 stsp const struct got_error *
680 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
681 b43fbaa0 2018-06-11 stsp struct got_commit_graph *graph)
682 372ccdbb 2018-06-10 stsp {
683 9ba79e04 2018-06-11 stsp *id = NULL;
684 372ccdbb 2018-06-10 stsp
685 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
686 2c7f8870 2018-09-19 stsp /* We are done iterating, or iteration was not started. */
687 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
688 9ba79e04 2018-06-11 stsp }
689 9ba79e04 2018-06-11 stsp
690 9ba79e04 2018-06-11 stsp if (graph->iter_node ==
691 9ba79e04 2018-06-11 stsp TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
692 9ba79e04 2018-06-11 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
693 2c7f8870 2018-09-19 stsp /* We are done iterating. */
694 6bc66952 2018-09-19 stsp *id = &graph->iter_node->id;
695 9ba79e04 2018-06-11 stsp graph->iter_node = NULL;
696 6bc66952 2018-09-19 stsp return NULL;
697 372ccdbb 2018-06-10 stsp }
698 372ccdbb 2018-06-10 stsp
699 9ba79e04 2018-06-11 stsp if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
700 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_ITER_NEED_MORE);
701 372ccdbb 2018-06-10 stsp
702 6bc66952 2018-09-19 stsp *id = &graph->iter_node->id;
703 6bc66952 2018-09-19 stsp graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
704 372ccdbb 2018-06-10 stsp return NULL;
705 372ccdbb 2018-06-10 stsp }