Skip to content

commit-reach: fix !FIND_ALL early exit with v1 commit graph#2162

Open
spkrka wants to merge 2 commits into
gitgitgadget:masterfrom
spkrka:find-all-fix
Open

commit-reach: fix !FIND_ALL early exit with v1 commit graph#2162
spkrka wants to merge 2 commits into
gitgitgadget:masterfrom
spkrka:find-all-fix

Conversation

@spkrka

@spkrka spkrka commented Jun 29, 2026

Copy link
Copy Markdown

Fixes a bug introduced by 93e5b16 (commit-reach: early exit
paint_down_to_common for single merge-base, 2025-04-10) where
git merge-base can return the wrong result.

The bug requires all of the following to trigger:

  1. A v1 commit graph (topological levels only, no corrected commit
    dates). Generation v2 with corrected commit dates has been the
    default since 2021, so only repos that have not rewritten their
    commit graph in over four years would be affected.
  2. git merge-base without --all (the common case, but --all
    is unaffected because it disables the early exit).
  3. A topology with clock skew: the correct merge base has a lower
    committer date than one of its ancestors that is also a common
    ancestor. With date ordering, the deeper ancestor pops first
    and the early exit fires before the correct result is found.

This two-patch series:

  1. Adds a test demonstrating the bug (clock-skew topology where the
    correct merge base has a lower date than its ancestor)
  2. Fixes it by tracking whether the queue is generation-ordered and
    gating the early exit on that flag

cc: Junio C Hamano gitster@pobox.com
cc: Derrick Stolee stolee@gmail.com
cc: Kristofer Karlsson krka@spotify.com

spkrka added 2 commits June 29, 2026 14:29
Add a topology where the correct merge base (M2) has a lower
committer date than its ancestor (M1) due to clock skew.  With a
v1 commit graph (topological levels only, no corrected commit
dates), paint_down_to_common() falls back to commit-date ordering.
In that mode, M1 pops before M2, acquires both paint sides, and
the !FIND_ALL early exit fires -- returning the wrong merge base.

Mark the test as test_expect_failure to document the bug; the next
commit will fix it.

Signed-off-by: Kristofer Karlsson <krka@spotify.com>
When paint_down_to_common() falls back to commit-date ordering (for
v1 commit graphs without corrected commit dates), the !FIND_ALL early
exit incorrectly fires.  The exit assumes the queue is generation-
ordered, so the first RESULT commit found must be the shallowest.
With date ordering this is not guaranteed: a closer merge base with
a lower committer date (clock skew) may still be in the queue behind
deeper commits.

Add a gen_ordered flag that is cleared when the date fallback fires,
and require it for the early exit.

Update the test from the previous commit to test_expect_success.

Signed-off-by: Kristofer Karlsson <krka@spotify.com>
@spkrka spkrka marked this pull request as ready for review June 29, 2026 12:39
@spkrka

spkrka commented Jun 29, 2026

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Jun 29, 2026

Copy link
Copy Markdown

Submitted as pull.2162.git.1782739162.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2162/spkrka/find-all-fix-v1

To fetch this version to local tag pr-2162/spkrka/find-all-fix-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2162/spkrka/find-all-fix-v1

@gitgitgadget

gitgitgadget Bot commented Jul 1, 2026

Copy link
Copy Markdown

This branch is now known as kk/commit-reach-find-all-fix.

@gitgitgadget

gitgitgadget Bot commented Jul 1, 2026

Copy link
Copy Markdown

This patch series was integrated into seen via git@a560473.

@gitgitgadget gitgitgadget Bot added the seen label Jul 1, 2026
@gitgitgadget

gitgitgadget Bot commented Jul 6, 2026

Copy link
Copy Markdown

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"Kristofer Karlsson via GitGitGadget" <gitgitgadget@gmail.com>
writes:

> Fixes a bug introduced by 93e5b1680e (commit-reach: early exit
> paint_down_to_common for single merge-base, 2025-04-10) where git merge-base
> can return the wrong result.
>
> The bug requires all of the following to trigger:
>
>  1. A v1 commit graph (topological levels only, no corrected commit dates).
>     Generation v2 with corrected commit dates has been the default since
>     2021, so only repos that have not rewritten their commit graph in over
>     four years would be affected.
>  2. git merge-base without --all (the common case, but --all is unaffected
>     because it disables the early exit).
>  3. A topology with clock skew: the correct merge base has a lower committer
>     date than one of its ancestors that is also a common ancestor. With date
>     ordering, the deeper ancestor pops first and the early exit fires before
>     the correct result is found.
>
> This two-patch series:
>
>  1. Adds a test demonstrating the bug (clock-skew topology where the correct
>     merge base has a lower date than its ancestor)
>  2. Fixes it by tracking whether the queue is generation-ordered and gating
>     the early exit on that flag

Where should this new "gen_ordered" flag go in the world with
kk/merge-base-exhaustion topic merged in?  Does it also belong
to the paint_state struct or can it be on-stack independent variable
to the function?

@gitgitgadget

gitgitgadget Bot commented Jul 6, 2026

Copy link
Copy Markdown

Kristofer Karlsson wrote on the Git mailing list (how to reply to this email):

On Mon, 29 Jun 2026 at 19:50, Junio C Hamano <gitster@pobox.com> wrote:
>
> Where should this new "gen_ordered" flag go in the world with
> kk/merge-base-exhaustion topic merged in?  Does it also belong
> to the paint_state struct or can it be on-stack independent variable
> to the function?

I don't think kk/merge-base-exhaustion is ready to be merged as-is
and you are right that these two topics would conflict.
I will need to reroll v5 and exactly how/when I do that
depends on what we do here.

I wanted to quickly share this small patch as a bugfix
since the related code is already merged. I think the bug
itself might be a very unlikely edge case but I can't really be
sure.

My preference would be to merge this as-is, and then I can rework
v5 of kk/merge-base-exhaustion on top of it later - it will
add some delay but I don't want to rush it since it's a non-trivial
change.

The other option is to drop this topic if the risk is deemed low
enough, and then I will rework v5 to either apply a similar
gen_ordered flag or eliminate that different ordering entirely -
I see that as a good long-term goal that simplifies the code while
retaining or improving the performance.

I realize the timing is terrible, I wish I had spotted this a week
ago instead of right at the 2.55 finalization period.

Thanks,
Kristofer

@gitgitgadget

gitgitgadget Bot commented Jul 6, 2026

Copy link
Copy Markdown

User Kristofer Karlsson <krka@spotify.com> has been added to the cc: list.

@gitgitgadget

gitgitgadget Bot commented Jul 6, 2026

Copy link
Copy Markdown

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

Kristofer Karlsson <krka@spotify.com> writes:

> My preference would be to merge this as-is, and then I can rework
> v5 of kk/merge-base-exhaustion on top of it later - it will
> add some delay but I don't want to rush it since it's a non-trivial
> change.

That sounds very sensible.  Let's do that.

I've taken a look at both of these two patches and found the
solution quite sensible, but I'd prefer a second set of eyes
to confirm.

Thanks.

@gitgitgadget

gitgitgadget Bot commented Jul 6, 2026

Copy link
Copy Markdown

There was a status update in the "New Topics" section about the branch kk/commit-reach-find-all-fix on the Git mailing list:

The early-exit optimization in paint_down_to_common() has been gated
on the queue being generation-ordered. This fixes a bug where git
merge-base (without --all) could return incorrect results on
repositories with v1 commit graphs and clock skew.

Comments?
cf. <xmqqa4sdw55v.fsf@gitster.g>
source: <pull.2162.git.1782739162.gitgitgadget@gmail.com>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant