From 5b5e32ff49a976762af55f8fc8852455d8cf0d34 Mon Sep 17 00:00:00 2001 From: Kristofer Karlsson Date: Fri, 27 Nov 2026 16:59:24 +0100 Subject: [PATCH] commit-reach: stop single-result walk at the topo ceiling "git merge-base" without --all stops the walk early when it finds a merge base in the generation-ordered region of the commit-graph. This optimization does not apply when the merge base is found outside that region, e.g. among commits not yet in the commit-graph. Generalize it to stop once a merge base has been found and the next queued commit is in the ordered region, wherever the merge base was found. This is safe because no remaining commit can be a descendant of a candidate found so far: no commit in the ordered region can descend from one outside it, and descendants of a candidate inside it were visited first. Any redundancy among the candidates already found is handled by remove_redundant(). The main benefit is simpler and broader logic. The scenario is unlikely to matter much in practice, so no benchmark is included. Add a test with the merge base just outside the commit-graph. Walk steps in that test, before and after this change: commit-graph before after none 21 21 full 11 3 half 15 11 no-gdat 18 3 No further tests are added, as the ways this exit condition can go wrong are already well covered. This was verified with simple mutation testing: - Stopping at the first result even outside the ordered region, before a descendant candidate is found: the "clock skew, v1 commit-graph" test in t6600, and "merge-base G H" and "unsynchronized clocks" in t6010. - Stopping at the ceiling without a result: t5318, t6099 and t6600. - Stopping early when --all is given: t5318 and t6600. - Not stopping at all: the new test, through its step counts. Signed-off-by: Kristofer Karlsson --- .../technical/paint-down-to-common.adoc | 17 ++++++++++------- commit-reach.c | 16 ++++++++++------ t/t6600-test-reach.sh | 17 +++++++++++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Documentation/technical/paint-down-to-common.adoc b/Documentation/technical/paint-down-to-common.adoc index c33c7ea7c1d38e..b807f351ef1f41 100644 --- a/Documentation/technical/paint-down-to-common.adoc +++ b/Documentation/technical/paint-down-to-common.adoc @@ -140,13 +140,16 @@ dequeues a commit below this threshold. Single result ~~~~~~~~~~~~~ -When only one merge base is needed and the walk is in the -ordered region with generation ordering, the first candidate -found is necessarily the highest-generation common ancestor. -No remaining commit in the queue can be a descendant of this -candidate (generation ordering guarantees children are visited -first), so it cannot be redundant and the walk can stop -immediately. +When only one merge base is needed, the walk stops once it has found +a candidate and the next commit to visit is in the ordered region, +wherever the candidate was found. + +No remaining commit can then be a descendant of a candidate: no +commit in the ordered region can descend from one outside it (as +generation numbers never increase along parent links), and +descendants of a candidate inside it were visited first. Candidates +may still be redundant with each other; `remove_redundant()` handles +that. Related documentation --------------------- diff --git a/commit-reach.c b/commit-reach.c index 453aff0c1cb021..4281cc1ac066af 100644 --- a/commit-reach.c +++ b/commit-reach.c @@ -92,6 +92,7 @@ struct paint_state { timestamp_t min_generation; timestamp_t last_gen; timestamp_t topo_ceiling; + bool stop_below_ceiling; }; static void paint_count_update(struct paint_state *state, @@ -159,6 +160,9 @@ static struct commit *paint_queue_get(struct paint_state *state) if (generation < state->min_generation) return NULL; + if (state->stop_below_ceiling && generation < state->topo_ceiling) + return NULL; + /* * Check exit condition before decrementing: the counters * still include this commit, so the last non-stale commit @@ -232,13 +236,13 @@ static int paint_down_to_common(struct repository *r, commit->object.flags |= RESULT; tail = commit_list_append(commit, tail); /* - * When the queue is generation-ordered, - * no remaining common ancestor can be a - * descendant of this one. + * Only one merge base is wanted, and once the + * walk is below the topo ceiling no remaining + * commit can be a descendant of a candidate, so + * it is safe to stop there. */ - if (!(mb_flags & MERGE_BASE_FIND_ALL) && - state.last_gen < state.topo_ceiling) - break; + if (!(mb_flags & MERGE_BASE_FIND_ALL)) + state.stop_below_ceiling = true; } /* Mark parents of a found merge stale */ flags |= STALE; diff --git a/t/t6600-test-reach.sh b/t/t6600-test-reach.sh index 13c84e0a423138..1a70d5f0b3a185 100755 --- a/t/t6600-test-reach.sh +++ b/t/t6600-test-reach.sh @@ -417,6 +417,23 @@ test_expect_success 'merge-base --all commit-walk steps' ' test_paint_down_steps 81 9 57 37 ' +test_expect_success 'merge-base without --all, result found outside commit-graph' ' + # sr-MB and the tips are created after the commit-graph files + # were saved, so they are outside every graph, while their grid + # parents are inside the full and no-gdat graphs. Once the walk + # has found sr-MB and enters the graph, it can stop instead of + # walking the grid down to commit-1-1. + mb=$(git commit-tree -m sr-MB -p commit-1-1 "$skew_tree") && + tip1=$(git commit-tree -m sr-TIP1 \ + -p "$mb" -p commit-9-1 "$skew_tree") && + tip2=$(git commit-tree -m sr-TIP2 \ + -p "$mb" -p commit-1-9 "$skew_tree") && + >input && + echo "$mb" >expect && + run_all_modes git merge-base "$tip1" "$tip2" && + test_paint_down_steps 21 3 11 3 +' + test_expect_success 'merge-base --all with clock skew (side-exhaustion)' ' # Verify that the merge base is computed correctly even # when commits have non-monotonic commit dates.