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>
20 #include <sys/stdint.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_cancel.h"
34 #include "got_commit_graph.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_idset.h"
41 #include "got_lib_object_qid.h"
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 struct got_commit_graph_node {
48 struct got_object_id id;
50 /* Used for topological sorting. */
51 struct got_commit_graph_node *parents[2];
52 struct got_commit_graph_node **more_parents;
56 /* Used only during iteration. */
58 TAILQ_ENTRY(got_commit_graph_node) entry;
61 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
63 struct got_commit_graph_branch_tip {
64 struct got_object_id *commit_id;
65 struct got_commit_object *commit;
66 struct got_commit_graph_node *new_node;
69 struct got_commit_graph {
70 /* The set of all commits we have traversed. */
71 struct got_object_idset *node_ids;
74 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
75 #define GOT_COMMIT_GRAPH_TOPOSORT 0x02
78 * A set of object IDs of known parent commits which we have not yet
79 * traversed. Each commit ID in this set represents a branch in commit
80 * history: Either the first-parent branch of the head node, or another
81 * branch corresponding to a traversed merge commit for which we have
82 * not traversed a branch point commit yet.
84 * Whenever we add a commit with a matching ID to the graph, we remove
85 * its corresponding element from this set, and add new elements for
86 * each of that commit's parent commits which were not traversed yet.
88 * When API users ask us to fetch more commits, we fetch commits from
89 * all currently open branches. This allows API users to process
90 * commits in linear order even though the history contains branches.
92 struct got_object_idset *open_branches;
94 /* Array of branch tips for fetch_commits_from_open_branches(). */
95 struct got_commit_graph_branch_tip *tips;
98 /* Path of tree entry of interest to the API user. */
102 * Nodes which will be passed to the API user next, sorted by
103 * commit timestamp. Sorted in topological order only if topological
104 * sorting was requested.
106 struct got_commit_graph_iter_list iter_list;
109 static const struct got_error *
110 detect_changed_path(int *changed, struct got_commit_object *commit,
111 struct got_object_id *commit_id, const char *path,
112 struct got_repository *repo)
114 const struct got_error *err = NULL;
115 struct got_commit_object *pcommit = NULL;
116 struct got_tree_object *tree = NULL, *ptree = NULL;
117 struct got_object_qid *pid;
119 if (got_path_is_root_dir(path)) {
126 pid = STAILQ_FIRST(&commit->parent_ids);
128 struct got_object_id *obj_id;
129 err = got_object_id_by_path(&obj_id, repo, commit, path);
131 if (err->code == GOT_ERR_NO_TREE_ENTRY)
134 *changed = 1; /* The path was created in this commit. */
139 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
143 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
147 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
151 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
154 got_object_tree_close(tree);
156 got_object_tree_close(ptree);
158 got_object_commit_close(pcommit);
163 add_node_to_iter_list(struct got_commit_graph *graph,
164 struct got_commit_graph_node *node, time_t committer_time)
166 struct got_commit_graph_node *n, *next;
168 node->timestamp = committer_time;
170 n = TAILQ_FIRST(&graph->iter_list);
172 next = TAILQ_NEXT(n, entry);
173 if (next && node->timestamp >= next->timestamp) {
174 TAILQ_INSERT_BEFORE(next, node, entry);
179 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
182 static const struct got_error *
183 add_node(struct got_commit_graph_node **new_node,
184 struct got_commit_graph *graph, struct got_object_id *commit_id,
185 struct got_repository *repo)
187 const struct got_error *err = NULL;
188 struct got_commit_graph_node *node;
192 node = calloc(1, sizeof(*node));
194 return got_error_from_errno("calloc");
196 memcpy(&node->id, commit_id, sizeof(node->id));
198 err = got_object_idset_add(graph->node_ids, &node->id, node);
207 * Ask got-read-pack to traverse first-parent history until a commit is
208 * encountered which modified graph->path, or until the pack file runs
209 * out of relevant commits. This is faster than sending an individual
210 * request for each commit stored in the pack file.
212 static const struct got_error *
213 packed_first_parent_traversal(int *ncommits_traversed,
214 struct got_commit_graph *graph, struct got_object_id *commit_id,
215 struct got_repository *repo)
217 const struct got_error *err = NULL;
218 struct got_object_id_queue traversed_commits;
219 struct got_object_qid *qid;
221 STAILQ_INIT(&traversed_commits);
222 *ncommits_traversed = 0;
224 err = got_traverse_packed_commits(&traversed_commits,
225 commit_id, graph->path, repo);
229 /* Add all traversed commits to the graph... */
230 STAILQ_FOREACH(qid, &traversed_commits, entry) {
231 if (got_object_idset_contains(graph->open_branches, &qid->id))
233 if (got_object_idset_contains(graph->node_ids, &qid->id))
236 (*ncommits_traversed)++;
238 /* ... except the last commit is the new branch tip. */
239 if (STAILQ_NEXT(qid, entry) == NULL) {
240 err = got_object_idset_add(graph->open_branches,
245 err = got_object_idset_add(graph->node_ids, &qid->id, NULL);
250 got_object_id_queue_free(&traversed_commits);
254 static const struct got_error *
255 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
257 const struct got_error *err;
259 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
260 if (err && err->code != GOT_ERR_NO_OBJ)
265 static const struct got_error *
266 advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
267 struct got_commit_object *commit, struct got_repository *repo)
269 const struct got_error *err;
270 struct got_object_qid *qid;
271 struct got_object_id *merged_id = NULL;
273 err = close_branch(graph, commit_id);
277 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
278 qid = STAILQ_FIRST(&commit->parent_ids);
280 got_object_idset_contains(graph->open_branches, &qid->id))
283 * The root directory always changes by definition, and when
284 * logging the root we want to traverse consecutive commits
285 * even if they point at the same tree.
286 * But if we are looking for a specific path then we can avoid
287 * fetching packed commits which did not modify the path and
288 * only fetch their IDs. This speeds up 'got blame'.
290 if (!got_path_is_root_dir(graph->path) &&
291 (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
293 err = packed_first_parent_traversal(&ncommits,
294 graph, &qid->id, repo);
295 if (err || ncommits > 0)
298 return got_object_idset_add(graph->open_branches,
303 * If we are graphing commits for a specific path, skip branches
304 * which do not contribute any content to this path.
306 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
307 err = got_object_id_by_path(&merged_id, repo, commit, graph->path);
308 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
310 /* The requested path does not exist in this merge commit. */
312 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path) &&
314 struct got_object_id *prev_id = NULL;
315 int branches_differ = 0;
318 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
319 struct got_object_id *id = NULL;
320 struct got_commit_object *pcommit = NULL;
322 if (got_object_idset_contains(graph->open_branches,
326 err = got_object_open_as_commit(&pcommit, repo,
333 err = got_object_id_by_path(&id, repo, pcommit,
335 got_object_commit_close(pcommit);
338 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
348 if (!branches_differ &&
349 got_object_id_cmp(id, prev_id) != 0)
356 * If a branch has created the merged content we can
357 * skip any other branches.
359 if (got_object_id_cmp(merged_id, id) == 0) {
360 err = got_object_idset_add(graph->open_branches,
374 * If the path's content is the same on all branches,
375 * follow the first parent only.
377 if (!branches_differ) {
378 qid = STAILQ_FIRST(&commit->parent_ids);
381 if (got_object_idset_contains(graph->open_branches,
384 if (got_object_idset_contains(graph->node_ids,
386 return NULL; /* parent already traversed */
387 return got_object_idset_add(graph->open_branches,
392 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
393 if (got_object_idset_contains(graph->open_branches, &qid->id))
395 if (got_object_idset_contains(graph->node_ids, &qid->id))
396 continue; /* parent already traversed */
397 err = got_object_idset_add(graph->open_branches, &qid->id,
406 const struct got_error *
407 got_commit_graph_open(struct got_commit_graph **graph,
408 const char *path, int first_parent_traversal)
410 const struct got_error *err = NULL;
412 *graph = calloc(1, sizeof(**graph));
414 return got_error_from_errno("calloc");
416 TAILQ_INIT(&(*graph)->iter_list);
418 (*graph)->path = strdup(path);
419 if ((*graph)->path == NULL) {
420 err = got_error_from_errno("strdup");
424 (*graph)->node_ids = got_object_idset_alloc();
425 if ((*graph)->node_ids == NULL) {
426 err = got_error_from_errno("got_object_idset_alloc");
430 (*graph)->open_branches = got_object_idset_alloc();
431 if ((*graph)->open_branches == NULL) {
432 err = got_error_from_errno("got_object_idset_alloc");
436 if (first_parent_traversal)
437 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
440 got_commit_graph_close(*graph);
446 struct add_branch_tip_arg {
447 struct got_commit_graph_branch_tip *tips;
449 struct got_repository *repo;
450 struct got_commit_graph *graph;
453 static const struct got_error *
454 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
456 const struct got_error *err;
457 struct add_branch_tip_arg *a = arg;
458 struct got_commit_graph_node *new_node;
459 struct got_commit_object *commit;
461 err = got_object_open_as_commit(&commit, a->repo, commit_id);
465 err = add_node(&new_node, a->graph, commit_id, a->repo);
467 got_object_commit_close(commit);
471 a->tips[a->ntips].commit_id = &new_node->id;
472 a->tips[a->ntips].commit = commit;
473 a->tips[a->ntips].new_node = new_node;
479 static const struct got_error *
480 fetch_commits_from_open_branches(struct got_commit_graph *graph,
481 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
483 const struct got_error *err;
484 struct add_branch_tip_arg arg;
487 ntips = got_object_idset_num_elements(graph->open_branches);
491 /* (Re-)allocate branch tips array if necessary. */
492 if (graph->ntips < ntips) {
493 struct got_commit_graph_branch_tip *tips;
494 tips = recallocarray(graph->tips, graph->ntips, ntips,
497 return got_error_from_errno("recallocarray");
499 graph->ntips = ntips;
501 arg.tips = graph->tips;
502 arg.ntips = 0; /* add_branch_tip() will increment */
505 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
510 for (i = 0; i < arg.ntips; i++) {
511 struct got_object_id *commit_id;
512 struct got_commit_object *commit;
513 struct got_commit_graph_node *new_node;
517 err = (*cancel_cb)(cancel_arg);
522 commit_id = arg.tips[i].commit_id;
523 commit = arg.tips[i].commit;
524 new_node = arg.tips[i].new_node;
526 err = detect_changed_path(&changed, commit, commit_id,
529 if (err->code != GOT_ERR_NO_OBJ)
532 * History of the path stops here on the current
533 * branch. Keep going on other branches.
535 err = close_branch(graph, commit_id);
541 add_node_to_iter_list(graph, new_node,
542 got_object_commit_get_committer_time(commit));
543 arg.tips[i].new_node = NULL;
545 err = advance_branch(graph, commit_id, commit, repo);
550 for (i = 0; i < arg.ntips; i++) {
551 got_object_commit_close(arg.tips[i].commit);
552 free(arg.tips[i].new_node);
558 got_commit_graph_close(struct got_commit_graph *graph)
560 struct got_commit_graph_node *node;
562 while ((node = TAILQ_FIRST(&graph->iter_list))) {
563 TAILQ_REMOVE(&graph->iter_list, node, entry);
564 free(node->more_parents);
568 if (graph->open_branches)
569 got_object_idset_free(graph->open_branches);
571 got_object_idset_free(graph->node_ids);
577 static const struct got_error *
578 remove_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
580 struct got_object_idset *open_branches = arg;
582 return got_object_idset_remove(NULL, open_branches, commit_id);
585 const struct got_error *
586 got_commit_graph_bfsort(struct got_commit_graph *graph,
587 struct got_object_id *id, struct got_repository *repo,
588 got_cancel_cb cancel_cb, void *cancel_arg)
590 const struct got_error *err = NULL;
591 struct got_commit_graph_node *node;
593 graph->flags &= ~GOT_COMMIT_GRAPH_TOPOSORT;
595 /* Clear left-over state from previous iteration attempts. */
596 while ((node = TAILQ_FIRST(&graph->iter_list)))
597 TAILQ_REMOVE(&graph->iter_list, node, entry);
598 err = got_object_idset_for_each(graph->open_branches,
599 remove_branch_tip, graph->open_branches);
603 err = got_object_idset_add(graph->open_branches, id, NULL);
607 /* Locate first commit which changed graph->path. */
608 while (TAILQ_EMPTY(&graph->iter_list) &&
609 got_object_idset_num_elements(graph->open_branches) > 0) {
610 err = fetch_commits_from_open_branches(graph, repo,
611 cancel_cb, cancel_arg);
616 if (TAILQ_EMPTY(&graph->iter_list)) {
618 if (got_path_is_root_dir(graph->path))
619 return got_error_no_obj(id);
621 while (path[0] == '/')
623 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
629 const struct got_error *
630 got_commit_graph_iter_next(struct got_object_id *id,
631 struct got_commit_graph *graph, struct got_repository *repo,
632 got_cancel_cb cancel_cb, void *cancel_arg)
634 const struct got_error *err = NULL;
635 struct got_commit_graph_node *node, *pnode;
638 node = TAILQ_FIRST(&graph->iter_list);
640 /* We are done iterating, or iteration was not started. */
641 return got_error(GOT_ERR_ITER_COMPLETED);
644 if (graph->flags & GOT_COMMIT_GRAPH_TOPOSORT) {
645 /* At least one node with in-degree zero must exist. */
646 while (node->indegree != 0)
647 node = TAILQ_NEXT(node, entry);
649 while (TAILQ_NEXT(node, entry) == NULL &&
650 got_object_idset_num_elements(graph->open_branches) > 0) {
651 err = fetch_commits_from_open_branches(graph, repo,
652 cancel_cb, cancel_arg);
658 memcpy(id, &node->id, sizeof(*id));
660 TAILQ_REMOVE(&graph->iter_list, node, entry);
661 if (graph->flags & GOT_COMMIT_GRAPH_TOPOSORT) {
662 /* When visiting a commit decrement in-degree of all parents. */
663 for (i = 0; i < node->nparents; i++) {
664 if (i < nitems(node->parents))
665 pnode = node->parents[i];
667 pnode = node->more_parents[i];
675 static const struct got_error *
676 find_yca_add_id(struct got_object_id **yca_id, struct got_commit_graph *graph,
677 struct got_object_idset *commit_ids, struct got_repository *repo,
678 got_cancel_cb cancel_cb, void *cancel_arg)
680 const struct got_error *err = NULL;
681 struct got_object_id id;
683 err = got_commit_graph_iter_next(&id, graph, repo, cancel_cb,
688 if (got_object_idset_contains(commit_ids, &id)) {
689 *yca_id = got_object_id_dup(&id);
691 err = got_error_from_errno("got_object_id_dup");
695 return got_object_idset_add(commit_ids, &id, NULL);
699 * Sets *yca_id to the youngest common ancestor of commit_id and
700 * commit_id2. Returns got_error(GOT_ERR_ANCESTRY) if they have no
703 * If first_parent_traversal is nonzero, only linear history is considered.
704 * If toposort is set then sort commits in topological order before
707 const struct got_error *
708 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
709 struct got_object_id *commit_id, struct got_object_id *commit_id2,
710 int first_parent_traversal, int toposort, struct got_repository *repo,
711 got_cancel_cb cancel_cb, void *cancel_arg)
713 const struct got_error *err = NULL;
714 struct got_commit_graph *graph = NULL, *graph2 = NULL;
715 int completed = 0, completed2 = 0;
716 struct got_object_idset *commit_ids;
720 commit_ids = got_object_idset_alloc();
721 if (commit_ids == NULL)
722 return got_error_from_errno("got_object_idset_alloc");
724 err = got_commit_graph_open(&graph, "/", first_parent_traversal);
728 err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
733 err = got_commit_graph_toposort(graph, commit_id, repo,
734 cancel_cb, cancel_arg);
738 err = got_commit_graph_toposort(graph2, commit_id2, repo,
739 cancel_cb, cancel_arg);
743 err = got_commit_graph_bfsort(graph, commit_id, repo,
744 cancel_cb, cancel_arg);
748 err = got_commit_graph_bfsort(graph2, commit_id2, repo,
749 cancel_cb, cancel_arg);
756 err = (*cancel_cb)(cancel_arg);
762 err = find_yca_add_id(yca_id, graph, commit_ids, repo,
763 cancel_cb, cancel_arg);
765 if (err->code != GOT_ERR_ITER_COMPLETED)
775 err = find_yca_add_id(yca_id, graph2, commit_ids, repo,
776 cancel_cb, cancel_arg);
778 if (err->code != GOT_ERR_ITER_COMPLETED)
787 if (completed && completed2) {
788 err = got_error(GOT_ERR_ANCESTRY);
793 got_object_idset_free(commit_ids);
795 got_commit_graph_close(graph);
797 got_commit_graph_close(graph2);
802 * Sort the graph for traversal in topological order.
804 * This implementation is based on the description of topological sorting
805 * of git commits by Derrick Stolee at
806 * https://github.blog/2022-08-30-gits-database-internals-ii-commit-history-queries/#topological-sorting
807 * which reads as follows:
809 * The basic algorithm for topological sorting is Kahn’s algorithm which
810 * follows two big steps:
811 * 1. Walk all reachable commits, counting the number of times a commit appears
812 * as a parent of another commit. Call these numbers the in-degree of the
813 * commit, referencing the number of incoming edges.
814 * 2. Walk the reachable commits, but only visit a commit if its in-degree
815 * value is zero. When visiting a commit, decrement the in-degree value of
818 * This algorithm works because at least one of our starting points will
819 * have in-degree zero, and then decrementing the in-degree value is similar
820 * to deleting the commit from the graph, always having at least one commit
821 * with in-degree zero.
823 const struct got_error *
824 got_commit_graph_toposort(struct got_commit_graph *graph,
825 struct got_object_id *id, struct got_repository *repo,
826 got_cancel_cb cancel_cb, void *cancel_arg)
828 const struct got_error *err = NULL;
829 struct got_commit_graph_node *node = NULL, *pnode = NULL;
830 struct got_commit_object *commit = NULL;
831 struct got_object_id_queue commits;
832 const struct got_object_id_queue *parent_ids;
833 struct got_object_qid *qid = NULL, *pid;
836 STAILQ_INIT(&commits);
838 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL)
839 return got_commit_graph_bfsort(graph, id, repo,
840 cancel_cb, cancel_arg);
842 /* Clear left-over state from previous iteration attempts. */
843 while ((node = TAILQ_FIRST(&graph->iter_list)))
844 TAILQ_REMOVE(&graph->iter_list, node, entry);
845 err = got_object_idset_for_each(graph->open_branches,
846 remove_branch_tip, graph->open_branches);
850 graph->flags |= GOT_COMMIT_GRAPH_TOPOSORT;
853 * Sorting the commit graph in topological order requires visiting
854 * every reachable commit. This is very expensive but there are
855 * ways to speed this up significantly in the future:
856 * 1) Run this loop in got-read-pack if possible.
857 * 2) Use Git's commit-graph file to compute the result incrementally.
858 * See the blog post linked above for details.
860 err = got_object_qid_alloc_partial(&qid);
863 memcpy(&qid->id, id, sizeof(qid->id));
864 STAILQ_INSERT_TAIL(&commits, qid, entry);
865 while (!STAILQ_EMPTY(&commits)) {
867 err = (*cancel_cb)(cancel_arg);
872 qid = STAILQ_FIRST(&commits);
873 STAILQ_REMOVE_HEAD(&commits, entry);
874 err = got_object_open_as_commit(&commit, repo, &qid->id);
878 node = got_object_idset_get(graph->node_ids, &qid->id);
880 err = add_node(&node, graph, id, repo);
883 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
886 got_object_qid_free(qid);
889 if (node->timestamp != 0) /* already traversed once */
892 if (node->nparents == -1) {
893 node->nparents = got_object_commit_get_nparents(commit);
894 if (node->nparents > nitems(node->parents)) {
895 node->more_parents = calloc(node->nparents,
896 sizeof(*node->more_parents));
897 if (node->more_parents == NULL) {
898 err = got_error_from_errno("calloc");
905 node->timestamp = got_object_commit_get_committer_time(commit);
906 parent_ids = got_object_commit_get_parent_ids(commit);
908 STAILQ_FOREACH(pid, parent_ids, entry) {
910 err = (*cancel_cb)(cancel_arg);
916 * Increment the in-degree counter every time a given
917 * commit appears as the parent of another commit.
919 pnode = got_object_idset_get(graph->node_ids, &pid->id);
921 err = add_node(&pnode, graph, &pid->id, repo);
924 TAILQ_INSERT_TAIL(&graph->iter_list, pnode,
930 * Cache parent pointers on the node to make future
931 * in-degree updates easier.
933 if (node->nparents <= nitems(node->parents)) {
934 node->parents[i] = pnode;
936 node->more_parents[i] = pnode;
937 if (i < nitems(node->parents))
938 node->parents[i] = pnode;
942 /* Keep traversing through all parent commits. */
943 err = got_object_qid_alloc_partial(&qid);
946 memcpy(&qid->id, &pid->id, sizeof(qid->id));
947 STAILQ_INSERT_TAIL(&commits, qid, entry);
951 got_object_commit_close(commit);
956 got_object_commit_close(commit);
957 got_object_qid_free(qid);
958 got_object_id_queue_free(&commits);