From 13f43e8bf7e2a20c34e7195ae490bf368a2cdd4b Mon Sep 17 00:00:00 2001 From: Kristofer Karlsson Date: Sun, 14 Jun 2026 11:28:50 +0200 Subject: [PATCH 1/5] commit-reach: introduce struct paint_queue with per-side counters Replace the nonstale_queue abstraction in paint_down_to_common() with a new paint_queue struct that tracks per-side commit counts. Each non-stale queued commit occupies exactly one counter bucket based on its paint flags: PARENT1-only, PARENT2-only, or both sides (a pending merge-base candidate). The counters are maintained by paint_count_transition() which handles all flag changes as bucket transfers: remove from the old bucket, add to the new one. Either step is a no-op when the respective state has no bucket (stale or zero). paint_queue_put() atomically updates flags, handles ENQUEUED-based dedup, and adjusts counters. paint_queue_get() pops a commit and decrements its counter. The loop condition changes from pointer-based (while max_nonstale) to counter-based (while any counter is positive), which is equivalent for termination but gives callers precise visibility into the queue composition. The ahead_behind() function continues to use its own insert_no_dup() helper with a single max_nonstale pointer, since it only needs one nonstale dimension and has no per-side tracking. No behavior change. Signed-off-by: Kristofer Karlsson --- commit-reach.c | 149 +++++++++++++++++++++++++++++-------------------- 1 file changed, 90 insertions(+), 59 deletions(-) diff --git a/commit-reach.c b/commit-reach.c index 5df471a313cf6b..8f107f24a590e8 100644 --- a/commit-reach.c +++ b/commit-reach.c @@ -41,58 +41,81 @@ static int compare_commits_by_gen(const void *_a, const void *_b) } /* - * A prio_queue with O(1) termination check. 'max_nonstale' tracks - * the lowest-priority non-stale commit enqueued so far; once it is - * popped, every remaining entry is known to be STALE. + * Priority queue with per-side commit counters for paint_down_to_common(). + * Each non-stale queued commit occupies exactly one bucket: PARENT1-only, + * PARENT2-only, or both (a pending merge-base candidate). */ -struct nonstale_queue { +struct paint_queue { struct prio_queue pq; - struct commit *max_nonstale; + int p1_count; + int p2_count; + int pending_merge_bases; }; -static void nonstale_queue_put(struct nonstale_queue *queue, - struct commit *c) +/* + * Adjust per-side counters when a queued commit's paint flags change. + * Each non-stale commit occupies exactly one counter bucket based on + * its paint: PARENT1-only, PARENT2-only, or both sides. Stale + * commits and commits outside the queue (flags=0) occupy no bucket. + * A flag change is a bucket transfer: remove from the old bucket, + * then add to the new one. Either step is a no-op when the + * respective state has no bucket (stale or zero). + */ +static void paint_count_transition(struct paint_queue *queue, + unsigned old_flags, unsigned new_flags) { - struct commit *old = queue->max_nonstale; + unsigned old_paint = old_flags & (PARENT1 | PARENT2 | STALE); + unsigned new_paint = new_flags & (PARENT1 | PARENT2 | STALE); - prio_queue_put(&queue->pq, c); - if (c->object.flags & STALE) + if (old_paint == new_paint) return; - if (!old || queue->pq.compare(old, c, queue->pq.cb_data) <= 0) - queue->max_nonstale = c; -} - -static struct commit *nonstale_queue_get(struct nonstale_queue *queue) -{ - struct commit *commit = prio_queue_get(&queue->pq); - - if (commit == queue->max_nonstale) - queue->max_nonstale = NULL; - return commit; + /* Remove from old bucket */ + if (!(old_paint & STALE)) { + switch (old_paint & (PARENT1 | PARENT2)) { + case PARENT1: queue->p1_count--; break; + case PARENT2: queue->p2_count--; break; + case PARENT1 | PARENT2: queue->pending_merge_bases--; break; + } + } + /* Add to new bucket */ + if (!(new_paint & STALE)) { + switch (new_paint & (PARENT1 | PARENT2)) { + case PARENT1: queue->p1_count++; break; + case PARENT2: queue->p2_count++; break; + case PARENT1 | PARENT2: queue->pending_merge_bases++; break; + } + } } -static void clear_nonstale_queue(struct nonstale_queue *queue) +/* + * Add flags to a commit and update the queue and counters. If the + * commit is already in the queue, only the counters are adjusted + * for the flag change. Otherwise the commit is enqueued. + */ +static void paint_queue_put(struct paint_queue *queue, + struct commit *c, unsigned add_flags) { - clear_prio_queue(&queue->pq); - queue->max_nonstale = NULL; -} + unsigned old_flags = c->object.flags; + c->object.flags |= add_flags; -static void nonstale_queue_put_dedup(struct nonstale_queue *queue, - struct commit *c) -{ - if (c->object.flags & ENQUEUED) - return; - c->object.flags |= ENQUEUED; - nonstale_queue_put(queue, c); + if (old_flags & ENQUEUED) { + paint_count_transition(queue, old_flags, c->object.flags); + } else { + c->object.flags |= ENQUEUED; + prio_queue_put(&queue->pq, c); + paint_count_transition(queue, 0, c->object.flags); + } } -static struct commit *nonstale_queue_get_dedup(struct nonstale_queue *queue) +static struct commit *paint_queue_get(struct paint_queue *queue) { - struct commit *commit = nonstale_queue_get(queue); + struct commit *commit = prio_queue_get(&queue->pq); - if (commit) + if (commit) { commit->object.flags &= ~ENQUEUED; + paint_count_transition(queue, commit->object.flags, 0); + } return commit; } @@ -104,8 +127,8 @@ static int paint_down_to_common(struct repository *r, enum merge_base_flags mb_flags, struct commit_list **result) { - struct nonstale_queue queue = { - { compare_commits_by_gen_then_commit_date } + struct paint_queue queue = { + .pq = { compare_commits_by_gen_then_commit_date } }; int i; timestamp_t last_gen = GENERATION_NUMBER_INFINITY; @@ -119,15 +142,14 @@ static int paint_down_to_common(struct repository *r, commit_list_append(one, result); return 0; } - nonstale_queue_put_dedup(&queue, one); + paint_queue_put(&queue, one, 0); - for (i = 0; i < n; i++) { - twos[i]->object.flags |= PARENT2; - nonstale_queue_put_dedup(&queue, twos[i]); - } + for (i = 0; i < n; i++) + paint_queue_put(&queue, twos[i], PARENT2); - while (queue.max_nonstale) { - struct commit *commit = nonstale_queue_get_dedup(&queue); + while (queue.p1_count + queue.p2_count + + queue.pending_merge_bases > 0) { + struct commit *commit = paint_queue_get(&queue); struct commit_list *parents; int flags; timestamp_t generation = commit_graph_generation(commit); @@ -165,7 +187,7 @@ static int paint_down_to_common(struct repository *r, if ((p->object.flags & flags) == flags) continue; if (repo_parse_commit(r, p)) { - clear_nonstale_queue(&queue); + clear_prio_queue(&queue.pq); commit_list_free(*result); *result = NULL; /* @@ -180,12 +202,11 @@ static int paint_down_to_common(struct repository *r, return error(_("could not parse commit %s"), oid_to_hex(&p->object.oid)); } - p->object.flags |= flags; - nonstale_queue_put_dedup(&queue, p); + paint_queue_put(&queue, p, flags); } } - clear_nonstale_queue(&queue); + clear_prio_queue(&queue.pq); commit_list_sort_by_date(result); return 0; } @@ -1089,12 +1110,18 @@ struct commit_list *get_reachable_subset(struct commit **from, size_t nr_from, define_commit_slab(bit_arrays, struct bitmap *); static struct bit_arrays bit_arrays; -static void insert_no_dup(struct nonstale_queue *queue, struct commit *c) +static void insert_no_dup(struct prio_queue *queue, + struct commit **max_nonstale, + struct commit *c) { if (c->object.flags & PARENT2) return; - nonstale_queue_put(queue, c); + prio_queue_put(queue, c); c->object.flags |= PARENT2; + if (!(c->object.flags & STALE) && + (!*max_nonstale || + queue->compare(*max_nonstale, c, queue->cb_data) <= 0)) + *max_nonstale = c; } static struct bitmap *get_bit_array(struct commit *c, int width) @@ -1118,9 +1145,10 @@ void ahead_behind(struct repository *r, struct commit **commits, size_t commits_nr, struct ahead_behind_count *counts, size_t counts_nr) { - struct nonstale_queue queue = { - { .compare = compare_commits_by_gen_then_commit_date } + struct prio_queue queue = { + .compare = compare_commits_by_gen_then_commit_date }; + struct commit *max_nonstale = NULL; size_t width = DIV_ROUND_UP(commits_nr, BITS_IN_EWORD); if (!commits_nr || !counts_nr) @@ -1140,14 +1168,17 @@ void ahead_behind(struct repository *r, struct bitmap *bitmap = get_bit_array(c, width); bitmap_set(bitmap, i); - insert_no_dup(&queue, c); + insert_no_dup(&queue, &max_nonstale, c); } - while (queue.max_nonstale) { - struct commit *c = nonstale_queue_get(&queue); + while (max_nonstale) { + struct commit *c = prio_queue_get(&queue); struct commit_list *p; struct bitmap *bitmap_c = get_bit_array(c, width); + if (c == max_nonstale) + max_nonstale = NULL; + for (size_t i = 0; i < counts_nr; i++) { int reach_from_tip = !!bitmap_get(bitmap_c, counts[i].tip_index); int reach_from_base = !!bitmap_get(bitmap_c, counts[i].base_index); @@ -1178,7 +1209,7 @@ void ahead_behind(struct repository *r, if (bitmap_popcount(bitmap_p) == commits_nr) p->item->object.flags |= STALE; - insert_no_dup(&queue, p->item); + insert_no_dup(&queue, &max_nonstale, p->item); } free_bit_array(c); @@ -1186,10 +1217,10 @@ void ahead_behind(struct repository *r, /* STALE is used here, PARENT2 is used by insert_no_dup(). */ repo_clear_commit_marks(r, PARENT2 | STALE); - for (size_t i = 0; i < queue.pq.nr; i++) - free_bit_array(queue.pq.array[i].data); + for (size_t i = 0; i < queue.nr; i++) + free_bit_array(queue.array[i].data); clear_bit_arrays(&bit_arrays); - clear_nonstale_queue(&queue); + clear_prio_queue(&queue); } struct commit_and_index { From f4e224e5c99ce0cbec93db40714d15e129d45294 Mon Sep 17 00:00:00 2001 From: Kristofer Karlsson Date: Fri, 12 Jun 2026 14:57:51 +0200 Subject: [PATCH 2/5] commit-reach: terminate merge-base walk when one paint side is exhausted Add an early termination check to paint_down_to_common() using the per-side counters introduced in the previous commit. Once the walk enters the finite-generation region (topological order guaranteed by commit-graph), terminate early when one side's exclusive count drops to zero -- no new merge-base can form without both paint sides meeting. The check also waits for pending_merge_bases to reach zero, ensuring all merge-base candidates have been popped and recorded before exiting. This is necessary for FIND_ALL to return all merge bases in criss-cross merge topologies. The INFINITY gate ensures correctness: commits without a commit-graph entry have GENERATION_NUMBER_INFINITY and are ordered by commit date, which is not topologically reliable. The optimization only fires once the walk enters the finite-generation region where ordering guarantees hold. On large repositories with commit-graph, this yields 100-1000x speedups for merge-base queries where one side (e.g. a PR branch) is much smaller than the other. Signed-off-by: Kristofer Karlsson --- commit-reach.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/commit-reach.c b/commit-reach.c index 8f107f24a590e8..ff58289c5b2bfc 100644 --- a/commit-reach.c +++ b/commit-reach.c @@ -204,6 +204,20 @@ static int paint_down_to_common(struct repository *r, } paint_queue_put(&queue, p, flags); } + + /* + * Side exhaustion: a new merge-base can only form + * when both PARENT1-only and PARENT2-only commits + * remain in the queue. In the finite-generation + * region the queue is ordered topologically, so + * once one side drains it cannot reappear. Also + * wait for all pending merge-base candidates to be + * popped so FIND_ALL records them. + */ + if (generation < GENERATION_NUMBER_INFINITY && + queue.pending_merge_bases == 0 && + (queue.p1_count == 0 || queue.p2_count == 0)) + break; } clear_prio_queue(&queue.pq); From 35659df94031016ec276c5536204bd242a47bb2e Mon Sep 17 00:00:00 2001 From: Kristofer Karlsson Date: Fri, 12 Jun 2026 15:33:49 +0200 Subject: [PATCH 3/5] t6099: test merge-base with ancestor among candidates Add tests for the case where multiple merge-base candidates exist and one is an ancestor of another. This exercises the side-exhaustion optimization in paint_down_to_common together with the remove_redundant safety net in get_merge_bases_many_0. Test with and without commit-graph to verify the optimization is a no-op when generation numbers are unavailable. Signed-off-by: Kristofer Karlsson --- t/t6099-merge-base-side-exhaustion.sh | 82 +++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 t/t6099-merge-base-side-exhaustion.sh diff --git a/t/t6099-merge-base-side-exhaustion.sh b/t/t6099-merge-base-side-exhaustion.sh new file mode 100755 index 00000000000000..bae3ea7f83be87 --- /dev/null +++ b/t/t6099-merge-base-side-exhaustion.sh @@ -0,0 +1,82 @@ +#!/bin/sh + +test_description='merge-base with ancestor among merge-base candidates + +Test that merge-base --all correctly handles cases where +multiple merge-base candidates exist and one is an ancestor +of another. The side-exhaustion optimization in +paint_down_to_common may exit before STALE propagation +removes the ancestor, but remove_redundant catches it. + +Graph shape (parents are below children): + + A ----------- X + |\ /| + | B---------/ | + | | | + e2 \ f2 + | | | + e1 d1 f1 + \ | / + \ | / + \| / + C + +A and X are the two tips. +B and C are both reachable from A and X. +B reaches C through d1. +Only B should appear in merge-base --all output. +' + +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME + +TEST_PASSES_SANITIZE_LEAK=true +. ./test-lib.sh + +test_expect_success 'setup ancestor merge-base candidate' ' + test_commit C && + + git checkout -b d-chain HEAD && + test_commit d1 && + test_commit B && + + git checkout -b e-path C && + test_commit e1 && + test_commit e2 && + + git checkout -b f-path C && + test_commit f1 && + test_commit f2 && + + git checkout -b branch-A e-path && + test_merge A B && + + git checkout -b branch-X f-path && + test_merge X B && + + git commit-graph write --reachable +' + +test_expect_success 'merge-base --all excludes ancestor candidate' ' + git rev-parse B >expected && + git merge-base --all A X >actual && + test_cmp expected actual +' + +test_expect_success 'merge-base (single) finds shallowest' ' + git rev-parse B >expected && + git merge-base A X >actual && + test_cmp expected actual +' + +# Without commit-graph: generation numbers are INFINITY, +# side-exhaustion optimization does not fire. +test_expect_success 'merge-base --all without commit-graph' ' + rm -f .git/objects/info/commit-graph && + git rev-parse B >expected && + git merge-base --all A X >actual && + test_cmp expected actual +' + +test_done From 4f32e193616e1f4687d3f0facf472054958182d4 Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Sun, 14 Jun 2026 13:03:32 +0200 Subject: [PATCH 4/5] t6600: add test cases for side-exhaustion edge cases Add test cases to t6600-test-reach.sh that exercise edge cases in the side-exhaustion optimization for paint_down_to_common(): - in_merge_bases_many:self: commit is both A and one of the X inputs - get_merge_bases_many:duplicate-twos: duplicate entries in X list - get_merge_bases_many:pending-stale: STALE transition on an already-painted commit (ps-* diamond topology) - get_merge_bases_many:infinity-both-sides: both tips outside the commit-graph with non-monotonic dates (pi-* topology) Signed-off-by: Elijah Newren --- t/t6600-test-reach.sh | 114 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/t/t6600-test-reach.sh b/t/t6600-test-reach.sh index b5b314e57068f9..bdfa586544faa1 100755 --- a/t/t6600-test-reach.sh +++ b/t/t6600-test-reach.sh @@ -49,6 +49,65 @@ test_expect_success 'setup' ' git tag -a -m "$x-$i" tag-$x-$i commit-$x-$i || return 1 done done && + + # Build a small side topology to exercise the (PARENT1|PARENT2) -> + # (PARENT1|PARENT2|STALE) transition in paint_down_to_common(); the + # 10x10 grid above does not exercise it because no merge-base candidate + # there is a descendant of another, so STALE never reaches a + # still-pending candidate. + # + # ps-X + # /|\ + # / | \ + # ps-Z ps-B ps-W + # | / \ | + # | / \ | + # |/ \| + # ps-T1 ps-T2 + # + # where ps-T1=merge(ps-Z,ps-B), ps-T2=merge(ps-W,ps-B), so + # merge-base(ps-T1,ps-T2) = ps-B. During the walk, ps-X transitions + # to (PARENT1|PARENT2) via ps-Z and ps-W before ps-B is dequeued; + # then the STALE-walk from ps-B transitions ps-X to + # (PARENT1|PARENT2|STALE). + git checkout --orphan ps-orphan && + test_commit ps-X && + git checkout -b ps-B-br ps-X && test_commit ps-B && + git checkout -b ps-Z-br ps-X && test_commit ps-Z && + git checkout -b ps-W-br ps-X && test_commit ps-W && + git checkout -b ps-T1 ps-Z && + git merge --no-ff -m ps-T1 ps-B && + git checkout -b ps-T2 ps-W && + git merge --no-ff -m ps-T2 ps-B && + + # Build a side topology that lives entirely outside the half + # commit-graph and has non-monotonic commit dates, to exercise the + # push-time INFINITY-gate in paint_down_to_common. With both tips + # outside the graph, their generation is INFINITY and the priority + # queue falls back to commit-date order, which here is non-monotonic + # between parent and child. If the optimization were not disabled, + # a cross-side flag transition on an already-dequeued INFINITY parent + # would underflow the exclusive counts. + # + # pi-X (date 500, PARENT1 tip) --> pi-P, pi-D + # pi-D (date 480) --> pi-C + # pi-C (date 200) --> pi-B + # pi-B (date 100, PARENT2 tip) --> pi-P + # pi-P (date 450, root) + # + # merge-base(pi-X, pi-B) = pi-B (it is an ancestor of pi-X and is + # itself one of the queried tips). + git checkout --orphan pi-orphan && + test_commit --date "@450 +0000" pi-P && + test_commit --date "@100 +0000" pi-B && + test_commit --date "@200 +0000" pi-C && + test_commit --date "@480 +0000" pi-D && + GIT_AUTHOR_DATE="@500 +0000" GIT_COMMITTER_DATE="@500 +0000" \ + git commit-tree -p pi-D -p pi-P -m pi-X pi-D^{tree} >pi-X-oid && + pi_x="$(cat pi-X-oid)" && + git branch -f pi-X-br "$pi_x" && + git tag pi-X "$pi_x" && + git commit-graph write --reachable && mv .git/objects/info/commit-graph commit-graph-full && chmod u+w commit-graph-full && @@ -146,6 +205,16 @@ test_expect_success 'in_merge_bases_many:miss-heuristic' ' test_all_modes in_merge_bases_many ' +test_expect_success 'in_merge_bases_many:self' ' + cat >input <<-\EOF && + A:commit-6-8 + X:commit-5-9 + X:commit-6-8 + EOF + echo "in_merge_bases_many(A,X):1" >expect && + test_all_modes in_merge_bases_many +' + test_expect_success 'is_descendant_of:hit' ' cat >input <<-\EOF && A:commit-5-7 @@ -183,6 +252,51 @@ test_expect_success 'get_merge_bases_many' ' test_all_modes get_merge_bases_many ' +test_expect_success 'get_merge_bases_many:duplicate-twos' ' + cat >input <<-\EOF && + A:commit-5-7 + X:commit-4-8 + X:commit-4-8 + X:commit-6-6 + X:commit-6-6 + X:commit-8-3 + EOF + { + echo "get_merge_bases_many(A,X):" && + git rev-parse commit-5-6 \ + commit-4-7 | sort + } >expect && + test_all_modes get_merge_bases_many +' + +test_expect_success 'get_merge_bases_many:pending-stale' ' + # Exercises the (PARENT1|PARENT2) -> (...|STALE) transition path in + # paint_down_to_common(). See the topology comment in the setup test. + cat >input <<-\EOF && + A:ps-T1 + X:ps-T2 + EOF + { + echo "get_merge_bases_many(A,X):" && + git rev-parse ps-B + } >expect && + test_all_modes get_merge_bases_many +' + +test_expect_success 'get_merge_bases_many:infinity-both-sides' ' + # Exercises the push-time INFINITY-gate in paint_down_to_common(). See + # the pi-* topology comment in the setup test. + cat >input <<-\EOF && + A:pi-X + X:pi-B + EOF + { + echo "get_merge_bases_many(A,X):" && + git rev-parse pi-B + } >expect && + test_all_modes get_merge_bases_many +' + test_expect_success 'reduce_heads' ' cat >input <<-\EOF && X:commit-1-10 From bf8f5258ef506a74b663bc3b11ab7fb620250ae4 Mon Sep 17 00:00:00 2001 From: Kristofer Karlsson Date: Sun, 14 Jun 2026 13:03:56 +0200 Subject: [PATCH 5/5] p6012: add perf test for merge-base with deep side branch Add a perf test that benchmarks merge-base on a synthetic 500k-commit repository with a deep side branch. The test creates a main branch, a side branch forked from an early commit with old timestamps, a merge commit bringing the side branch in, and a PR branch just before the merge. Computing merge-base(main, pr) exercises the side-exhaustion optimization. Includes variants with and without a commit-graph to cover the case where both tips are outside the commit-graph (INFINITY region). Signed-off-by: Kristofer Karlsson --- t/perf/p6012-merge-base-deep-side-branch.sh | 187 ++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100755 t/perf/p6012-merge-base-deep-side-branch.sh diff --git a/t/perf/p6012-merge-base-deep-side-branch.sh b/t/perf/p6012-merge-base-deep-side-branch.sh new file mode 100755 index 00000000000000..c3671a6ecadfaa --- /dev/null +++ b/t/perf/p6012-merge-base-deep-side-branch.sh @@ -0,0 +1,187 @@ +#!/bin/sh + +test_description='Performance of merge-base with a deep side branch + +Synthetic repository that triggers slow merge-base when a merge commit +introduces a side branch rooted far back in history. The pathological +case forces paint_down_to_common() to chase STALE flags through many +intermediate commits after the merge-base is already found. + +Topology (N = $NUM_COMMITS commits on main before the merge, F is +the BRANCH_POINT-th of them, S = $SIDE_COMMITS commits on the side): + +# F -- s1 -- ... -- sS (side, dates set old) +# / \ +# 1 -- 2 -- ... -- N-1 -- N -- M (main) +# | +# P (pr branch) + +merge-base(M, P) = N. Without early termination, the algorithm +walks from N back through roughly N - BRANCH_POINT commits to F +to exhaust STALE processing. +' + +. ./perf-lib.sh + +NUM_COMMITS=500000 +BRANCH_POINT=10000 +SIDE_COMMITS=5 +EXTRA_COMMITS=10 + +test_expect_success 'setup synthetic repo' ' + git init --bare repo.git && + awk -v N=$NUM_COMMITS -v BP=$BRANCH_POINT -v S=$SIDE_COMMITS \ + -v E=$EXTRA_COMMITS '\'' + BEGIN { + # Shared blob + print "blob" + print "mark :1" + print "data 8" + print "content" + print "" + + # Main branch commits: mark :2 is the 1st main + # commit, mark :(N+1) is the Nth. + for (i = 2; i <= N + 1; i++) { + print "commit refs/heads/main" + print "mark :" i + print "committer C " (1000000 + i) " +0000" + print "data 2" + print "x" + if (i > 2) + print "from :" (i - 1) + print "M 100644 :1 file" + print "" + } + + # Side branch forks from the BP-th main commit + # (mark :(BP+1)), with old dates. + side_start = BP + 1 + side_mark_base = N + 2 + for (j = 0; j < S; j++) { + mark = side_mark_base + j + if (j == 0) + from_mark = side_start + else + from_mark = mark - 1 + print "commit refs/heads/side" + print "mark :" mark + print "committer C " (500000 + j) " +0000" + print "data 2" + print "x" + print "from :" from_mark + print "" + } + + # Merge side into main + main_tip = N + 1 + side_tip = side_mark_base + S - 1 + merge_mark = side_tip + 1 + print "commit refs/heads/main" + print "mark :" merge_mark + print "committer C " (1000000 + N + 2) " +0000" + print "data 2" + print "x" + print "from :" main_tip + print "merge :" side_tip + print "" + + # PR branch forked from main tip (just before merge) + pr_mark = merge_mark + 1 + print "commit refs/heads/pr" + print "mark :" pr_mark + print "committer C " (1000000 + N + 3) " +0000" + print "data 2" + print "x" + print "from :" main_tip + print "" + + # Save refs before extra commits for the + # commit-graph boundary. + print "reset refs/markers/graph-boundary-main" + print "from :" merge_mark + print "" + print "reset refs/markers/graph-boundary-pr" + print "from :" pr_mark + print "" + + # Extra commits on main (beyond commit-graph). + next_mark = pr_mark + 1 + from_mark = merge_mark + for (k = 0; k < E; k++) { + print "commit refs/heads/main" + print "mark :" next_mark + print "committer C " (2000000 + k) " +0000" + print "data 2" + print "x" + print "from :" from_mark + print "" + from_mark = next_mark + next_mark++ + } + + # Extra commits on pr (beyond commit-graph). + from_mark = pr_mark + for (k = 0; k < E; k++) { + print "commit refs/heads/pr" + print "mark :" next_mark + print "committer C " (2000000 + k) " +0000" + print "data 2" + print "x" + print "from :" from_mark + print "" + from_mark = next_mark + next_mark++ + } + } + '\'' expect +' + +test_expect_success 'merge-base result is correct' ' + git -C repo.git merge-base --all main pr >actual && + test_cmp expect actual +' + +test_perf 'merge-base: both tips in INFINITY region' ' + git -C repo.git merge-base --all main pr +' + +# Rewrite commit-graph to cover all commits, so both tips are in +# the finite region. +test_expect_success 'setup: full commit-graph' ' + git -C repo.git commit-graph write --reachable +' + +test_perf 'merge-base: both tips in finite region' ' + git -C repo.git merge-base --all main pr +' + +test_perf 'merge-base: no side branch (baseline)' ' + git -C repo.git merge-base --all main~1~$EXTRA_COMMITS pr +' + +# Remove commit-graph entirely: all 500k commits are INFINITY. +test_expect_success 'setup: remove commit-graph' ' + rm -f repo.git/objects/info/commit-graph && + rm -rf repo.git/objects/info/commit-graphs +' + +test_perf 'merge-base: all INFINITY (no commit-graph)' ' + git -C repo.git merge-base --all main pr +' + +test_expect_success 'merge-base result correct without commit-graph' ' + git -C repo.git merge-base --all main pr >actual && + test_cmp expect actual +' + +test_done