Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sha1.h>
26 #include <zlib.h>
27 #include <ctype.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_commit_graph.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_zbuf.h"
35 #include "got_lib_object.h"
36 #include "got_lib_object_idset.h"
38 struct got_commit_graph_node {
39 struct got_object_id id;
41 /*
42 * Each graph node corresponds to a commit object.
43 * Graph vertices are modelled with two separate adjacency lists:
44 * Adjacencies of a graph node are either parent (older) commits,
45 * and child (younger) commits.
46 */
47 struct got_commit_object *commit; /* contains list of parents */
48 int nchildren;
49 SIMPLEQ_HEAD(, got_parent_id) child_ids;
51 /* Used during graph iteration. */
52 TAILQ_ENTRY(got_commit_graph_node) entry;
53 };
55 struct got_commit_graph {
56 /* The set of all commits we have traversed. */
57 struct got_object_idset *node_ids;
59 /* The commit at which traversal began (youngest commit in node_ids). */
60 struct got_commit_graph_node *head_node;
62 /*
63 * A set of object IDs of known parent commits which we have not yet
64 * traversed. Each commit ID in this set represents a branch in commit
65 * history: Either the first-parent branch of the head node, or another
66 * branch corresponding to a traversed merge commit for which we have
67 * not traversed a branch point commit yet.
68 *
69 * Whenever we add a commit with a matching ID to the graph, we remove
70 * its corresponding element from this set, and add new elements for
71 * each of that commit's parent commits which were not traversed yet.
72 *
73 * When API users ask us to fetch more commits, we fetch commits from
74 * all currently open branches. This allows API users to process
75 * commits in linear order even though the history contains branches.
76 */
77 struct got_object_idset *open_branches;
79 /* The next commit to return when the API user asks for one. */
80 struct got_commit_graph_node *iter_node;
82 TAILQ_HEAD(, got_commit_graph_node) iter_candidates;
83 };
85 static struct got_commit_graph *
86 alloc_graph(void)
87 {
88 struct got_commit_graph *graph;
90 graph = calloc(1, sizeof(*graph));
91 if (graph == NULL)
92 return NULL;
94 graph->node_ids = got_object_idset_alloc();
95 if (graph->node_ids == NULL) {
96 free(graph);
97 return NULL;
98 }
100 graph->open_branches = got_object_idset_alloc();
101 if (graph->open_branches == NULL) {
102 got_object_idset_free(graph->node_ids);
103 free(graph);
104 return NULL;
107 TAILQ_INIT(&graph->iter_candidates);
108 return graph;
111 #if 0
112 static int
113 is_head_node(struct got_commit_graph_node *node)
115 return node->nchildren == 0;
118 static int
119 is_merge_point(struct got_commit_graph_node *node)
121 return node->commit->nparents > 1;
124 int
125 is_branch_point(struct got_commit_graph_node *node)
127 return node->nchildren > 1;
129 #endif
131 static int
132 is_root_node(struct got_commit_graph_node *node)
134 return node->commit->nparents == 0;
137 static int
138 compare_commits(struct got_commit_object *c1, struct got_commit_object *c2)
140 time_t t1, t2;
142 t1 = c1->committer_time;
143 t2 = c2->committer_time;
144 if (t1 == t2)
145 return 0;
146 else if (t1 < t2)
147 return -1;
148 return 1;
151 static void
152 add_iteration_candidate(struct got_commit_graph *graph,
153 struct got_commit_graph_node *node)
155 struct got_commit_graph_node *n, *next;
157 if (TAILQ_EMPTY(&graph->iter_candidates)) {
158 TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
159 return;
162 TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
163 int cmp = compare_commits(node->commit, n->commit);
164 if (cmp < 0) {
165 next = TAILQ_NEXT(n, entry);
166 if (next == NULL) {
167 TAILQ_INSERT_AFTER(&graph->iter_candidates, n,
168 node, entry);
169 break;
171 cmp = compare_commits(node->commit, next->commit);
172 if (cmp >= 0) {
173 TAILQ_INSERT_BEFORE(next, node, entry);
174 break;
176 } else {
177 TAILQ_INSERT_BEFORE(n, node, entry);
178 break;
183 static const struct got_error *
184 add_node(struct got_commit_graph_node **new_node,
185 struct got_commit_graph *graph, struct got_object_id *commit_id,
186 struct got_commit_object *commit, struct got_object_id *child_commit_id)
188 const struct got_error *err = NULL;
189 struct got_commit_graph_node *node, *existing_node;
191 *new_node = NULL;
193 node = calloc(1, sizeof(*node));
194 if (node == NULL)
195 return got_error_from_errno();
197 memcpy(&node->id, commit_id, sizeof(node->id));
198 node->commit = commit;
199 SIMPLEQ_INIT(&node->child_ids);
201 err = got_object_idset_add((void **)(&existing_node),
202 graph->node_ids, &node->id, node);
203 if (err == NULL) {
204 struct got_parent_id *pid;
206 add_iteration_candidate(graph, node);
207 err = got_object_idset_remove(graph->open_branches, commit_id);
208 if (err && err->code != GOT_ERR_NO_OBJ)
209 return err;
210 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
211 if (got_object_idset_get(graph->node_ids, pid->id))
212 continue; /* parent already traversed */
213 err = got_object_idset_add(NULL, graph->open_branches,
214 pid->id, node);
215 if (err && err->code != GOT_ERR_OBJ_EXISTS)
216 return err;
218 *new_node = node;
219 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
220 err = NULL;
221 free(node);
222 node = existing_node;
223 } else {
224 free(node);
225 return err;
228 if (child_commit_id) {
229 struct got_parent_id *child, *cid;
231 /* Prevent linking to self. */
232 if (got_object_id_cmp(commit_id, child_commit_id) == 0)
233 return got_error(GOT_ERR_BAD_OBJ_ID);
235 /* Prevent double-linking to the same child. */
236 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
237 if (got_object_id_cmp(cid->id, child_commit_id) == 0)
238 return got_error(GOT_ERR_BAD_OBJ_ID);
241 child = calloc(1, sizeof(*child));
242 if (child == NULL)
243 return got_error_from_errno();
244 child->id = got_object_id_dup(child_commit_id);
245 if (child->id == NULL) {
246 err = got_error_from_errno();
247 free(child);
248 return err;
250 SIMPLEQ_INSERT_TAIL(&node->child_ids, child, entry);
251 node->nchildren++;
254 return err;
257 const struct got_error *
258 got_commit_graph_open(struct got_commit_graph **graph,
259 struct got_object_id *commit_id, struct got_repository *repo)
261 const struct got_error *err = NULL;
262 struct got_object *obj;
263 struct got_commit_object *commit;
265 *graph = NULL;
267 err = got_object_open(&obj, repo, commit_id);
268 if (err)
269 return err;
270 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
271 err = got_error(GOT_ERR_OBJ_TYPE);
272 got_object_close(obj);
273 return err;
276 err = got_object_commit_open(&commit, repo, obj);
277 got_object_close(obj);
278 if (err)
279 return err;
281 *graph = alloc_graph();
282 if (*graph == NULL) {
283 got_object_commit_close(commit);
284 return got_error_from_errno();
287 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
288 if (err) {
289 got_commit_graph_close(*graph);
290 *graph = NULL;
291 return err;
294 return NULL;
297 static const struct got_error *
298 open_commit(struct got_commit_object **commit, struct got_object_id *id,
299 struct got_repository *repo)
301 const struct got_error *err;
302 struct got_object *obj;
304 err = got_object_open(&obj, repo, id);
305 if (err)
306 return err;
307 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
308 err = got_error(GOT_ERR_OBJ_TYPE);
309 goto done;
312 err = got_object_commit_open(commit, repo, obj);
313 done:
314 got_object_close(obj);
315 return err;
318 struct got_commit_graph_branch {
319 struct got_object_id parent_id;
320 struct got_commit_graph_node *node;
321 };
323 struct gather_branches_arg {
324 struct got_commit_graph_branch *branches;
325 int nbranches;
326 };
328 static void
329 gather_branches(struct got_object_id *id, void *data, void *arg)
331 struct gather_branches_arg *a = arg;
332 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
333 a->branches[a->nbranches].node = data;
334 a->nbranches++;
337 const struct got_error *
338 fetch_commits_from_open_branches(int *ncommits,
339 struct got_commit_graph *graph, struct got_repository *repo)
341 const struct got_error *err;
342 struct got_commit_graph_branch *branches;
343 struct gather_branches_arg arg;
344 int i;
346 *ncommits = 0;
348 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
349 if (arg.nbranches == 0)
350 return NULL;
352 /*
353 * Adding nodes to the graph might change the graph's open
354 * branches state. Create a local copy of the current state.
355 */
356 branches = calloc(arg.nbranches, sizeof(*branches));
357 if (branches == NULL)
358 return got_error_from_errno();
359 arg.nbranches = 0; /* reset; gather_branches() will increment */
360 arg.branches = branches;
361 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
363 for (i = 0; i < arg.nbranches; i++) {
364 struct got_object_id *commit_id;
365 struct got_commit_graph_node *child_node, *new_node;
366 struct got_commit_object *commit;
368 commit_id = &branches[i].parent_id;
369 child_node = branches[i].node;
371 err = open_commit(&commit, commit_id, repo);
372 if (err)
373 break;
375 err = add_node(&new_node, graph, commit_id, commit,
376 &child_node->id);
377 if (err) {
378 if (err->code != GOT_ERR_OBJ_EXISTS)
379 break;
380 err = NULL;
382 if (new_node)
383 (*ncommits)++;
384 else
385 got_object_commit_close(commit);
388 free(branches);
389 return err;
392 const struct got_error *
393 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
394 int limit, struct got_repository *repo)
396 const struct got_error *err;
397 int total = 0, ncommits;
399 *nfetched = 0;
401 while (total < limit) {
402 err = fetch_commits_from_open_branches(&ncommits, graph, repo);
403 if (err)
404 return err;
405 if (ncommits == 0)
406 break;
407 total += ncommits;
410 *nfetched = total;
411 return NULL;
414 static void
415 free_graph_node(struct got_object_id *id, void *data, void *arg)
417 struct got_commit_graph_node *node = data;
418 got_object_commit_close(node->commit);
419 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
420 struct got_parent_id *child = SIMPLEQ_FIRST(&node->child_ids);
421 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
422 free(child);
424 free(node);
427 void
428 got_commit_graph_close(struct got_commit_graph *graph)
430 got_object_idset_free(graph->open_branches);
431 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
432 got_object_idset_free(graph->node_ids);
433 free(graph);
436 const struct got_error *
437 got_commit_graph_iter_start(struct got_commit_graph *graph,
438 struct got_object_id *id)
440 struct got_commit_graph_node *start_node, *node;
441 struct got_parent_id *pid;
443 start_node = got_object_idset_get(graph->node_ids, id);
444 if (start_node == NULL)
445 return got_error(GOT_ERR_NO_OBJ);
447 graph->iter_node = start_node;
449 while (!TAILQ_EMPTY(&graph->iter_candidates)) {
450 node = TAILQ_FIRST(&graph->iter_candidates);
451 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
454 /* Put all known parents of this commit on the candidate list. */
455 SIMPLEQ_FOREACH(pid, &start_node->commit->parent_ids, entry) {
456 node = got_object_idset_get(graph->node_ids, pid->id);
457 if (node)
458 add_iteration_candidate(graph, node);
461 return NULL;
464 const struct got_error *
465 got_commit_graph_iter_next(struct got_commit_object **commit,
466 struct got_object_id **id, struct got_commit_graph *graph)
468 struct got_commit_graph_node *node;
470 if (graph->iter_node == NULL) {
471 /* We are done interating, or iteration was not started. */
472 *commit = NULL;
473 *id = NULL;
474 return NULL;
477 if (TAILQ_EMPTY(&graph->iter_candidates)) {
478 if (is_root_node(graph->iter_node) &&
479 got_object_idset_num_elements(graph->open_branches) == 0) {
480 *commit = graph->iter_node->commit;
481 *id = &graph->iter_node->id;
482 /* We are done interating. */
483 graph->iter_node = NULL;
484 return NULL;
486 return got_error(GOT_ERR_ITER_NEED_MORE);
489 *commit = graph->iter_node->commit;
490 *id = &graph->iter_node->id;
491 node = TAILQ_FIRST(&graph->iter_candidates);
492 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
493 graph->iter_node = node;
494 return NULL;