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>
21 #include <sys/stdint.h>
30 #include "got_compat.h"
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_cancel.h"
35 #include "got_commit_graph.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_idset.h"
42 #include "got_lib_object_qid.h"
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 struct got_commit_graph_node
{
49 struct got_object_id id
;
51 /* Used for topological sorting. */
52 struct got_commit_graph_node
*parents
[2];
53 struct got_commit_graph_node
**more_parents
;
57 /* Used only during iteration. */
59 TAILQ_ENTRY(got_commit_graph_node
) entry
;
62 TAILQ_HEAD(got_commit_graph_iter_list
, got_commit_graph_node
);
64 struct got_commit_graph_branch_tip
{
65 struct got_object_id
*commit_id
;
66 struct got_commit_object
*commit
;
67 struct got_commit_graph_node
*new_node
;
70 struct got_commit_graph
{
71 /* The set of all commits we have traversed. */
72 struct got_object_idset
*node_ids
;
75 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
76 #define GOT_COMMIT_GRAPH_TOPOSORT 0x02
79 * A set of object IDs of known parent commits which we have not yet
80 * traversed. Each commit ID in this set represents a branch in commit
81 * history: Either the first-parent branch of the head node, or another
82 * branch corresponding to a traversed merge commit for which we have
83 * not traversed a branch point commit yet.
85 * Whenever we add a commit with a matching ID to the graph, we remove
86 * its corresponding element from this set, and add new elements for
87 * each of that commit's parent commits which were not traversed yet.
89 * When API users ask us to fetch more commits, we fetch commits from
90 * all currently open branches. This allows API users to process
91 * commits in linear order even though the history contains branches.
93 struct got_object_idset
*open_branches
;
95 /* Array of branch tips for fetch_commits_from_open_branches(). */
96 struct got_commit_graph_branch_tip
*tips
;
99 /* Path of tree entry of interest to the API user. */
103 * Nodes which will be passed to the API user next, sorted by
104 * commit timestamp. Sorted in topological order only if topological
105 * sorting was requested.
107 struct got_commit_graph_iter_list iter_list
;
110 static const struct got_error
*
111 detect_changed_path(int *changed
, struct got_commit_object
*commit
,
112 struct got_object_id
*commit_id
, const char *path
,
113 struct got_repository
*repo
)
115 const struct got_error
*err
= NULL
;
116 struct got_commit_object
*pcommit
= NULL
;
117 struct got_tree_object
*tree
= NULL
, *ptree
= NULL
;
118 struct got_object_qid
*pid
;
120 if (got_path_is_root_dir(path
)) {
127 pid
= STAILQ_FIRST(&commit
->parent_ids
);
129 struct got_object_id
*obj_id
;
130 err
= got_object_id_by_path(&obj_id
, repo
, commit
, path
);
132 if (err
->code
== GOT_ERR_NO_TREE_ENTRY
)
135 *changed
= 1; /* The path was created in this commit. */
140 err
= got_object_open_as_tree(&tree
, repo
, commit
->tree_id
);
144 err
= got_object_open_as_commit(&pcommit
, repo
, &pid
->id
);
148 err
= got_object_open_as_tree(&ptree
, repo
, pcommit
->tree_id
);
152 err
= got_object_tree_path_changed(changed
, tree
, ptree
, path
, repo
);
155 got_object_tree_close(tree
);
157 got_object_tree_close(ptree
);
159 got_object_commit_close(pcommit
);
164 add_node_to_iter_list(struct got_commit_graph
*graph
,
165 struct got_commit_graph_node
*node
, time_t committer_time
)
167 struct got_commit_graph_node
*n
, *next
;
169 node
->timestamp
= committer_time
;
171 n
= TAILQ_FIRST(&graph
->iter_list
);
173 next
= TAILQ_NEXT(n
, entry
);
174 if (next
&& node
->timestamp
>= next
->timestamp
) {
175 TAILQ_INSERT_BEFORE(next
, node
, entry
);
180 TAILQ_INSERT_TAIL(&graph
->iter_list
, node
, entry
);
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_repository
*repo
)
188 const struct got_error
*err
= NULL
;
189 struct got_commit_graph_node
*node
;
193 node
= calloc(1, sizeof(*node
));
195 return got_error_from_errno("calloc");
197 memcpy(&node
->id
, commit_id
, sizeof(node
->id
));
199 err
= got_object_idset_add(graph
->node_ids
, &node
->id
, node
);
208 * Ask got-read-pack to traverse first-parent history until a commit is
209 * encountered which modified graph->path, or until the pack file runs
210 * out of relevant commits. This is faster than sending an individual
211 * request for each commit stored in the pack file.
213 static const struct got_error
*
214 packed_first_parent_traversal(int *ncommits_traversed
,
215 struct got_commit_graph
*graph
, struct got_object_id
*commit_id
,
216 struct got_repository
*repo
)
218 const struct got_error
*err
= NULL
;
219 struct got_object_id_queue traversed_commits
;
220 struct got_object_qid
*qid
;
222 STAILQ_INIT(&traversed_commits
);
223 *ncommits_traversed
= 0;
225 err
= got_traverse_packed_commits(&traversed_commits
,
226 commit_id
, graph
->path
, repo
);
230 /* Add all traversed commits to the graph... */
231 STAILQ_FOREACH(qid
, &traversed_commits
, entry
) {
232 if (got_object_idset_contains(graph
->open_branches
, &qid
->id
))
234 if (got_object_idset_contains(graph
->node_ids
, &qid
->id
))
237 (*ncommits_traversed
)++;
239 /* ... except the last commit is the new branch tip. */
240 if (STAILQ_NEXT(qid
, entry
) == NULL
) {
241 err
= got_object_idset_add(graph
->open_branches
,
246 err
= got_object_idset_add(graph
->node_ids
, &qid
->id
, NULL
);
251 got_object_id_queue_free(&traversed_commits
);
255 static const struct got_error
*
256 close_branch(struct got_commit_graph
*graph
, struct got_object_id
*commit_id
)
258 const struct got_error
*err
;
260 err
= got_object_idset_remove(NULL
, graph
->open_branches
, commit_id
);
261 if (err
&& err
->code
!= GOT_ERR_NO_OBJ
)
266 static const struct got_error
*
267 advance_branch(struct got_commit_graph
*graph
, struct got_object_id
*commit_id
,
268 struct got_commit_object
*commit
, struct got_repository
*repo
)
270 const struct got_error
*err
;
271 struct got_object_qid
*qid
;
272 struct got_object_id
*merged_id
= NULL
;
274 err
= close_branch(graph
, commit_id
);
278 if (graph
->flags
& GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL
) {
279 qid
= STAILQ_FIRST(&commit
->parent_ids
);
281 got_object_idset_contains(graph
->open_branches
, &qid
->id
))
284 * The root directory always changes by definition, and when
285 * logging the root we want to traverse consecutive commits
286 * even if they point at the same tree.
287 * But if we are looking for a specific path then we can avoid
288 * fetching packed commits which did not modify the path and
289 * only fetch their IDs. This speeds up 'got blame'.
291 if (!got_path_is_root_dir(graph
->path
) &&
292 (commit
->flags
& GOT_COMMIT_FLAG_PACKED
)) {
294 err
= packed_first_parent_traversal(&ncommits
,
295 graph
, &qid
->id
, repo
);
296 if (err
|| ncommits
> 0)
299 return got_object_idset_add(graph
->open_branches
,
304 * If we are graphing commits for a specific path, skip branches
305 * which do not contribute any content to this path.
307 if (commit
->nparents
> 1 && !got_path_is_root_dir(graph
->path
)) {
308 err
= got_object_id_by_path(&merged_id
, repo
, commit
, graph
->path
);
309 if (err
&& err
->code
!= GOT_ERR_NO_TREE_ENTRY
)
311 /* The requested path does not exist in this merge commit. */
313 if (commit
->nparents
> 1 && !got_path_is_root_dir(graph
->path
) &&
315 struct got_object_id
*prev_id
= NULL
;
316 int branches_differ
= 0;
319 STAILQ_FOREACH(qid
, &commit
->parent_ids
, entry
) {
320 struct got_object_id
*id
= NULL
;
321 struct got_commit_object
*pcommit
= NULL
;
323 if (got_object_idset_contains(graph
->open_branches
,
327 err
= got_object_open_as_commit(&pcommit
, repo
,
334 err
= got_object_id_by_path(&id
, repo
, pcommit
,
336 got_object_commit_close(pcommit
);
339 if (err
->code
== GOT_ERR_NO_TREE_ENTRY
) {
349 if (!branches_differ
&&
350 got_object_id_cmp(id
, prev_id
) != 0)
357 * If a branch has created the merged content we can
358 * skip any other branches.
360 if (got_object_id_cmp(merged_id
, id
) == 0) {
361 err
= got_object_idset_add(graph
->open_branches
,
375 * If the path's content is the same on all branches,
376 * follow the first parent only.
378 if (!branches_differ
) {
379 qid
= STAILQ_FIRST(&commit
->parent_ids
);
382 if (got_object_idset_contains(graph
->open_branches
,
385 if (got_object_idset_contains(graph
->node_ids
,
387 return NULL
; /* parent already traversed */
388 return got_object_idset_add(graph
->open_branches
,
393 STAILQ_FOREACH(qid
, &commit
->parent_ids
, entry
) {
394 if (got_object_idset_contains(graph
->open_branches
, &qid
->id
))
396 if (got_object_idset_contains(graph
->node_ids
, &qid
->id
))
397 continue; /* parent already traversed */
398 err
= got_object_idset_add(graph
->open_branches
, &qid
->id
,
407 const struct got_error
*
408 got_commit_graph_open(struct got_commit_graph
**graph
,
409 const char *path
, int first_parent_traversal
)
411 const struct got_error
*err
= NULL
;
413 *graph
= calloc(1, sizeof(**graph
));
415 return got_error_from_errno("calloc");
417 TAILQ_INIT(&(*graph
)->iter_list
);
419 (*graph
)->path
= strdup(path
);
420 if ((*graph
)->path
== NULL
) {
421 err
= got_error_from_errno("strdup");
425 (*graph
)->node_ids
= got_object_idset_alloc();
426 if ((*graph
)->node_ids
== NULL
) {
427 err
= got_error_from_errno("got_object_idset_alloc");
431 (*graph
)->open_branches
= got_object_idset_alloc();
432 if ((*graph
)->open_branches
== NULL
) {
433 err
= got_error_from_errno("got_object_idset_alloc");
437 if (first_parent_traversal
)
438 (*graph
)->flags
|= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL
;
441 got_commit_graph_close(*graph
);
447 struct add_branch_tip_arg
{
448 struct got_commit_graph_branch_tip
*tips
;
450 struct got_repository
*repo
;
451 struct got_commit_graph
*graph
;
454 static const struct got_error
*
455 add_branch_tip(struct got_object_id
*commit_id
, void *data
, void *arg
)
457 const struct got_error
*err
;
458 struct add_branch_tip_arg
*a
= arg
;
459 struct got_commit_graph_node
*new_node
;
460 struct got_commit_object
*commit
;
462 err
= got_object_open_as_commit(&commit
, a
->repo
, commit_id
);
466 err
= add_node(&new_node
, a
->graph
, commit_id
, a
->repo
);
468 got_object_commit_close(commit
);
472 a
->tips
[a
->ntips
].commit_id
= &new_node
->id
;
473 a
->tips
[a
->ntips
].commit
= commit
;
474 a
->tips
[a
->ntips
].new_node
= new_node
;
480 static const struct got_error
*
481 fetch_commits_from_open_branches(struct got_commit_graph
*graph
,
482 struct got_repository
*repo
, got_cancel_cb cancel_cb
, void *cancel_arg
)
484 const struct got_error
*err
;
485 struct add_branch_tip_arg arg
;
488 ntips
= got_object_idset_num_elements(graph
->open_branches
);
492 /* (Re-)allocate branch tips array if necessary. */
493 if (graph
->ntips
< ntips
) {
494 struct got_commit_graph_branch_tip
*tips
;
495 tips
= recallocarray(graph
->tips
, graph
->ntips
, ntips
,
498 return got_error_from_errno("recallocarray");
500 graph
->ntips
= ntips
;
502 arg
.tips
= graph
->tips
;
503 arg
.ntips
= 0; /* add_branch_tip() will increment */
506 err
= got_object_idset_for_each(graph
->open_branches
, add_branch_tip
,
511 for (i
= 0; i
< arg
.ntips
; i
++) {
512 struct got_object_id
*commit_id
;
513 struct got_commit_object
*commit
;
514 struct got_commit_graph_node
*new_node
;
518 err
= (*cancel_cb
)(cancel_arg
);
523 commit_id
= arg
.tips
[i
].commit_id
;
524 commit
= arg
.tips
[i
].commit
;
525 new_node
= arg
.tips
[i
].new_node
;
527 err
= detect_changed_path(&changed
, commit
, commit_id
,
530 if (err
->code
!= GOT_ERR_NO_OBJ
)
533 * History of the path stops here on the current
534 * branch. Keep going on other branches.
536 err
= close_branch(graph
, commit_id
);
542 add_node_to_iter_list(graph
, new_node
,
543 got_object_commit_get_committer_time(commit
));
544 arg
.tips
[i
].new_node
= NULL
;
546 err
= advance_branch(graph
, commit_id
, commit
, repo
);
551 for (i
= 0; i
< arg
.ntips
; i
++) {
552 got_object_commit_close(arg
.tips
[i
].commit
);
553 free(arg
.tips
[i
].new_node
);
559 got_commit_graph_close(struct got_commit_graph
*graph
)
561 struct got_commit_graph_node
*node
;
563 while ((node
= TAILQ_FIRST(&graph
->iter_list
))) {
564 TAILQ_REMOVE(&graph
->iter_list
, node
, entry
);
565 free(node
->more_parents
);
569 if (graph
->open_branches
)
570 got_object_idset_free(graph
->open_branches
);
572 got_object_idset_free(graph
->node_ids
);
578 static const struct got_error
*
579 remove_branch_tip(struct got_object_id
*commit_id
, void *data
, void *arg
)
581 struct got_object_idset
*open_branches
= arg
;
583 return got_object_idset_remove(NULL
, open_branches
, commit_id
);
586 const struct got_error
*
587 got_commit_graph_bfsort(struct got_commit_graph
*graph
,
588 struct got_object_id
*id
, struct got_repository
*repo
,
589 got_cancel_cb cancel_cb
, void *cancel_arg
)
591 const struct got_error
*err
= NULL
;
592 struct got_commit_graph_node
*node
;
594 graph
->flags
&= ~GOT_COMMIT_GRAPH_TOPOSORT
;
596 /* Clear left-over state from previous iteration attempts. */
597 while ((node
= TAILQ_FIRST(&graph
->iter_list
)))
598 TAILQ_REMOVE(&graph
->iter_list
, node
, entry
);
599 err
= got_object_idset_for_each(graph
->open_branches
,
600 remove_branch_tip
, graph
->open_branches
);
604 err
= got_object_idset_add(graph
->open_branches
, id
, NULL
);
608 /* Locate first commit which changed graph->path. */
609 while (TAILQ_EMPTY(&graph
->iter_list
) &&
610 got_object_idset_num_elements(graph
->open_branches
) > 0) {
611 err
= fetch_commits_from_open_branches(graph
, repo
,
612 cancel_cb
, cancel_arg
);
617 if (TAILQ_EMPTY(&graph
->iter_list
)) {
619 if (got_path_is_root_dir(graph
->path
))
620 return got_error_no_obj(id
);
622 while (path
[0] == '/')
624 return got_error_path(path
, GOT_ERR_NO_TREE_ENTRY
);
630 const struct got_error
*
631 got_commit_graph_iter_next(struct got_object_id
*id
,
632 struct got_commit_graph
*graph
, struct got_repository
*repo
,
633 got_cancel_cb cancel_cb
, void *cancel_arg
)
635 const struct got_error
*err
= NULL
;
636 struct got_commit_graph_node
*node
, *pnode
;
639 node
= TAILQ_FIRST(&graph
->iter_list
);
641 /* We are done iterating, or iteration was not started. */
642 return got_error(GOT_ERR_ITER_COMPLETED
);
645 if (graph
->flags
& GOT_COMMIT_GRAPH_TOPOSORT
) {
646 /* At least one node with in-degree zero must exist. */
647 while (node
->indegree
!= 0)
648 node
= TAILQ_NEXT(node
, entry
);
650 while (TAILQ_NEXT(node
, entry
) == NULL
&&
651 got_object_idset_num_elements(graph
->open_branches
) > 0) {
652 err
= fetch_commits_from_open_branches(graph
, repo
,
653 cancel_cb
, cancel_arg
);
659 memcpy(id
, &node
->id
, sizeof(*id
));
661 TAILQ_REMOVE(&graph
->iter_list
, node
, entry
);
662 if (graph
->flags
& GOT_COMMIT_GRAPH_TOPOSORT
) {
663 /* When visiting a commit decrement in-degree of all parents. */
664 for (i
= 0; i
< node
->nparents
; i
++) {
665 if (i
< nitems(node
->parents
))
666 pnode
= node
->parents
[i
];
668 pnode
= node
->more_parents
[i
];
676 static const struct got_error
*
677 find_yca_add_id(struct got_object_id
**yca_id
, struct got_commit_graph
*graph
,
678 struct got_object_idset
*commit_ids
, struct got_repository
*repo
,
679 got_cancel_cb cancel_cb
, void *cancel_arg
)
681 const struct got_error
*err
= NULL
;
682 struct got_object_id id
;
684 err
= got_commit_graph_iter_next(&id
, graph
, repo
, cancel_cb
,
689 if (got_object_idset_contains(commit_ids
, &id
)) {
690 *yca_id
= got_object_id_dup(&id
);
692 err
= got_error_from_errno("got_object_id_dup");
696 return got_object_idset_add(commit_ids
, &id
, NULL
);
700 * Sets *yca_id to the youngest common ancestor of commit_id and
701 * commit_id2. Returns got_error(GOT_ERR_ANCESTRY) if they have no
704 * If first_parent_traversal is nonzero, only linear history is considered.
705 * If toposort is set then sort commits in topological order before
708 const struct got_error
*
709 got_commit_graph_find_youngest_common_ancestor(struct got_object_id
**yca_id
,
710 struct got_object_id
*commit_id
, struct got_object_id
*commit_id2
,
711 int first_parent_traversal
, int toposort
, struct got_repository
*repo
,
712 got_cancel_cb cancel_cb
, void *cancel_arg
)
714 const struct got_error
*err
= NULL
;
715 struct got_commit_graph
*graph
= NULL
, *graph2
= NULL
;
716 int completed
= 0, completed2
= 0;
717 struct got_object_idset
*commit_ids
;
721 commit_ids
= got_object_idset_alloc();
722 if (commit_ids
== NULL
)
723 return got_error_from_errno("got_object_idset_alloc");
725 err
= got_commit_graph_open(&graph
, "/", first_parent_traversal
);
729 err
= got_commit_graph_open(&graph2
, "/", first_parent_traversal
);
734 err
= got_commit_graph_toposort(graph
, commit_id
, repo
,
735 cancel_cb
, cancel_arg
);
739 err
= got_commit_graph_toposort(graph2
, commit_id2
, repo
,
740 cancel_cb
, cancel_arg
);
744 err
= got_commit_graph_bfsort(graph
, commit_id
, repo
,
745 cancel_cb
, cancel_arg
);
749 err
= got_commit_graph_bfsort(graph2
, commit_id2
, repo
,
750 cancel_cb
, cancel_arg
);
757 err
= (*cancel_cb
)(cancel_arg
);
763 err
= find_yca_add_id(yca_id
, graph
, commit_ids
, repo
,
764 cancel_cb
, cancel_arg
);
766 if (err
->code
!= GOT_ERR_ITER_COMPLETED
)
776 err
= find_yca_add_id(yca_id
, graph2
, commit_ids
, repo
,
777 cancel_cb
, cancel_arg
);
779 if (err
->code
!= GOT_ERR_ITER_COMPLETED
)
788 if (completed
&& completed2
) {
789 err
= got_error(GOT_ERR_ANCESTRY
);
794 got_object_idset_free(commit_ids
);
796 got_commit_graph_close(graph
);
798 got_commit_graph_close(graph2
);
803 * Sort the graph for traversal in topological order.
805 * This implementation is based on the description of topological sorting
806 * of git commits by Derrick Stolee at
807 * https://github.blog/2022-08-30-gits-database-internals-ii-commit-history-queries/#topological-sorting
808 * which reads as follows:
810 * The basic algorithm for topological sorting is Kahn’s algorithm which
811 * follows two big steps:
812 * 1. Walk all reachable commits, counting the number of times a commit appears
813 * as a parent of another commit. Call these numbers the in-degree of the
814 * commit, referencing the number of incoming edges.
815 * 2. Walk the reachable commits, but only visit a commit if its in-degree
816 * value is zero. When visiting a commit, decrement the in-degree value of
819 * This algorithm works because at least one of our starting points will
820 * have in-degree zero, and then decrementing the in-degree value is similar
821 * to deleting the commit from the graph, always having at least one commit
822 * with in-degree zero.
824 const struct got_error
*
825 got_commit_graph_toposort(struct got_commit_graph
*graph
,
826 struct got_object_id
*id
, struct got_repository
*repo
,
827 got_cancel_cb cancel_cb
, void *cancel_arg
)
829 const struct got_error
*err
= NULL
;
830 struct got_commit_graph_node
*node
= NULL
, *pnode
= NULL
;
831 struct got_commit_object
*commit
= NULL
;
832 struct got_object_id_queue commits
;
833 const struct got_object_id_queue
*parent_ids
;
834 struct got_object_qid
*qid
= NULL
, *pid
;
837 STAILQ_INIT(&commits
);
839 if (graph
->flags
& GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL
)
840 return got_commit_graph_bfsort(graph
, id
, repo
,
841 cancel_cb
, cancel_arg
);
843 /* Clear left-over state from previous iteration attempts. */
844 while ((node
= TAILQ_FIRST(&graph
->iter_list
)))
845 TAILQ_REMOVE(&graph
->iter_list
, node
, entry
);
846 err
= got_object_idset_for_each(graph
->open_branches
,
847 remove_branch_tip
, graph
->open_branches
);
851 graph
->flags
|= GOT_COMMIT_GRAPH_TOPOSORT
;
854 * Sorting the commit graph in topological order requires visiting
855 * every reachable commit. This is very expensive but there are
856 * ways to speed this up significantly in the future:
857 * 1) Run this loop in got-read-pack if possible.
858 * 2) Use Git's commit-graph file to compute the result incrementally.
859 * See the blog post linked above for details.
861 err
= got_object_qid_alloc_partial(&qid
);
864 memcpy(&qid
->id
, id
, sizeof(qid
->id
));
865 STAILQ_INSERT_TAIL(&commits
, qid
, entry
);
866 while (!STAILQ_EMPTY(&commits
)) {
868 err
= (*cancel_cb
)(cancel_arg
);
873 qid
= STAILQ_FIRST(&commits
);
874 STAILQ_REMOVE_HEAD(&commits
, entry
);
875 err
= got_object_open_as_commit(&commit
, repo
, &qid
->id
);
879 node
= got_object_idset_get(graph
->node_ids
, &qid
->id
);
881 err
= add_node(&node
, graph
, id
, repo
);
884 TAILQ_INSERT_TAIL(&graph
->iter_list
, node
, entry
);
887 got_object_qid_free(qid
);
890 if (node
->timestamp
!= 0) /* already traversed once */
893 if (node
->nparents
== -1) {
894 node
->nparents
= got_object_commit_get_nparents(commit
);
895 if (node
->nparents
> nitems(node
->parents
)) {
896 node
->more_parents
= calloc(node
->nparents
,
897 sizeof(*node
->more_parents
));
898 if (node
->more_parents
== NULL
) {
899 err
= got_error_from_errno("calloc");
906 node
->timestamp
= got_object_commit_get_committer_time(commit
);
907 parent_ids
= got_object_commit_get_parent_ids(commit
);
909 STAILQ_FOREACH(pid
, parent_ids
, entry
) {
911 err
= (*cancel_cb
)(cancel_arg
);
917 * Increment the in-degree counter every time a given
918 * commit appears as the parent of another commit.
920 pnode
= got_object_idset_get(graph
->node_ids
, &pid
->id
);
922 err
= add_node(&pnode
, graph
, &pid
->id
, repo
);
925 TAILQ_INSERT_TAIL(&graph
->iter_list
, pnode
,
931 * Cache parent pointers on the node to make future
932 * in-degree updates easier.
934 if (node
->nparents
<= nitems(node
->parents
)) {
935 node
->parents
[i
] = pnode
;
937 node
->more_parents
[i
] = pnode
;
938 if (i
< nitems(node
->parents
))
939 node
->parents
[i
] = pnode
;
943 /* Keep traversing through all parent commits. */
944 err
= got_object_qid_alloc_partial(&qid
);
947 memcpy(&qid
->id
, &pid
->id
, sizeof(qid
->id
));
948 STAILQ_INSERT_TAIL(&commits
, qid
, entry
);
952 got_object_commit_close(commit
);
957 got_object_commit_close(commit
);
958 got_object_qid_free(qid
);
959 got_object_id_queue_free(&commits
);