2 * Copyright (c) 2018, 2019, 2020 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>
28 #include "got_compat.h"
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_cancel.h"
33 #include "got_commit_graph.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_idset.h"
40 #include "got_lib_object_qid.h"
43 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 struct got_commit_graph_node
{
47 struct got_object_id id
;
49 /* Used for topological sorting. */
50 struct got_commit_graph_node
*parents
[2];
51 struct got_commit_graph_node
**more_parents
;
55 /* Used only during iteration. */
57 TAILQ_ENTRY(got_commit_graph_node
) entry
;
60 TAILQ_HEAD(got_commit_graph_iter_list
, got_commit_graph_node
);
62 struct got_commit_graph_branch_tip
{
63 struct got_object_id
*commit_id
;
64 struct got_commit_object
*commit
;
65 struct got_commit_graph_node
*new_node
;
68 struct got_commit_graph
{
69 /* The set of all commits we have traversed. */
70 struct got_object_idset
*node_ids
;
73 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
74 #define GOT_COMMIT_GRAPH_TOPOSORT 0x02
77 * A set of object IDs of known parent commits which we have not yet
78 * traversed. Each commit ID in this set represents a branch in commit
79 * history: Either the first-parent branch of the head node, or another
80 * branch corresponding to a traversed merge commit for which we have
81 * not traversed a branch point commit yet.
83 * Whenever we add a commit with a matching ID to the graph, we remove
84 * its corresponding element from this set, and add new elements for
85 * each of that commit's parent commits which were not traversed yet.
87 * When API users ask us to fetch more commits, we fetch commits from
88 * all currently open branches. This allows API users to process
89 * commits in linear order even though the history contains branches.
91 struct got_object_idset
*open_branches
;
93 /* Array of branch tips for fetch_commits_from_open_branches(). */
94 struct got_commit_graph_branch_tip
*tips
;
97 /* Path of tree entry of interest to the API user. */
101 * Nodes which will be passed to the API user next, sorted by
102 * commit timestamp. Sorted in topological order only if topological
103 * sorting was requested.
105 struct got_commit_graph_iter_list iter_list
;
108 static const struct got_error
*
109 detect_changed_path(int *changed
, struct got_commit_object
*commit
,
110 struct got_object_id
*commit_id
, const char *path
,
111 struct got_repository
*repo
)
113 const struct got_error
*err
= NULL
;
114 struct got_commit_object
*pcommit
= NULL
;
115 struct got_tree_object
*tree
= NULL
, *ptree
= NULL
;
116 struct got_object_qid
*pid
;
118 if (got_path_is_root_dir(path
)) {
125 pid
= STAILQ_FIRST(&commit
->parent_ids
);
127 struct got_object_id
*obj_id
;
128 err
= got_object_id_by_path(&obj_id
, repo
, commit
, path
);
130 if (err
->code
== GOT_ERR_NO_TREE_ENTRY
)
133 *changed
= 1; /* The path was created in this commit. */
138 err
= got_object_open_as_tree(&tree
, repo
, commit
->tree_id
);
142 err
= got_object_open_as_commit(&pcommit
, repo
, &pid
->id
);
146 err
= got_object_open_as_tree(&ptree
, repo
, pcommit
->tree_id
);
150 err
= got_object_tree_path_changed(changed
, tree
, ptree
, path
, repo
);
153 got_object_tree_close(tree
);
155 got_object_tree_close(ptree
);
157 got_object_commit_close(pcommit
);
162 add_node_to_iter_list(struct got_commit_graph
*graph
,
163 struct got_commit_graph_node
*node
, time_t committer_time
)
165 struct got_commit_graph_node
*n
, *next
;
167 node
->timestamp
= committer_time
;
169 n
= TAILQ_FIRST(&graph
->iter_list
);
171 next
= TAILQ_NEXT(n
, entry
);
172 if (next
&& node
->timestamp
>= next
->timestamp
) {
173 TAILQ_INSERT_BEFORE(next
, node
, entry
);
178 TAILQ_INSERT_TAIL(&graph
->iter_list
, node
, entry
);
181 static const struct got_error
*
182 add_node(struct got_commit_graph_node
**new_node
,
183 struct got_commit_graph
*graph
, struct got_object_id
*commit_id
,
184 struct got_repository
*repo
)
186 const struct got_error
*err
= NULL
;
187 struct got_commit_graph_node
*node
;
191 node
= calloc(1, sizeof(*node
));
193 return got_error_from_errno("calloc");
195 memcpy(&node
->id
, commit_id
, sizeof(node
->id
));
197 err
= got_object_idset_add(graph
->node_ids
, &node
->id
, node
);
206 * Ask got-read-pack to traverse first-parent history until a commit is
207 * encountered which modified graph->path, or until the pack file runs
208 * out of relevant commits. This is faster than sending an individual
209 * request for each commit stored in the pack file.
211 static const struct got_error
*
212 packed_first_parent_traversal(int *ncommits_traversed
,
213 struct got_commit_graph
*graph
, struct got_object_id
*commit_id
,
214 struct got_repository
*repo
)
216 const struct got_error
*err
= NULL
;
217 struct got_object_id_queue traversed_commits
;
218 struct got_object_qid
*qid
;
220 STAILQ_INIT(&traversed_commits
);
221 *ncommits_traversed
= 0;
223 err
= got_traverse_packed_commits(&traversed_commits
,
224 commit_id
, graph
->path
, repo
);
228 /* Add all traversed commits to the graph... */
229 STAILQ_FOREACH(qid
, &traversed_commits
, entry
) {
230 if (got_object_idset_contains(graph
->open_branches
, &qid
->id
))
232 if (got_object_idset_contains(graph
->node_ids
, &qid
->id
))
235 (*ncommits_traversed
)++;
237 /* ... except the last commit is the new branch tip. */
238 if (STAILQ_NEXT(qid
, entry
) == NULL
) {
239 err
= got_object_idset_add(graph
->open_branches
,
244 err
= got_object_idset_add(graph
->node_ids
, &qid
->id
, NULL
);
249 got_object_id_queue_free(&traversed_commits
);
253 static const struct got_error
*
254 close_branch(struct got_commit_graph
*graph
, struct got_object_id
*commit_id
)
256 const struct got_error
*err
;
258 err
= got_object_idset_remove(NULL
, graph
->open_branches
, commit_id
);
259 if (err
&& err
->code
!= GOT_ERR_NO_OBJ
)
264 static const struct got_error
*
265 advance_branch(struct got_commit_graph
*graph
, struct got_object_id
*commit_id
,
266 struct got_commit_object
*commit
, struct got_repository
*repo
)
268 const struct got_error
*err
;
269 struct got_object_qid
*qid
;
270 struct got_object_id
*merged_id
= NULL
;
272 err
= close_branch(graph
, commit_id
);
276 if (graph
->flags
& GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL
) {
277 qid
= STAILQ_FIRST(&commit
->parent_ids
);
279 got_object_idset_contains(graph
->open_branches
, &qid
->id
))
282 * The root directory always changes by definition, and when
283 * logging the root we want to traverse consecutive commits
284 * even if they point at the same tree.
285 * But if we are looking for a specific path then we can avoid
286 * fetching packed commits which did not modify the path and
287 * only fetch their IDs. This speeds up 'got blame'.
289 if (!got_path_is_root_dir(graph
->path
) &&
290 (commit
->flags
& GOT_COMMIT_FLAG_PACKED
)) {
292 err
= packed_first_parent_traversal(&ncommits
,
293 graph
, &qid
->id
, repo
);
294 if (err
|| ncommits
> 0)
297 return got_object_idset_add(graph
->open_branches
,
302 * If we are graphing commits for a specific path, skip branches
303 * which do not contribute any content to this path.
305 if (commit
->nparents
> 1 && !got_path_is_root_dir(graph
->path
)) {
306 err
= got_object_id_by_path(&merged_id
, repo
, commit
, graph
->path
);
307 if (err
&& err
->code
!= GOT_ERR_NO_TREE_ENTRY
)
309 /* The requested path does not exist in this merge commit. */
311 if (commit
->nparents
> 1 && !got_path_is_root_dir(graph
->path
) &&
313 struct got_object_id
*prev_id
= NULL
;
314 int branches_differ
= 0;
317 STAILQ_FOREACH(qid
, &commit
->parent_ids
, entry
) {
318 struct got_object_id
*id
= NULL
;
319 struct got_commit_object
*pcommit
= NULL
;
321 if (got_object_idset_contains(graph
->open_branches
,
325 err
= got_object_open_as_commit(&pcommit
, repo
,
332 err
= got_object_id_by_path(&id
, repo
, pcommit
,
334 got_object_commit_close(pcommit
);
337 if (err
->code
== GOT_ERR_NO_TREE_ENTRY
) {
347 if (!branches_differ
&&
348 got_object_id_cmp(id
, prev_id
) != 0)
355 * If a branch has created the merged content we can
356 * skip any other branches.
358 if (got_object_id_cmp(merged_id
, id
) == 0) {
359 err
= got_object_idset_add(graph
->open_branches
,
373 * If the path's content is the same on all branches,
374 * follow the first parent only.
376 if (!branches_differ
) {
377 qid
= STAILQ_FIRST(&commit
->parent_ids
);
380 if (got_object_idset_contains(graph
->open_branches
,
383 if (got_object_idset_contains(graph
->node_ids
,
385 return NULL
; /* parent already traversed */
386 return got_object_idset_add(graph
->open_branches
,
391 STAILQ_FOREACH(qid
, &commit
->parent_ids
, entry
) {
392 if (got_object_idset_contains(graph
->open_branches
, &qid
->id
))
394 if (got_object_idset_contains(graph
->node_ids
, &qid
->id
))
395 continue; /* parent already traversed */
396 err
= got_object_idset_add(graph
->open_branches
, &qid
->id
,
405 const struct got_error
*
406 got_commit_graph_open(struct got_commit_graph
**graph
,
407 const char *path
, int first_parent_traversal
)
409 const struct got_error
*err
= NULL
;
411 *graph
= calloc(1, sizeof(**graph
));
413 return got_error_from_errno("calloc");
415 TAILQ_INIT(&(*graph
)->iter_list
);
417 (*graph
)->path
= strdup(path
);
418 if ((*graph
)->path
== NULL
) {
419 err
= got_error_from_errno("strdup");
423 (*graph
)->node_ids
= got_object_idset_alloc();
424 if ((*graph
)->node_ids
== NULL
) {
425 err
= got_error_from_errno("got_object_idset_alloc");
429 (*graph
)->open_branches
= got_object_idset_alloc();
430 if ((*graph
)->open_branches
== NULL
) {
431 err
= got_error_from_errno("got_object_idset_alloc");
435 if (first_parent_traversal
)
436 (*graph
)->flags
|= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL
;
439 got_commit_graph_close(*graph
);
445 struct add_branch_tip_arg
{
446 struct got_commit_graph_branch_tip
*tips
;
448 struct got_repository
*repo
;
449 struct got_commit_graph
*graph
;
452 static const struct got_error
*
453 add_branch_tip(struct got_object_id
*commit_id
, void *data
, void *arg
)
455 const struct got_error
*err
;
456 struct add_branch_tip_arg
*a
= arg
;
457 struct got_commit_graph_node
*new_node
;
458 struct got_commit_object
*commit
;
460 err
= got_object_open_as_commit(&commit
, a
->repo
, commit_id
);
464 err
= add_node(&new_node
, a
->graph
, commit_id
, a
->repo
);
466 got_object_commit_close(commit
);
470 a
->tips
[a
->ntips
].commit_id
= &new_node
->id
;
471 a
->tips
[a
->ntips
].commit
= commit
;
472 a
->tips
[a
->ntips
].new_node
= new_node
;
478 static const struct got_error
*
479 fetch_commits_from_open_branches(struct got_commit_graph
*graph
,
480 struct got_repository
*repo
, got_cancel_cb cancel_cb
, void *cancel_arg
)
482 const struct got_error
*err
;
483 struct add_branch_tip_arg arg
;
486 ntips
= got_object_idset_num_elements(graph
->open_branches
);
490 /* (Re-)allocate branch tips array if necessary. */
491 if (graph
->ntips
< ntips
) {
492 struct got_commit_graph_branch_tip
*tips
;
493 tips
= recallocarray(graph
->tips
, graph
->ntips
, ntips
,
496 return got_error_from_errno("recallocarray");
498 graph
->ntips
= ntips
;
500 arg
.tips
= graph
->tips
;
501 arg
.ntips
= 0; /* add_branch_tip() will increment */
504 err
= got_object_idset_for_each(graph
->open_branches
, add_branch_tip
,
509 for (i
= 0; i
< arg
.ntips
; i
++) {
510 struct got_object_id
*commit_id
;
511 struct got_commit_object
*commit
;
512 struct got_commit_graph_node
*new_node
;
516 err
= (*cancel_cb
)(cancel_arg
);
521 commit_id
= arg
.tips
[i
].commit_id
;
522 commit
= arg
.tips
[i
].commit
;
523 new_node
= arg
.tips
[i
].new_node
;
525 err
= detect_changed_path(&changed
, commit
, commit_id
,
528 if (err
->code
!= GOT_ERR_NO_OBJ
)
531 * History of the path stops here on the current
532 * branch. Keep going on other branches.
534 err
= close_branch(graph
, commit_id
);
540 add_node_to_iter_list(graph
, new_node
,
541 got_object_commit_get_committer_time(commit
));
542 arg
.tips
[i
].new_node
= NULL
;
544 err
= advance_branch(graph
, commit_id
, commit
, repo
);
549 for (i
= 0; i
< arg
.ntips
; i
++) {
550 got_object_commit_close(arg
.tips
[i
].commit
);
551 free(arg
.tips
[i
].new_node
);
557 got_commit_graph_close(struct got_commit_graph
*graph
)
559 struct got_commit_graph_node
*node
;
561 while ((node
= TAILQ_FIRST(&graph
->iter_list
))) {
562 TAILQ_REMOVE(&graph
->iter_list
, node
, entry
);
563 free(node
->more_parents
);
567 if (graph
->open_branches
)
568 got_object_idset_free(graph
->open_branches
);
570 got_object_idset_free(graph
->node_ids
);
576 static const struct got_error
*
577 remove_branch_tip(struct got_object_id
*commit_id
, void *data
, void *arg
)
579 struct got_object_idset
*open_branches
= arg
;
581 return got_object_idset_remove(NULL
, open_branches
, commit_id
);
584 const struct got_error
*
585 got_commit_graph_bfsort(struct got_commit_graph
*graph
,
586 struct got_object_id
*id
, struct got_repository
*repo
,
587 got_cancel_cb cancel_cb
, void *cancel_arg
)
589 const struct got_error
*err
= NULL
;
590 struct got_commit_graph_node
*node
;
592 graph
->flags
&= ~GOT_COMMIT_GRAPH_TOPOSORT
;
594 /* Clear left-over state from previous iteration attempts. */
595 while ((node
= TAILQ_FIRST(&graph
->iter_list
)))
596 TAILQ_REMOVE(&graph
->iter_list
, node
, entry
);
597 err
= got_object_idset_for_each(graph
->open_branches
,
598 remove_branch_tip
, graph
->open_branches
);
602 err
= got_object_idset_add(graph
->open_branches
, id
, NULL
);
606 /* Locate first commit which changed graph->path. */
607 while (TAILQ_EMPTY(&graph
->iter_list
) &&
608 got_object_idset_num_elements(graph
->open_branches
) > 0) {
609 err
= fetch_commits_from_open_branches(graph
, repo
,
610 cancel_cb
, cancel_arg
);
615 if (TAILQ_EMPTY(&graph
->iter_list
)) {
617 if (got_path_is_root_dir(graph
->path
))
618 return got_error_no_obj(id
);
620 while (path
[0] == '/')
622 return got_error_path(path
, GOT_ERR_NO_TREE_ENTRY
);
628 const struct got_error
*
629 got_commit_graph_iter_next(struct got_object_id
*id
,
630 struct got_commit_graph
*graph
, struct got_repository
*repo
,
631 got_cancel_cb cancel_cb
, void *cancel_arg
)
633 const struct got_error
*err
= NULL
;
634 struct got_commit_graph_node
*node
, *pnode
;
637 node
= TAILQ_FIRST(&graph
->iter_list
);
639 /* We are done iterating, or iteration was not started. */
640 return got_error(GOT_ERR_ITER_COMPLETED
);
643 if (graph
->flags
& GOT_COMMIT_GRAPH_TOPOSORT
) {
644 /* At least one node with in-degree zero must exist. */
645 while (node
->indegree
!= 0)
646 node
= TAILQ_NEXT(node
, entry
);
648 while (TAILQ_NEXT(node
, entry
) == NULL
&&
649 got_object_idset_num_elements(graph
->open_branches
) > 0) {
650 err
= fetch_commits_from_open_branches(graph
, repo
,
651 cancel_cb
, cancel_arg
);
657 memcpy(id
, &node
->id
, sizeof(*id
));
659 TAILQ_REMOVE(&graph
->iter_list
, node
, entry
);
660 if (graph
->flags
& GOT_COMMIT_GRAPH_TOPOSORT
) {
661 /* When visiting a commit decrement in-degree of all parents. */
662 for (i
= 0; i
< node
->nparents
; i
++) {
663 if (i
< nitems(node
->parents
))
664 pnode
= node
->parents
[i
];
666 pnode
= node
->more_parents
[i
];
674 static const struct got_error
*
675 find_yca_add_id(struct got_object_id
**yca_id
, struct got_commit_graph
*graph
,
676 struct got_object_idset
*commit_ids
, struct got_repository
*repo
,
677 got_cancel_cb cancel_cb
, void *cancel_arg
)
679 const struct got_error
*err
= NULL
;
680 struct got_object_id id
;
682 err
= got_commit_graph_iter_next(&id
, graph
, repo
, cancel_cb
,
687 if (got_object_idset_contains(commit_ids
, &id
)) {
688 *yca_id
= got_object_id_dup(&id
);
690 err
= got_error_from_errno("got_object_id_dup");
694 return got_object_idset_add(commit_ids
, &id
, NULL
);
698 * Sets *yca_id to the youngest common ancestor of commit_id and
699 * commit_id2. Returns got_error(GOT_ERR_ANCESTRY) if they have no
702 * If first_parent_traversal is nonzero, only linear history is considered.
703 * If toposort is set then sort commits in topological order before
706 const struct got_error
*
707 got_commit_graph_find_youngest_common_ancestor(struct got_object_id
**yca_id
,
708 struct got_object_id
*commit_id
, struct got_object_id
*commit_id2
,
709 int first_parent_traversal
, int toposort
, struct got_repository
*repo
,
710 got_cancel_cb cancel_cb
, void *cancel_arg
)
712 const struct got_error
*err
= NULL
;
713 struct got_commit_graph
*graph
= NULL
, *graph2
= NULL
;
714 int completed
= 0, completed2
= 0;
715 struct got_object_idset
*commit_ids
;
719 commit_ids
= got_object_idset_alloc();
720 if (commit_ids
== NULL
)
721 return got_error_from_errno("got_object_idset_alloc");
723 err
= got_commit_graph_open(&graph
, "/", first_parent_traversal
);
727 err
= got_commit_graph_open(&graph2
, "/", first_parent_traversal
);
732 err
= got_commit_graph_toposort(graph
, commit_id
, repo
,
733 cancel_cb
, cancel_arg
);
737 err
= got_commit_graph_toposort(graph2
, commit_id2
, repo
,
738 cancel_cb
, cancel_arg
);
742 err
= got_commit_graph_bfsort(graph
, commit_id
, repo
,
743 cancel_cb
, cancel_arg
);
747 err
= got_commit_graph_bfsort(graph2
, commit_id2
, repo
,
748 cancel_cb
, cancel_arg
);
755 err
= (*cancel_cb
)(cancel_arg
);
761 err
= find_yca_add_id(yca_id
, graph
, commit_ids
, repo
,
762 cancel_cb
, cancel_arg
);
764 if (err
->code
!= GOT_ERR_ITER_COMPLETED
)
774 err
= find_yca_add_id(yca_id
, graph2
, commit_ids
, repo
,
775 cancel_cb
, cancel_arg
);
777 if (err
->code
!= GOT_ERR_ITER_COMPLETED
)
786 if (completed
&& completed2
) {
787 err
= got_error(GOT_ERR_ANCESTRY
);
792 got_object_idset_free(commit_ids
);
794 got_commit_graph_close(graph
);
796 got_commit_graph_close(graph2
);
801 * Sort the graph for traversal in topological order.
803 * This implementation is based on the description of topological sorting
804 * of git commits by Derrick Stolee at
805 * https://github.blog/2022-08-30-gits-database-internals-ii-commit-history-queries/#topological-sorting
806 * which reads as follows:
808 * The basic algorithm for topological sorting is Kahn’s algorithm which
809 * follows two big steps:
810 * 1. Walk all reachable commits, counting the number of times a commit appears
811 * as a parent of another commit. Call these numbers the in-degree of the
812 * commit, referencing the number of incoming edges.
813 * 2. Walk the reachable commits, but only visit a commit if its in-degree
814 * value is zero. When visiting a commit, decrement the in-degree value of
817 * This algorithm works because at least one of our starting points will
818 * have in-degree zero, and then decrementing the in-degree value is similar
819 * to deleting the commit from the graph, always having at least one commit
820 * with in-degree zero.
822 const struct got_error
*
823 got_commit_graph_toposort(struct got_commit_graph
*graph
,
824 struct got_object_id
*id
, struct got_repository
*repo
,
825 got_cancel_cb cancel_cb
, void *cancel_arg
)
827 const struct got_error
*err
= NULL
;
828 struct got_commit_graph_node
*node
= NULL
, *pnode
= NULL
;
829 struct got_commit_object
*commit
= NULL
;
830 struct got_object_id_queue commits
;
831 const struct got_object_id_queue
*parent_ids
;
832 struct got_object_qid
*qid
= NULL
, *pid
;
835 STAILQ_INIT(&commits
);
837 if (graph
->flags
& GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL
)
838 return got_commit_graph_bfsort(graph
, id
, repo
,
839 cancel_cb
, cancel_arg
);
841 /* Clear left-over state from previous iteration attempts. */
842 while ((node
= TAILQ_FIRST(&graph
->iter_list
)))
843 TAILQ_REMOVE(&graph
->iter_list
, node
, entry
);
844 err
= got_object_idset_for_each(graph
->open_branches
,
845 remove_branch_tip
, graph
->open_branches
);
849 graph
->flags
|= GOT_COMMIT_GRAPH_TOPOSORT
;
852 * Sorting the commit graph in topological order requires visiting
853 * every reachable commit. This is very expensive but there are
854 * ways to speed this up significantly in the future:
855 * 1) Run this loop in got-read-pack if possible.
856 * 2) Use Git's commit-graph file to compute the result incrementally.
857 * See the blog post linked above for details.
859 err
= got_object_qid_alloc_partial(&qid
);
862 memcpy(&qid
->id
, id
, sizeof(qid
->id
));
863 STAILQ_INSERT_TAIL(&commits
, qid
, entry
);
864 while (!STAILQ_EMPTY(&commits
)) {
866 err
= (*cancel_cb
)(cancel_arg
);
871 qid
= STAILQ_FIRST(&commits
);
872 STAILQ_REMOVE_HEAD(&commits
, entry
);
873 err
= got_object_open_as_commit(&commit
, repo
, &qid
->id
);
877 node
= got_object_idset_get(graph
->node_ids
, &qid
->id
);
879 err
= add_node(&node
, graph
, id
, repo
);
882 TAILQ_INSERT_TAIL(&graph
->iter_list
, node
, entry
);
885 got_object_qid_free(qid
);
888 if (node
->timestamp
!= 0) /* already traversed once */
891 if (node
->nparents
== -1) {
892 node
->nparents
= got_object_commit_get_nparents(commit
);
893 if (node
->nparents
> nitems(node
->parents
)) {
894 node
->more_parents
= calloc(node
->nparents
,
895 sizeof(*node
->more_parents
));
896 if (node
->more_parents
== NULL
) {
897 err
= got_error_from_errno("calloc");
904 node
->timestamp
= got_object_commit_get_committer_time(commit
);
905 parent_ids
= got_object_commit_get_parent_ids(commit
);
907 STAILQ_FOREACH(pid
, parent_ids
, entry
) {
909 err
= (*cancel_cb
)(cancel_arg
);
915 * Increment the in-degree counter every time a given
916 * commit appears as the parent of another commit.
918 pnode
= got_object_idset_get(graph
->node_ids
, &pid
->id
);
920 err
= add_node(&pnode
, graph
, &pid
->id
, repo
);
923 TAILQ_INSERT_TAIL(&graph
->iter_list
, pnode
,
929 * Cache parent pointers on the node to make future
930 * in-degree updates easier.
932 if (node
->nparents
<= nitems(node
->parents
)) {
933 node
->parents
[i
] = pnode
;
935 node
->more_parents
[i
] = pnode
;
936 if (i
< nitems(node
->parents
))
937 node
->parents
[i
] = pnode
;
941 /* Keep traversing through all parent commits. */
942 err
= got_object_qid_alloc_partial(&qid
);
945 memcpy(&qid
->id
, &pid
->id
, sizeof(qid
->id
));
946 STAILQ_INSERT_TAIL(&commits
, qid
, entry
);
950 got_object_commit_close(commit
);
955 got_object_commit_close(commit
);
956 got_object_qid_free(qid
);
957 got_object_id_queue_free(&commits
);