Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/stdint.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sha1.h>
27 #include <sha2.h>
28 #include <zlib.h>
29 #include <ctype.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_cancel.h"
34 #include "got_commit_graph.h"
35 #include "got_path.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"
43 #ifndef nitems
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 #endif
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;
53 int nparents;
54 int indegree;
56 /* Used only during iteration. */
57 time_t timestamp;
58 TAILQ_ENTRY(got_commit_graph_node) entry;
59 };
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;
67 };
69 struct got_commit_graph {
70 /* The set of all commits we have traversed. */
71 struct got_object_idset *node_ids;
73 int flags;
74 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
75 #define GOT_COMMIT_GRAPH_TOPOSORT 0x02
77 /*
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.
83 *
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.
87 *
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.
91 */
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;
96 int ntips;
98 /* Path of tree entry of interest to the API user. */
99 char *path;
101 /*
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.
105 */
106 struct got_commit_graph_iter_list iter_list;
107 };
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)) {
120 *changed = 1;
121 return NULL;
124 *changed = 0;
126 pid = STAILQ_FIRST(&commit->parent_ids);
127 if (pid == NULL) {
128 struct got_object_id *obj_id;
129 err = got_object_id_by_path(&obj_id, repo, commit, path);
130 if (err) {
131 if (err->code == GOT_ERR_NO_TREE_ENTRY)
132 err = NULL;
133 } else
134 *changed = 1; /* The path was created in this commit. */
135 free(obj_id);
136 return err;
139 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
140 if (err)
141 return err;
143 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
144 if (err)
145 goto done;
147 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
148 if (err)
149 goto done;
151 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
152 done:
153 if (tree)
154 got_object_tree_close(tree);
155 if (ptree)
156 got_object_tree_close(ptree);
157 if (pcommit)
158 got_object_commit_close(pcommit);
159 return err;
162 static void
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);
171 while (n) {
172 next = TAILQ_NEXT(n, entry);
173 if (next && node->timestamp >= next->timestamp) {
174 TAILQ_INSERT_BEFORE(next, node, entry);
175 return;
177 n = next;
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;
190 *new_node = NULL;
192 node = calloc(1, sizeof(*node));
193 if (node == NULL)
194 return got_error_from_errno("calloc");
196 memcpy(&node->id, commit_id, sizeof(node->id));
197 node->nparents = -1;
198 err = got_object_idset_add(graph->node_ids, &node->id, node);
199 if (err)
200 free(node);
201 else
202 *new_node = node;
203 return err;
206 /*
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.
211 */
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);
226 if (err)
227 return err;
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))
232 continue;
233 if (got_object_idset_contains(graph->node_ids, &qid->id))
234 continue;
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,
241 &qid->id, NULL);
242 break;
245 err = got_object_idset_add(graph->node_ids, &qid->id, NULL);
246 if (err)
247 break;
250 got_object_id_queue_free(&traversed_commits);
251 return err;
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)
261 return err;
262 return NULL;
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);
274 if (err)
275 return err;
277 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
278 qid = STAILQ_FIRST(&commit->parent_ids);
279 if (qid == NULL ||
280 got_object_idset_contains(graph->open_branches, &qid->id))
281 return NULL;
282 /*
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'.
289 */
290 if (!got_path_is_root_dir(graph->path) &&
291 (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
292 int ncommits = 0;
293 err = packed_first_parent_traversal(&ncommits,
294 graph, &qid->id, repo);
295 if (err || ncommits > 0)
296 return err;
298 return got_object_idset_add(graph->open_branches,
299 &qid->id, NULL);
302 /*
303 * If we are graphing commits for a specific path, skip branches
304 * which do not contribute any content to this path.
305 */
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)
309 return err;
310 /* The requested path does not exist in this merge commit. */
312 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path) &&
313 merged_id != NULL) {
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,
323 &qid->id))
324 continue;
326 err = got_object_open_as_commit(&pcommit, repo,
327 &qid->id);
328 if (err) {
329 free(merged_id);
330 free(prev_id);
331 return err;
333 err = got_object_id_by_path(&id, repo, pcommit,
334 graph->path);
335 got_object_commit_close(pcommit);
336 pcommit = NULL;
337 if (err) {
338 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
339 branches_differ = 1;
340 continue;
342 free(merged_id);
343 free(prev_id);
344 return err;
347 if (prev_id) {
348 if (!branches_differ &&
349 got_object_id_cmp(id, prev_id) != 0)
350 branches_differ = 1;
351 free(prev_id);
353 prev_id = id;
355 /*
356 * If a branch has created the merged content we can
357 * skip any other branches.
358 */
359 if (got_object_id_cmp(merged_id, id) == 0) {
360 err = got_object_idset_add(graph->open_branches,
361 &qid->id, NULL);
362 free(merged_id);
363 free(id);
364 return err;
368 free(prev_id);
369 prev_id = NULL;
370 free(merged_id);
371 merged_id = NULL;
373 /*
374 * If the path's content is the same on all branches,
375 * follow the first parent only.
376 */
377 if (!branches_differ) {
378 qid = STAILQ_FIRST(&commit->parent_ids);
379 if (qid == NULL)
380 return NULL;
381 if (got_object_idset_contains(graph->open_branches,
382 &qid->id))
383 return NULL;
384 if (got_object_idset_contains(graph->node_ids,
385 &qid->id))
386 return NULL; /* parent already traversed */
387 return got_object_idset_add(graph->open_branches,
388 &qid->id, NULL);
392 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
393 if (got_object_idset_contains(graph->open_branches, &qid->id))
394 continue;
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,
398 NULL);
399 if (err)
400 return err;
403 return NULL;
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));
413 if (*graph == NULL)
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");
421 goto done;
424 (*graph)->node_ids = got_object_idset_alloc();
425 if ((*graph)->node_ids == NULL) {
426 err = got_error_from_errno("got_object_idset_alloc");
427 goto done;
430 (*graph)->open_branches = got_object_idset_alloc();
431 if ((*graph)->open_branches == NULL) {
432 err = got_error_from_errno("got_object_idset_alloc");
433 goto done;
436 if (first_parent_traversal)
437 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
438 done:
439 if (err) {
440 got_commit_graph_close(*graph);
441 *graph = NULL;
443 return err;
446 struct add_branch_tip_arg {
447 struct got_commit_graph_branch_tip *tips;
448 int ntips;
449 struct got_repository *repo;
450 struct got_commit_graph *graph;
451 };
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);
462 if (err)
463 return err;
465 err = add_node(&new_node, a->graph, commit_id, a->repo);
466 if (err) {
467 got_object_commit_close(commit);
468 return err;
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;
474 a->ntips++;
476 return NULL;
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;
485 int i, ntips;
487 ntips = got_object_idset_num_elements(graph->open_branches);
488 if (ntips == 0)
489 return NULL;
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,
495 sizeof(*tips));
496 if (tips == NULL)
497 return got_error_from_errno("recallocarray");
498 graph->tips = tips;
499 graph->ntips = ntips;
501 arg.tips = graph->tips;
502 arg.ntips = 0; /* add_branch_tip() will increment */
503 arg.repo = repo;
504 arg.graph = graph;
505 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
506 &arg);
507 if (err)
508 goto done;
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;
514 int changed;
516 if (cancel_cb) {
517 err = (*cancel_cb)(cancel_arg);
518 if (err)
519 break;
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,
527 graph->path, repo);
528 if (err) {
529 if (err->code != GOT_ERR_NO_OBJ)
530 break;
531 /*
532 * History of the path stops here on the current
533 * branch. Keep going on other branches.
534 */
535 err = close_branch(graph, commit_id);
536 if (err)
537 break;
538 continue;
540 if (changed) {
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);
546 if (err)
547 break;
549 done:
550 for (i = 0; i < arg.ntips; i++) {
551 got_object_commit_close(arg.tips[i].commit);
552 free(arg.tips[i].new_node);
554 return err;
557 void
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);
565 free(node);
568 if (graph->open_branches)
569 got_object_idset_free(graph->open_branches);
570 if (graph->node_ids)
571 got_object_idset_free(graph->node_ids);
572 free(graph->tips);
573 free(graph->path);
574 free(graph);
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);
600 if (err)
601 return err;
603 err = got_object_idset_add(graph->open_branches, id, NULL);
604 if (err)
605 return err;
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);
612 if (err)
613 return err;
616 if (TAILQ_EMPTY(&graph->iter_list)) {
617 const char *path;
618 if (got_path_is_root_dir(graph->path))
619 return got_error_no_obj(id);
620 path = graph->path;
621 while (path[0] == '/')
622 path++;
623 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
626 return NULL;
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;
636 int i;
638 node = TAILQ_FIRST(&graph->iter_list);
639 if (node == NULL) {
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);
648 } else {
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);
653 if (err)
654 return err;
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];
666 else
667 pnode = node->more_parents[i];
668 pnode->indegree--;
671 free(node);
672 return NULL;
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,
684 cancel_arg);
685 if (err)
686 return err;
688 if (got_object_idset_contains(commit_ids, &id)) {
689 *yca_id = got_object_id_dup(&id);
690 if (*yca_id == NULL)
691 err = got_error_from_errno("got_object_id_dup");
692 return err;
695 return got_object_idset_add(commit_ids, &id, NULL);
698 /*
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
701 * common ancestors.
703 * If first_parent_traversal is nonzero, only linear history is considered.
704 * If toposort is set then sort commits in topological order before
705 * traversing them.
706 */
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;
718 *yca_id = NULL;
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);
725 if (err)
726 goto done;
728 err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
729 if (err)
730 goto done;
732 if (toposort) {
733 err = got_commit_graph_toposort(graph, commit_id, repo,
734 cancel_cb, cancel_arg);
735 if (err)
736 goto done;
738 err = got_commit_graph_toposort(graph2, commit_id2, repo,
739 cancel_cb, cancel_arg);
740 if (err)
741 goto done;
742 } else {
743 err = got_commit_graph_bfsort(graph, commit_id, repo,
744 cancel_cb, cancel_arg);
745 if (err)
746 goto done;
748 err = got_commit_graph_bfsort(graph2, commit_id2, repo,
749 cancel_cb, cancel_arg);
750 if (err)
751 goto done;
754 for (;;) {
755 if (cancel_cb) {
756 err = (*cancel_cb)(cancel_arg);
757 if (err)
758 break;
761 if (!completed) {
762 err = find_yca_add_id(yca_id, graph, commit_ids, repo,
763 cancel_cb, cancel_arg);
764 if (err) {
765 if (err->code != GOT_ERR_ITER_COMPLETED)
766 break;
767 err = NULL;
768 completed = 1;
770 if (*yca_id)
771 break;
774 if (!completed2) {
775 err = find_yca_add_id(yca_id, graph2, commit_ids, repo,
776 cancel_cb, cancel_arg);
777 if (err) {
778 if (err->code != GOT_ERR_ITER_COMPLETED)
779 break;
780 err = NULL;
781 completed2 = 1;
783 if (*yca_id)
784 break;
787 if (completed && completed2) {
788 err = got_error(GOT_ERR_ANCESTRY);
789 break;
792 done:
793 got_object_idset_free(commit_ids);
794 if (graph)
795 got_commit_graph_close(graph);
796 if (graph2)
797 got_commit_graph_close(graph2);
798 return err;
801 /*
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
816 * each parent.
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.
822 */
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;
834 int i;
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);
847 if (err)
848 return err;
850 graph->flags |= GOT_COMMIT_GRAPH_TOPOSORT;
852 /*
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.
859 */
860 err = got_object_qid_alloc_partial(&qid);
861 if (err)
862 return err;
863 memcpy(&qid->id, id, sizeof(qid->id));
864 STAILQ_INSERT_TAIL(&commits, qid, entry);
865 while (!STAILQ_EMPTY(&commits)) {
866 if (cancel_cb) {
867 err = (*cancel_cb)(cancel_arg);
868 if (err)
869 break;
872 qid = STAILQ_FIRST(&commits);
873 STAILQ_REMOVE_HEAD(&commits, entry);
874 err = got_object_open_as_commit(&commit, repo, &qid->id);
875 if (err)
876 break;
878 node = got_object_idset_get(graph->node_ids, &qid->id);
879 if (node == NULL) {
880 err = add_node(&node, graph, id, repo);
881 if (err)
882 break;
883 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
886 got_object_qid_free(qid);
887 qid = NULL;
889 if (node->timestamp != 0) /* already traversed once */
890 continue;
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");
899 break;
905 node->timestamp = got_object_commit_get_committer_time(commit);
906 parent_ids = got_object_commit_get_parent_ids(commit);
907 i = 0;
908 STAILQ_FOREACH(pid, parent_ids, entry) {
909 if (cancel_cb) {
910 err = (*cancel_cb)(cancel_arg);
911 if (err)
912 goto done;
915 /*
916 * Increment the in-degree counter every time a given
917 * commit appears as the parent of another commit.
918 */
919 pnode = got_object_idset_get(graph->node_ids, &pid->id);
920 if (pnode == NULL) {
921 err = add_node(&pnode, graph, &pid->id, repo);
922 if (err)
923 goto done;
924 TAILQ_INSERT_TAIL(&graph->iter_list, pnode,
925 entry);
927 pnode->indegree++;
929 /*
930 * Cache parent pointers on the node to make future
931 * in-degree updates easier.
932 */
933 if (node->nparents <= nitems(node->parents)) {
934 node->parents[i] = pnode;
935 } else {
936 node->more_parents[i] = pnode;
937 if (i < nitems(node->parents))
938 node->parents[i] = pnode;
940 i++;
942 /* Keep traversing through all parent commits. */
943 err = got_object_qid_alloc_partial(&qid);
944 if (err)
945 goto done;
946 memcpy(&qid->id, &pid->id, sizeof(qid->id));
947 STAILQ_INSERT_TAIL(&commits, qid, entry);
948 qid = NULL;
951 got_object_commit_close(commit);
952 commit = NULL;
954 done:
955 if (commit)
956 got_object_commit_close(commit);
957 got_object_qid_free(qid);
958 got_object_id_queue_free(&commits);
959 return err;