2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/stdint.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;
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.
48 struct got_object_id_queue parent_ids;
50 struct got_object_id_queue child_ids;
52 time_t commit_timestamp;
54 /* Used during graph iteration. */
55 TAILQ_ENTRY(got_commit_graph_node) entry;
58 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
60 struct got_commit_graph {
61 /* The set of all commits we have traversed. */
62 struct got_object_idset *node_ids;
64 /* The commit at which traversal began (youngest commit in node_ids). */
65 struct got_commit_graph_node *head_node;
68 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
71 * A set of object IDs of known parent commits which we have not yet
72 * traversed. Each commit ID in this set represents a branch in commit
73 * history: Either the first-parent branch of the head node, or another
74 * branch corresponding to a traversed merge commit for which we have
75 * not traversed a branch point commit yet.
77 * Whenever we add a commit with a matching ID to the graph, we remove
78 * its corresponding element from this set, and add new elements for
79 * each of that commit's parent commits which were not traversed yet.
81 * When API users ask us to fetch more commits, we fetch commits from
82 * all currently open branches. This allows API users to process
83 * commits in linear order even though the history contains branches.
85 struct got_object_idset *open_branches;
87 /* The next commit to return when the API user asks for one. */
88 struct got_commit_graph_node *iter_node;
90 /* The graph iteration list contains all nodes in sorted order. */
91 struct got_commit_graph_iter_list iter_list;
94 static struct got_commit_graph *
97 struct got_commit_graph *graph;
99 graph = calloc(1, sizeof(*graph));
103 graph->node_ids = got_object_idset_alloc(
104 GOT_OBJECT_IDSET_ITERATE_BY_OBJECT_ID);
105 if (graph->node_ids == NULL) {
110 graph->open_branches = got_object_idset_alloc(
111 GOT_OBJECT_IDSET_ITERATE_BY_OBJECT_ID);
112 if (graph->open_branches == NULL) {
113 got_object_idset_free(graph->node_ids);
118 TAILQ_INIT(&graph->iter_list);
124 is_head_node(struct got_commit_graph_node *node)
126 return node->nchildren == 0;
130 is_merge_point(struct got_commit_graph_node *node)
132 return node->nparents > 1;
136 is_branch_point(struct got_commit_graph_node *node)
138 return node->nchildren > 1;
142 is_root_node(struct got_commit_graph_node *node)
144 return node->nparents == 0;
149 add_node_to_iter_list(struct got_commit_graph *graph,
150 struct got_commit_graph_node *node,
151 struct got_commit_graph_node *child_node)
153 struct got_commit_graph_node *n, *next;
155 if (TAILQ_EMPTY(&graph->iter_list)) {
156 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
160 /* Ensure that an iteration in progress will see this new commit. */
161 if (graph->iter_node) {
162 n = graph->iter_node;
164 next = TAILQ_NEXT(n, entry);
166 node->commit_timestamp >= next->commit_timestamp) {
167 TAILQ_INSERT_BEFORE(next, node, entry);
172 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
178 * If a child node is known, begin looping over the list there
179 * instead of beginning from the list head.
181 n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
183 /* Insert into list based on committer timestamp. */
185 if (node->commit_timestamp == n->commit_timestamp) {
186 TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
188 } else if (node->commit_timestamp < n->commit_timestamp) {
189 next = TAILQ_NEXT(n, entry);
191 TAILQ_INSERT_AFTER(&graph->iter_list, n,
195 if (node->commit_timestamp >= next->commit_timestamp) {
196 TAILQ_INSERT_BEFORE(next, node, entry);
200 TAILQ_INSERT_BEFORE(n, node, entry);
203 n = TAILQ_NEXT(n, entry);
207 static const struct got_error *
208 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
210 struct got_object_qid *qid;
212 qid = calloc(1, sizeof(*qid));
214 return got_error_from_errno();
216 qid->id = got_object_id_dup(id);
217 if (qid->id == NULL) {
218 const struct got_error *err = got_error_from_errno();
223 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
227 static const struct got_error *
228 advance_open_branches(struct got_commit_graph *graph,
229 struct got_commit_graph_node *node,
230 struct got_object_id *commit_id, struct got_commit_object *commit)
232 const struct got_error *err;
233 struct got_object_qid *qid;
235 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
236 if (err && err->code != GOT_ERR_NO_OBJ)
239 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
240 qid = SIMPLEQ_FIRST(&commit->parent_ids);
243 err = got_object_idset_add(NULL, graph->open_branches, qid->id,
245 if (err && err->code != GOT_ERR_OBJ_EXISTS)
250 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
251 if (got_object_idset_get(graph->node_ids, qid->id))
252 continue; /* parent already traversed */
253 err = got_object_idset_add(NULL, graph->open_branches,
255 if (err && err->code != GOT_ERR_OBJ_EXISTS)
262 static const struct got_error *
263 add_node(struct got_commit_graph_node **new_node,
264 struct got_commit_graph *graph, struct got_object_id *commit_id,
265 struct got_commit_object *commit, struct got_commit_graph_node *child_node)
267 const struct got_error *err = NULL;
268 struct got_commit_graph_node *node, *existing_node;
269 struct got_object_qid *qid;
273 node = calloc(1, sizeof(*node));
275 return got_error_from_errno();
277 memcpy(&node->id, commit_id, sizeof(node->id));
278 SIMPLEQ_INIT(&node->parent_ids);
279 SIMPLEQ_INIT(&node->child_ids);
280 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
281 err = add_vertex(&node->parent_ids, qid->id);
286 node->commit_timestamp = mktime(&commit->tm_committer);
287 if (node->commit_timestamp == -1)
288 return got_error_from_errno();
290 err = got_object_idset_add((void **)(&existing_node),
291 graph->node_ids, &node->id, node);
293 add_node_to_iter_list(graph, node, child_node);
294 err = advance_open_branches(graph, node, commit_id, commit);
296 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
299 node = existing_node;
306 struct got_object_qid *cid;
308 /* Prevent linking to self. */
309 if (got_object_id_cmp(commit_id, &child_node->id) == 0)
310 return got_error(GOT_ERR_BAD_OBJ_ID);
312 /* Prevent double-linking to the same child. */
313 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
314 if (got_object_id_cmp(cid->id, &child_node->id) == 0)
315 return got_error(GOT_ERR_BAD_OBJ_ID);
318 err = add_vertex(&node->child_ids, &child_node->id);
327 const struct got_error *
328 got_commit_graph_open(struct got_commit_graph **graph,
329 struct got_object_id *commit_id, int first_parent_traversal,
330 struct got_repository *repo)
332 const struct got_error *err = NULL;
333 struct got_commit_object *commit;
337 err = got_object_open_as_commit(&commit, repo, commit_id);
341 *graph = alloc_graph();
342 if (*graph == NULL) {
343 got_object_commit_close(commit);
344 return got_error_from_errno();
347 if (first_parent_traversal)
348 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
350 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
351 got_object_commit_close(commit);
353 got_commit_graph_close(*graph);
361 struct got_commit_graph_branch {
362 struct got_object_id parent_id;
363 struct got_commit_graph_node *node;
366 struct gather_branches_arg {
367 struct got_commit_graph_branch *branches;
372 gather_branches(struct got_object_id *id, void *data, void *arg)
374 struct gather_branches_arg *a = arg;
375 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
376 a->branches[a->nbranches].node = data;
380 static const struct got_error *
381 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
382 struct got_commit_graph *graph, struct got_repository *repo,
383 struct got_object_id *wanted_id)
385 const struct got_error *err;
386 struct got_commit_graph_branch *branches;
387 struct gather_branches_arg arg;
392 *wanted_id_added = 0;
394 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
395 if (arg.nbranches == 0)
399 * Adding nodes to the graph might change the graph's open
400 * branches state. Create a local copy of the current state.
402 branches = calloc(arg.nbranches, sizeof(*branches));
403 if (branches == NULL)
404 return got_error_from_errno();
405 arg.nbranches = 0; /* reset; gather_branches() will increment */
406 arg.branches = branches;
407 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
409 for (i = 0; i < arg.nbranches; i++) {
410 struct got_object_id *commit_id;
411 struct got_commit_graph_node *child_node, *new_node;
412 struct got_commit_object *commit;
414 commit_id = &branches[i].parent_id;
415 child_node = branches[i].node;
417 err = got_object_open_as_commit(&commit, repo, commit_id);
421 err = add_node(&new_node, graph, commit_id, commit, child_node);
422 got_object_commit_close(commit);
424 if (err->code != GOT_ERR_OBJ_EXISTS)
430 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
431 *wanted_id_added = 1;
438 const struct got_error *
439 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
440 int limit, struct got_repository *repo)
442 const struct got_error *err;
447 while (*nfetched < limit) {
448 err = fetch_commits_from_open_branches(&ncommits, NULL,
454 *nfetched += ncommits;
460 const struct got_error *
461 got_commit_graph_fetch_commits_up_to(int *nfetched,
462 struct got_commit_graph *graph, struct got_object_id *wanted_id,
463 struct got_repository *repo)
465 const struct got_error *err;
466 int ncommits, wanted_id_added = 0;
470 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
473 while (!wanted_id_added) {
474 err = fetch_commits_from_open_branches(&ncommits,
475 &wanted_id_added, graph, repo, wanted_id);
480 *nfetched += ncommits;
487 free_graph_node(struct got_object_id *id, void *data, void *arg)
489 struct got_commit_graph_node *node = data;
490 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
491 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
492 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
499 got_commit_graph_close(struct got_commit_graph *graph)
501 got_object_idset_free(graph->open_branches);
502 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
503 got_object_idset_free(graph->node_ids);
507 const struct got_error *
508 got_commit_graph_iter_start(struct got_commit_graph *graph,
509 struct got_object_id *id)
511 struct got_commit_graph_node *start_node;
513 start_node = got_object_idset_get(graph->node_ids, id);
514 if (start_node == NULL)
515 return got_error(GOT_ERR_NO_OBJ);
517 graph->iter_node = start_node;
521 const struct got_error *
522 got_commit_graph_iter_next(struct got_object_id **id,
523 struct got_commit_graph *graph)
527 if (graph->iter_node == NULL) {
528 /* We are done interating, or iteration was not started. */
529 return got_error(GOT_ERR_ITER_COMPLETED);
532 if (graph->iter_node ==
533 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
534 got_object_idset_num_elements(graph->open_branches) == 0) {
535 *id = &graph->iter_node->id;
536 /* We are done interating. */
537 graph->iter_node = NULL;
541 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
542 return got_error(GOT_ERR_ITER_NEED_MORE);
544 *id = &graph->iter_node->id;
545 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);