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_inflate.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 if (graph->node_ids == NULL) {
109 graph->open_branches = got_object_idset_alloc();
110 if (graph->open_branches == NULL) {
111 got_object_idset_free(graph->node_ids);
116 TAILQ_INIT(&graph->iter_list);
122 is_head_node(struct got_commit_graph_node *node)
124 return node->nchildren == 0;
128 is_merge_point(struct got_commit_graph_node *node)
130 return node->nparents > 1;
134 is_branch_point(struct got_commit_graph_node *node)
136 return node->nchildren > 1;
140 is_root_node(struct got_commit_graph_node *node)
142 return node->nparents == 0;
147 add_node_to_iter_list(struct got_commit_graph *graph,
148 struct got_commit_graph_node *node,
149 struct got_commit_graph_node *child_node)
151 struct got_commit_graph_node *n, *next;
153 if (TAILQ_EMPTY(&graph->iter_list)) {
154 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
158 /* Ensure that an iteration in progress will see this new commit. */
159 if (graph->iter_node) {
160 n = graph->iter_node;
162 next = TAILQ_NEXT(n, entry);
164 node->commit_timestamp >= next->commit_timestamp) {
165 TAILQ_INSERT_BEFORE(next, node, entry);
170 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
176 * If a child node is known, begin looping over the list there
177 * instead of beginning from the list head.
179 n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
181 /* Insert into list based on committer timestamp. */
183 if (node->commit_timestamp == n->commit_timestamp) {
184 TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
186 } else if (node->commit_timestamp < n->commit_timestamp) {
187 next = TAILQ_NEXT(n, entry);
189 TAILQ_INSERT_AFTER(&graph->iter_list, n,
193 if (node->commit_timestamp >= next->commit_timestamp) {
194 TAILQ_INSERT_BEFORE(next, node, entry);
198 TAILQ_INSERT_BEFORE(n, node, entry);
201 n = TAILQ_NEXT(n, entry);
205 static const struct got_error *
206 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
208 struct got_object_qid *qid;
210 qid = calloc(1, sizeof(*qid));
212 return got_error_from_errno();
214 qid->id = got_object_id_dup(id);
215 if (qid->id == NULL) {
216 const struct got_error *err = got_error_from_errno();
221 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
225 static const struct got_error *
226 advance_open_branches(struct got_commit_graph *graph,
227 struct got_commit_graph_node *node,
228 struct got_object_id *commit_id, struct got_commit_object *commit)
230 const struct got_error *err;
231 struct got_object_qid *qid;
233 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
234 if (err && err->code != GOT_ERR_NO_OBJ)
237 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
238 qid = SIMPLEQ_FIRST(&commit->parent_ids);
241 err = got_object_idset_add(NULL, graph->open_branches, qid->id,
243 if (err && err->code != GOT_ERR_OBJ_EXISTS)
248 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
249 if (got_object_idset_get(graph->node_ids, qid->id))
250 continue; /* parent already traversed */
251 err = got_object_idset_add(NULL, graph->open_branches,
253 if (err && err->code != GOT_ERR_OBJ_EXISTS)
261 free_node(struct got_commit_graph_node *node)
263 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
264 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
265 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
268 while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
269 struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
270 SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
276 static const struct got_error *
277 add_node(struct got_commit_graph_node **new_node,
278 struct got_commit_graph *graph, struct got_object_id *commit_id,
279 struct got_commit_object *commit, struct got_commit_graph_node *child_node)
281 const struct got_error *err = NULL;
282 struct got_commit_graph_node *node, *existing_node;
283 struct got_object_qid *qid;
287 node = calloc(1, sizeof(*node));
289 return got_error_from_errno();
291 memcpy(&node->id, commit_id, sizeof(node->id));
292 SIMPLEQ_INIT(&node->parent_ids);
293 SIMPLEQ_INIT(&node->child_ids);
294 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
295 err = add_vertex(&node->parent_ids, qid->id);
302 node->commit_timestamp = mktime(&commit->tm_committer);
303 if (node->commit_timestamp == -1) {
305 return got_error_from_errno();
308 err = got_object_idset_add((void **)(&existing_node),
309 graph->node_ids, &node->id, node);
311 add_node_to_iter_list(graph, node, child_node);
312 err = advance_open_branches(graph, node, commit_id, commit);
314 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
317 node = existing_node;
324 struct got_object_qid *cid;
326 /* Prevent linking to self. */
327 if (got_object_id_cmp(commit_id, &child_node->id) == 0) {
328 err = got_error(GOT_ERR_BAD_OBJ_ID);
332 /* Prevent double-linking to the same child (treat as no-op). */
333 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
334 if (got_object_id_cmp(cid->id, &child_node->id) == 0)
338 err = add_vertex(&node->child_ids, &child_node->id);
346 if (node != existing_node)
353 const struct got_error *
354 got_commit_graph_open(struct got_commit_graph **graph,
355 struct got_object_id *commit_id, int first_parent_traversal,
356 struct got_repository *repo)
358 const struct got_error *err = NULL;
359 struct got_commit_object *commit;
363 err = got_object_open_as_commit(&commit, repo, commit_id);
367 *graph = alloc_graph();
368 if (*graph == NULL) {
369 got_object_commit_close(commit);
370 return got_error_from_errno();
373 if (first_parent_traversal)
374 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
376 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
377 got_object_commit_close(commit);
379 got_commit_graph_close(*graph);
387 struct got_commit_graph_branch {
388 struct got_object_id parent_id;
389 struct got_commit_graph_node *node;
392 struct gather_branches_arg {
393 struct got_commit_graph_branch *branches;
398 gather_branches(struct got_object_id *id, void *data, void *arg)
400 struct gather_branches_arg *a = arg;
401 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
402 a->branches[a->nbranches].node = data;
406 static const struct got_error *
407 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
408 struct got_commit_graph *graph, struct got_repository *repo,
409 struct got_object_id *wanted_id)
411 const struct got_error *err;
412 struct got_commit_graph_branch *branches;
413 struct gather_branches_arg arg;
418 *wanted_id_added = 0;
420 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
421 if (arg.nbranches == 0)
425 * Adding nodes to the graph might change the graph's open
426 * branches state. Create a local copy of the current state.
428 branches = calloc(arg.nbranches, sizeof(*branches));
429 if (branches == NULL)
430 return got_error_from_errno();
431 arg.nbranches = 0; /* reset; gather_branches() will increment */
432 arg.branches = branches;
433 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
435 for (i = 0; i < arg.nbranches; i++) {
436 struct got_object_id *commit_id;
437 struct got_commit_graph_node *child_node, *new_node;
438 struct got_commit_object *commit;
440 commit_id = &branches[i].parent_id;
441 child_node = branches[i].node;
443 err = got_object_open_as_commit(&commit, repo, commit_id);
447 err = add_node(&new_node, graph, commit_id, commit, child_node);
448 got_object_commit_close(commit);
453 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
454 *wanted_id_added = 1;
461 const struct got_error *
462 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
463 int limit, struct got_repository *repo)
465 const struct got_error *err;
470 while (*nfetched < limit) {
471 err = fetch_commits_from_open_branches(&ncommits, NULL,
477 *nfetched += ncommits;
483 const struct got_error *
484 got_commit_graph_fetch_commits_up_to(int *nfetched,
485 struct got_commit_graph *graph, struct got_object_id *wanted_id,
486 struct got_repository *repo)
488 const struct got_error *err;
489 int ncommits, wanted_id_added = 0;
493 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
496 while (!wanted_id_added) {
497 err = fetch_commits_from_open_branches(&ncommits,
498 &wanted_id_added, graph, repo, wanted_id);
503 *nfetched += ncommits;
510 free_node_iter(struct got_object_id *id, void *data, void *arg)
512 struct got_commit_graph_node *node = data;
517 got_commit_graph_close(struct got_commit_graph *graph)
519 got_object_idset_free(graph->open_branches);
520 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
521 got_object_idset_free(graph->node_ids);
525 const struct got_error *
526 got_commit_graph_iter_start(struct got_commit_graph *graph,
527 struct got_object_id *id)
529 struct got_commit_graph_node *start_node;
531 start_node = got_object_idset_get(graph->node_ids, id);
532 if (start_node == NULL)
533 return got_error(GOT_ERR_NO_OBJ);
535 graph->iter_node = start_node;
539 const struct got_error *
540 got_commit_graph_iter_next(struct got_object_id **id,
541 struct got_commit_graph *graph)
545 if (graph->iter_node == NULL) {
546 /* We are done interating, or iteration was not started. */
547 return got_error(GOT_ERR_ITER_COMPLETED);
550 if (graph->iter_node ==
551 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
552 got_object_idset_num_elements(graph->open_branches) == 0) {
553 *id = &graph->iter_node->id;
554 /* We are done interating. */
555 graph->iter_node = NULL;
559 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
560 return got_error(GOT_ERR_ITER_NEED_MORE);
562 *id = &graph->iter_node->id;
563 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);