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 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"
37 372ccdbb 2018-06-10 stsp
38 372ccdbb 2018-06-10 stsp struct got_commit_graph_node {
39 372ccdbb 2018-06-10 stsp struct got_object_id id;
40 372ccdbb 2018-06-10 stsp
41 372ccdbb 2018-06-10 stsp /*
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.
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 int nchildren;
50 79f35eb3 2018-06-11 stsp struct got_object_id_queue child_ids;
51 372ccdbb 2018-06-10 stsp
52 b43fbaa0 2018-06-11 stsp time_t commit_timestamp;
53 b43fbaa0 2018-06-11 stsp
54 372ccdbb 2018-06-10 stsp /* Used during graph iteration. */
55 372ccdbb 2018-06-10 stsp TAILQ_ENTRY(got_commit_graph_node) entry;
56 372ccdbb 2018-06-10 stsp };
57 372ccdbb 2018-06-10 stsp
58 372ccdbb 2018-06-10 stsp struct got_commit_graph {
59 372ccdbb 2018-06-10 stsp /* The set of all commits we have traversed. */
60 372ccdbb 2018-06-10 stsp struct got_object_idset *node_ids;
61 372ccdbb 2018-06-10 stsp
62 372ccdbb 2018-06-10 stsp /* The commit at which traversal began (youngest commit in node_ids). */
63 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *head_node;
64 372ccdbb 2018-06-10 stsp
65 372ccdbb 2018-06-10 stsp /*
66 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
67 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
68 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
69 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
70 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
71 372ccdbb 2018-06-10 stsp *
72 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
73 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
74 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
75 372ccdbb 2018-06-10 stsp *
76 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
77 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
78 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
79 372ccdbb 2018-06-10 stsp */
80 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
81 372ccdbb 2018-06-10 stsp
82 372ccdbb 2018-06-10 stsp /* The next commit to return when the API user asks for one. */
83 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *iter_node;
84 372ccdbb 2018-06-10 stsp
85 372ccdbb 2018-06-10 stsp TAILQ_HEAD(, got_commit_graph_node) iter_candidates;
86 372ccdbb 2018-06-10 stsp };
87 372ccdbb 2018-06-10 stsp
88 372ccdbb 2018-06-10 stsp static struct got_commit_graph *
89 372ccdbb 2018-06-10 stsp alloc_graph(void)
90 372ccdbb 2018-06-10 stsp {
91 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
92 372ccdbb 2018-06-10 stsp
93 372ccdbb 2018-06-10 stsp graph = calloc(1, sizeof(*graph));
94 372ccdbb 2018-06-10 stsp if (graph == NULL)
95 372ccdbb 2018-06-10 stsp return NULL;
96 372ccdbb 2018-06-10 stsp
97 372ccdbb 2018-06-10 stsp graph->node_ids = got_object_idset_alloc();
98 372ccdbb 2018-06-10 stsp if (graph->node_ids == NULL) {
99 372ccdbb 2018-06-10 stsp free(graph);
100 372ccdbb 2018-06-10 stsp return NULL;
101 372ccdbb 2018-06-10 stsp }
102 372ccdbb 2018-06-10 stsp
103 372ccdbb 2018-06-10 stsp graph->open_branches = got_object_idset_alloc();
104 372ccdbb 2018-06-10 stsp if (graph->open_branches == NULL) {
105 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
106 372ccdbb 2018-06-10 stsp free(graph);
107 372ccdbb 2018-06-10 stsp return NULL;
108 372ccdbb 2018-06-10 stsp }
109 372ccdbb 2018-06-10 stsp
110 372ccdbb 2018-06-10 stsp TAILQ_INIT(&graph->iter_candidates);
111 372ccdbb 2018-06-10 stsp return graph;
112 372ccdbb 2018-06-10 stsp }
113 372ccdbb 2018-06-10 stsp
114 372ccdbb 2018-06-10 stsp #if 0
115 372ccdbb 2018-06-10 stsp static int
116 372ccdbb 2018-06-10 stsp is_head_node(struct got_commit_graph_node *node)
117 372ccdbb 2018-06-10 stsp {
118 372ccdbb 2018-06-10 stsp return node->nchildren == 0;
119 372ccdbb 2018-06-10 stsp }
120 372ccdbb 2018-06-10 stsp
121 372ccdbb 2018-06-10 stsp static int
122 372ccdbb 2018-06-10 stsp is_merge_point(struct got_commit_graph_node *node)
123 372ccdbb 2018-06-10 stsp {
124 b43fbaa0 2018-06-11 stsp return node->nparents > 1;
125 372ccdbb 2018-06-10 stsp }
126 372ccdbb 2018-06-10 stsp
127 372ccdbb 2018-06-10 stsp int
128 372ccdbb 2018-06-10 stsp is_branch_point(struct got_commit_graph_node *node)
129 372ccdbb 2018-06-10 stsp {
130 372ccdbb 2018-06-10 stsp return node->nchildren > 1;
131 372ccdbb 2018-06-10 stsp }
132 372ccdbb 2018-06-10 stsp #endif
133 372ccdbb 2018-06-10 stsp
134 372ccdbb 2018-06-10 stsp static int
135 372ccdbb 2018-06-10 stsp is_root_node(struct got_commit_graph_node *node)
136 372ccdbb 2018-06-10 stsp {
137 b43fbaa0 2018-06-11 stsp return node->nparents == 0;
138 372ccdbb 2018-06-10 stsp }
139 372ccdbb 2018-06-10 stsp
140 4626e416 2018-06-10 stsp static void
141 372ccdbb 2018-06-10 stsp add_iteration_candidate(struct got_commit_graph *graph,
142 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node)
143 372ccdbb 2018-06-10 stsp {
144 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
145 372ccdbb 2018-06-10 stsp
146 372ccdbb 2018-06-10 stsp if (TAILQ_EMPTY(&graph->iter_candidates)) {
147 372ccdbb 2018-06-10 stsp TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
148 4626e416 2018-06-10 stsp return;
149 372ccdbb 2018-06-10 stsp }
150 372ccdbb 2018-06-10 stsp
151 372ccdbb 2018-06-10 stsp TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
152 b43fbaa0 2018-06-11 stsp if (node->commit_timestamp < n->commit_timestamp) {
153 4bd3f2bb 2018-06-10 stsp next = TAILQ_NEXT(n, entry);
154 4bd3f2bb 2018-06-10 stsp if (next == NULL) {
155 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_AFTER(&graph->iter_candidates, n,
156 4bd3f2bb 2018-06-10 stsp node, entry);
157 4bd3f2bb 2018-06-10 stsp break;
158 4bd3f2bb 2018-06-10 stsp }
159 b43fbaa0 2018-06-11 stsp if (node->commit_timestamp >= next->commit_timestamp) {
160 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_BEFORE(next, node, entry);
161 4bd3f2bb 2018-06-10 stsp break;
162 4bd3f2bb 2018-06-10 stsp }
163 4bd3f2bb 2018-06-10 stsp } else {
164 4bd3f2bb 2018-06-10 stsp TAILQ_INSERT_BEFORE(n, node, entry);
165 4bd3f2bb 2018-06-10 stsp break;
166 4bd3f2bb 2018-06-10 stsp }
167 372ccdbb 2018-06-10 stsp }
168 372ccdbb 2018-06-10 stsp }
169 372ccdbb 2018-06-10 stsp
170 372ccdbb 2018-06-10 stsp static const struct got_error *
171 79f35eb3 2018-06-11 stsp add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
172 b43fbaa0 2018-06-11 stsp {
173 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
174 b43fbaa0 2018-06-11 stsp
175 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
176 79f35eb3 2018-06-11 stsp if (qid == NULL)
177 b43fbaa0 2018-06-11 stsp return got_error_from_errno();
178 b43fbaa0 2018-06-11 stsp
179 79f35eb3 2018-06-11 stsp qid->id = got_object_id_dup(id);
180 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
181 b43fbaa0 2018-06-11 stsp const struct got_error *err = got_error_from_errno();
182 79f35eb3 2018-06-11 stsp free(qid);
183 b43fbaa0 2018-06-11 stsp return err;
184 b43fbaa0 2018-06-11 stsp }
185 b43fbaa0 2018-06-11 stsp
186 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(ids, qid, entry);
187 b43fbaa0 2018-06-11 stsp return NULL;
188 b43fbaa0 2018-06-11 stsp }
189 b43fbaa0 2018-06-11 stsp
190 b43fbaa0 2018-06-11 stsp static const struct got_error *
191 372ccdbb 2018-06-10 stsp add_node(struct got_commit_graph_node **new_node,
192 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
193 372ccdbb 2018-06-10 stsp struct got_commit_object *commit, struct got_object_id *child_commit_id)
194 372ccdbb 2018-06-10 stsp {
195 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
196 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node, *existing_node;
197 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
198 372ccdbb 2018-06-10 stsp
199 372ccdbb 2018-06-10 stsp *new_node = NULL;
200 372ccdbb 2018-06-10 stsp
201 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
202 372ccdbb 2018-06-10 stsp if (node == NULL)
203 372ccdbb 2018-06-10 stsp return got_error_from_errno();
204 372ccdbb 2018-06-10 stsp
205 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
206 b43fbaa0 2018-06-11 stsp SIMPLEQ_INIT(&node->parent_ids);
207 372ccdbb 2018-06-10 stsp SIMPLEQ_INIT(&node->child_ids);
208 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
209 79f35eb3 2018-06-11 stsp err = add_vertex(&node->parent_ids, qid->id);
210 b43fbaa0 2018-06-11 stsp if (err)
211 b43fbaa0 2018-06-11 stsp return err;
212 b43fbaa0 2018-06-11 stsp node->nparents++;
213 b43fbaa0 2018-06-11 stsp }
214 b43fbaa0 2018-06-11 stsp node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
215 372ccdbb 2018-06-10 stsp
216 372ccdbb 2018-06-10 stsp err = got_object_idset_add((void **)(&existing_node),
217 372ccdbb 2018-06-10 stsp graph->node_ids, &node->id, node);
218 372ccdbb 2018-06-10 stsp if (err == NULL) {
219 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
220 372ccdbb 2018-06-10 stsp
221 4626e416 2018-06-10 stsp add_iteration_candidate(graph, node);
222 372ccdbb 2018-06-10 stsp err = got_object_idset_remove(graph->open_branches, commit_id);
223 372ccdbb 2018-06-10 stsp if (err && err->code != GOT_ERR_NO_OBJ)
224 372ccdbb 2018-06-10 stsp return err;
225 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
226 79f35eb3 2018-06-11 stsp if (got_object_idset_get(graph->node_ids, qid->id))
227 372ccdbb 2018-06-10 stsp continue; /* parent already traversed */
228 372ccdbb 2018-06-10 stsp err = got_object_idset_add(NULL, graph->open_branches,
229 79f35eb3 2018-06-11 stsp qid->id, node);
230 372ccdbb 2018-06-10 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
231 372ccdbb 2018-06-10 stsp return err;
232 372ccdbb 2018-06-10 stsp }
233 372ccdbb 2018-06-10 stsp *new_node = node;
234 372ccdbb 2018-06-10 stsp } else if (err->code == GOT_ERR_OBJ_EXISTS) {
235 372ccdbb 2018-06-10 stsp err = NULL;
236 372ccdbb 2018-06-10 stsp free(node);
237 372ccdbb 2018-06-10 stsp node = existing_node;
238 372ccdbb 2018-06-10 stsp } else {
239 372ccdbb 2018-06-10 stsp free(node);
240 372ccdbb 2018-06-10 stsp return err;
241 372ccdbb 2018-06-10 stsp }
242 372ccdbb 2018-06-10 stsp
243 372ccdbb 2018-06-10 stsp if (child_commit_id) {
244 79f35eb3 2018-06-11 stsp struct got_object_qid *cid;
245 372ccdbb 2018-06-10 stsp
246 372ccdbb 2018-06-10 stsp /* Prevent linking to self. */
247 372ccdbb 2018-06-10 stsp if (got_object_id_cmp(commit_id, child_commit_id) == 0)
248 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
249 372ccdbb 2018-06-10 stsp
250 372ccdbb 2018-06-10 stsp /* Prevent double-linking to the same child. */
251 372ccdbb 2018-06-10 stsp SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
252 372ccdbb 2018-06-10 stsp if (got_object_id_cmp(cid->id, child_commit_id) == 0)
253 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
254 372ccdbb 2018-06-10 stsp }
255 372ccdbb 2018-06-10 stsp
256 b43fbaa0 2018-06-11 stsp err = add_vertex(&node->child_ids, child_commit_id);
257 b43fbaa0 2018-06-11 stsp if (err)
258 372ccdbb 2018-06-10 stsp return err;
259 372ccdbb 2018-06-10 stsp node->nchildren++;
260 372ccdbb 2018-06-10 stsp
261 372ccdbb 2018-06-10 stsp }
262 372ccdbb 2018-06-10 stsp
263 c4d7a9c4 2018-06-11 stsp return err;
264 c4d7a9c4 2018-06-11 stsp }
265 c4d7a9c4 2018-06-11 stsp
266 c4d7a9c4 2018-06-11 stsp const struct got_error *
267 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
268 c4d7a9c4 2018-06-11 stsp struct got_object_id *commit_id, struct got_repository *repo)
269 c4d7a9c4 2018-06-11 stsp {
270 c4d7a9c4 2018-06-11 stsp const struct got_error *err = NULL;
271 c4d7a9c4 2018-06-11 stsp struct got_commit_object *commit;
272 c4d7a9c4 2018-06-11 stsp
273 c4d7a9c4 2018-06-11 stsp *graph = NULL;
274 c4d7a9c4 2018-06-11 stsp
275 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
276 372ccdbb 2018-06-10 stsp if (err)
277 372ccdbb 2018-06-10 stsp return err;
278 372ccdbb 2018-06-10 stsp
279 372ccdbb 2018-06-10 stsp *graph = alloc_graph();
280 372ccdbb 2018-06-10 stsp if (*graph == NULL) {
281 372ccdbb 2018-06-10 stsp got_object_commit_close(commit);
282 372ccdbb 2018-06-10 stsp return got_error_from_errno();
283 372ccdbb 2018-06-10 stsp }
284 372ccdbb 2018-06-10 stsp
285 372ccdbb 2018-06-10 stsp err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
286 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
287 372ccdbb 2018-06-10 stsp if (err) {
288 372ccdbb 2018-06-10 stsp got_commit_graph_close(*graph);
289 372ccdbb 2018-06-10 stsp *graph = NULL;
290 372ccdbb 2018-06-10 stsp return err;
291 372ccdbb 2018-06-10 stsp }
292 372ccdbb 2018-06-10 stsp
293 372ccdbb 2018-06-10 stsp return NULL;
294 372ccdbb 2018-06-10 stsp }
295 372ccdbb 2018-06-10 stsp
296 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch {
297 372ccdbb 2018-06-10 stsp struct got_object_id parent_id;
298 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node;
299 372ccdbb 2018-06-10 stsp };
300 372ccdbb 2018-06-10 stsp
301 372ccdbb 2018-06-10 stsp struct gather_branches_arg {
302 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch *branches;
303 372ccdbb 2018-06-10 stsp int nbranches;
304 372ccdbb 2018-06-10 stsp };
305 372ccdbb 2018-06-10 stsp
306 372ccdbb 2018-06-10 stsp static void
307 372ccdbb 2018-06-10 stsp gather_branches(struct got_object_id *id, void *data, void *arg)
308 372ccdbb 2018-06-10 stsp {
309 372ccdbb 2018-06-10 stsp struct gather_branches_arg *a = arg;
310 372ccdbb 2018-06-10 stsp memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
311 372ccdbb 2018-06-10 stsp a->branches[a->nbranches].node = data;
312 372ccdbb 2018-06-10 stsp a->nbranches++;
313 372ccdbb 2018-06-10 stsp }
314 372ccdbb 2018-06-10 stsp
315 1142eae9 2018-06-11 stsp static const struct got_error *
316 1142eae9 2018-06-11 stsp fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
317 1142eae9 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo,
318 1142eae9 2018-06-11 stsp struct got_object_id *wanted_id)
319 372ccdbb 2018-06-10 stsp {
320 372ccdbb 2018-06-10 stsp const struct got_error *err;
321 372ccdbb 2018-06-10 stsp struct got_commit_graph_branch *branches;
322 372ccdbb 2018-06-10 stsp struct gather_branches_arg arg;
323 372ccdbb 2018-06-10 stsp int i;
324 372ccdbb 2018-06-10 stsp
325 372ccdbb 2018-06-10 stsp *ncommits = 0;
326 1142eae9 2018-06-11 stsp if (wanted_id_added)
327 1142eae9 2018-06-11 stsp *wanted_id_added = 0;
328 372ccdbb 2018-06-10 stsp
329 372ccdbb 2018-06-10 stsp arg.nbranches = got_object_idset_num_elements(graph->open_branches);
330 372ccdbb 2018-06-10 stsp if (arg.nbranches == 0)
331 372ccdbb 2018-06-10 stsp return NULL;
332 372ccdbb 2018-06-10 stsp
333 372ccdbb 2018-06-10 stsp /*
334 372ccdbb 2018-06-10 stsp * Adding nodes to the graph might change the graph's open
335 372ccdbb 2018-06-10 stsp * branches state. Create a local copy of the current state.
336 372ccdbb 2018-06-10 stsp */
337 372ccdbb 2018-06-10 stsp branches = calloc(arg.nbranches, sizeof(*branches));
338 372ccdbb 2018-06-10 stsp if (branches == NULL)
339 372ccdbb 2018-06-10 stsp return got_error_from_errno();
340 372ccdbb 2018-06-10 stsp arg.nbranches = 0; /* reset; gather_branches() will increment */
341 372ccdbb 2018-06-10 stsp arg.branches = branches;
342 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
343 372ccdbb 2018-06-10 stsp
344 372ccdbb 2018-06-10 stsp for (i = 0; i < arg.nbranches; i++) {
345 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
346 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *child_node, *new_node;
347 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
348 372ccdbb 2018-06-10 stsp
349 372ccdbb 2018-06-10 stsp commit_id = &branches[i].parent_id;
350 372ccdbb 2018-06-10 stsp child_node = branches[i].node;
351 372ccdbb 2018-06-10 stsp
352 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
353 372ccdbb 2018-06-10 stsp if (err)
354 372ccdbb 2018-06-10 stsp break;
355 372ccdbb 2018-06-10 stsp
356 372ccdbb 2018-06-10 stsp err = add_node(&new_node, graph, commit_id, commit,
357 372ccdbb 2018-06-10 stsp &child_node->id);
358 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
359 372ccdbb 2018-06-10 stsp if (err) {
360 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_OBJ_EXISTS)
361 372ccdbb 2018-06-10 stsp break;
362 372ccdbb 2018-06-10 stsp err = NULL;
363 372ccdbb 2018-06-10 stsp }
364 372ccdbb 2018-06-10 stsp if (new_node)
365 372ccdbb 2018-06-10 stsp (*ncommits)++;
366 1142eae9 2018-06-11 stsp if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
367 1142eae9 2018-06-11 stsp *wanted_id_added = 1;
368 372ccdbb 2018-06-10 stsp }
369 372ccdbb 2018-06-10 stsp
370 372ccdbb 2018-06-10 stsp free(branches);
371 372ccdbb 2018-06-10 stsp return err;
372 372ccdbb 2018-06-10 stsp }
373 372ccdbb 2018-06-10 stsp
374 372ccdbb 2018-06-10 stsp const struct got_error *
375 372ccdbb 2018-06-10 stsp got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
376 372ccdbb 2018-06-10 stsp int limit, struct got_repository *repo)
377 372ccdbb 2018-06-10 stsp {
378 372ccdbb 2018-06-10 stsp const struct got_error *err;
379 1142eae9 2018-06-11 stsp int ncommits;
380 372ccdbb 2018-06-10 stsp
381 372ccdbb 2018-06-10 stsp *nfetched = 0;
382 372ccdbb 2018-06-10 stsp
383 1142eae9 2018-06-11 stsp while (*nfetched < limit) {
384 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits, NULL,
385 1142eae9 2018-06-11 stsp graph, repo, NULL);
386 372ccdbb 2018-06-10 stsp if (err)
387 372ccdbb 2018-06-10 stsp return err;
388 372ccdbb 2018-06-10 stsp if (ncommits == 0)
389 372ccdbb 2018-06-10 stsp break;
390 1142eae9 2018-06-11 stsp *nfetched += ncommits;
391 372ccdbb 2018-06-10 stsp }
392 372ccdbb 2018-06-10 stsp
393 372ccdbb 2018-06-10 stsp return NULL;
394 372ccdbb 2018-06-10 stsp }
395 372ccdbb 2018-06-10 stsp
396 1142eae9 2018-06-11 stsp const struct got_error *
397 1142eae9 2018-06-11 stsp got_commit_graph_fetch_commits_up_to(int *nfetched,
398 1142eae9 2018-06-11 stsp struct got_commit_graph *graph, struct got_object_id *wanted_id,
399 1142eae9 2018-06-11 stsp struct got_repository *repo)
400 1142eae9 2018-06-11 stsp {
401 1142eae9 2018-06-11 stsp const struct got_error *err;
402 1142eae9 2018-06-11 stsp int ncommits, wanted_id_added = 0;
403 1142eae9 2018-06-11 stsp
404 1142eae9 2018-06-11 stsp *nfetched = 0;
405 6e0c0f9a 2018-06-11 stsp
406 6e0c0f9a 2018-06-11 stsp if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
407 6e0c0f9a 2018-06-11 stsp return NULL;
408 6e0c0f9a 2018-06-11 stsp
409 1142eae9 2018-06-11 stsp while (!wanted_id_added) {
410 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits,
411 1142eae9 2018-06-11 stsp &wanted_id_added, graph, repo, wanted_id);
412 1142eae9 2018-06-11 stsp if (err)
413 1142eae9 2018-06-11 stsp return err;
414 1142eae9 2018-06-11 stsp if (ncommits == 0)
415 1142eae9 2018-06-11 stsp return NULL;
416 1142eae9 2018-06-11 stsp *nfetched += ncommits;
417 1142eae9 2018-06-11 stsp }
418 1142eae9 2018-06-11 stsp
419 1142eae9 2018-06-11 stsp return NULL;
420 1142eae9 2018-06-11 stsp }
421 1142eae9 2018-06-11 stsp
422 372ccdbb 2018-06-10 stsp static void
423 372ccdbb 2018-06-10 stsp free_graph_node(struct got_object_id *id, void *data, void *arg)
424 372ccdbb 2018-06-10 stsp {
425 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node = data;
426 372ccdbb 2018-06-10 stsp while (!SIMPLEQ_EMPTY(&node->child_ids)) {
427 79f35eb3 2018-06-11 stsp struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
428 372ccdbb 2018-06-10 stsp SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
429 372ccdbb 2018-06-10 stsp free(child);
430 372ccdbb 2018-06-10 stsp }
431 372ccdbb 2018-06-10 stsp free(node);
432 372ccdbb 2018-06-10 stsp }
433 372ccdbb 2018-06-10 stsp
434 372ccdbb 2018-06-10 stsp void
435 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
436 372ccdbb 2018-06-10 stsp {
437 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->open_branches);
438 372ccdbb 2018-06-10 stsp got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
439 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
440 372ccdbb 2018-06-10 stsp free(graph);
441 372ccdbb 2018-06-10 stsp }
442 372ccdbb 2018-06-10 stsp
443 372ccdbb 2018-06-10 stsp const struct got_error *
444 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
445 372ccdbb 2018-06-10 stsp struct got_object_id *id)
446 372ccdbb 2018-06-10 stsp {
447 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *start_node, *node;
448 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
449 372ccdbb 2018-06-10 stsp
450 372ccdbb 2018-06-10 stsp start_node = got_object_idset_get(graph->node_ids, id);
451 372ccdbb 2018-06-10 stsp if (start_node == NULL)
452 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_NO_OBJ);
453 372ccdbb 2018-06-10 stsp
454 372ccdbb 2018-06-10 stsp graph->iter_node = start_node;
455 372ccdbb 2018-06-10 stsp
456 372ccdbb 2018-06-10 stsp while (!TAILQ_EMPTY(&graph->iter_candidates)) {
457 372ccdbb 2018-06-10 stsp node = TAILQ_FIRST(&graph->iter_candidates);
458 372ccdbb 2018-06-10 stsp TAILQ_REMOVE(&graph->iter_candidates, node, entry);
459 372ccdbb 2018-06-10 stsp }
460 372ccdbb 2018-06-10 stsp
461 372ccdbb 2018-06-10 stsp /* Put all known parents of this commit on the candidate list. */
462 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &start_node->parent_ids, entry) {
463 79f35eb3 2018-06-11 stsp node = got_object_idset_get(graph->node_ids, qid->id);
464 4626e416 2018-06-10 stsp if (node)
465 4626e416 2018-06-10 stsp add_iteration_candidate(graph, node);
466 372ccdbb 2018-06-10 stsp }
467 372ccdbb 2018-06-10 stsp
468 372ccdbb 2018-06-10 stsp return NULL;
469 372ccdbb 2018-06-10 stsp }
470 372ccdbb 2018-06-10 stsp
471 372ccdbb 2018-06-10 stsp const struct got_error *
472 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
473 b43fbaa0 2018-06-11 stsp struct got_commit_graph *graph)
474 372ccdbb 2018-06-10 stsp {
475 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node;
476 372ccdbb 2018-06-10 stsp
477 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
478 372ccdbb 2018-06-10 stsp /* We are done interating, or iteration was not started. */
479 372ccdbb 2018-06-10 stsp *id = NULL;
480 372ccdbb 2018-06-10 stsp return NULL;
481 372ccdbb 2018-06-10 stsp }
482 372ccdbb 2018-06-10 stsp
483 372ccdbb 2018-06-10 stsp if (TAILQ_EMPTY(&graph->iter_candidates)) {
484 372ccdbb 2018-06-10 stsp if (is_root_node(graph->iter_node) &&
485 372ccdbb 2018-06-10 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
486 372ccdbb 2018-06-10 stsp *id = &graph->iter_node->id;
487 372ccdbb 2018-06-10 stsp /* We are done interating. */
488 372ccdbb 2018-06-10 stsp graph->iter_node = NULL;
489 372ccdbb 2018-06-10 stsp return NULL;
490 372ccdbb 2018-06-10 stsp }
491 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_ITER_NEED_MORE);
492 372ccdbb 2018-06-10 stsp }
493 372ccdbb 2018-06-10 stsp
494 372ccdbb 2018-06-10 stsp *id = &graph->iter_node->id;
495 372ccdbb 2018-06-10 stsp node = TAILQ_FIRST(&graph->iter_candidates);
496 372ccdbb 2018-06-10 stsp TAILQ_REMOVE(&graph->iter_candidates, node, entry);
497 372ccdbb 2018-06-10 stsp graph->iter_node = node;
498 372ccdbb 2018-06-10 stsp return NULL;
499 372ccdbb 2018-06-10 stsp }