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