Subject: Re: Feature request: Exponential search in git bisect

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Tue, 27 Oct 2020 13:10:03 +0100

On Fri, Oct 09 2020, Manuel Bärenz wrote:

> This feature was requested 8 years ago and briefly discussed:
> https://public-inbox.org/git/[email-masked]/
>
>
>   TL;DR
>
> Before doing git bisect, I want to use exponential search to
> automatically find a good commit, in logarithmic time.
>
>
>   Scenario
>
>   * I have a bug in HEAD.
>   * I strongly suspect that it was introduced some time ago, but I don't
>     know when exactly.
>   * I have an automated test that will find the bug if the test can run
>     properly.
>   * Most of the commits in the repository are not testable, i.e. the
>     test doesn't run properly. (E.g. because the feature it tests wasn't
>     introduced yet, refactoring, etc.)
>   * I have no idea what a good commit might be, because I don't know
>     when the first /testable/ good commit is.
>
> This sounds like a standard application for git bisect. No matter how
> big the repo, with binary search we would expect to find the first bad
> commit in logarithmic time.
>
>
>   Failed attempt
>
> The zeroth idea might be trying to find the good commit by hand, by
> reading changelogs, by trying some commits, whatever. In some
> situations, this is not feasible. In fact, such situations occur
> frequently for me, for example for undocumented features, unversioned
> rolling releases, incidental complexity leading to older commits not
> being testable, etc.
>
> The first idea that comes to mind - and it was recommended 8 years agos,
> and I've tried it a few times already - is to simply mark the root
> commit as good. (Now, there might be several roots, but that's a puzzle
> you typically only have to figure out once per repo.) This sounds great
> in theory because binary search should get through the good old commits
> in logarithmic time.
>
> The problem with this approach is that if most older commits are
> untestable, I have to git bisect skip them. This basically kills the
> logarithmic performance, because bisect skip doesn't do binary search,
> but something rather random. Just yesterday I killed a bisect search
> that took hours because it kept skipping and didn't find actual good
> commits.
>
> You might say that instead of skipping old commits, one should mark them
> as good. That's problematic because then I might accidentally mark a
> commit as good that was already untestable bad. Given that bisect has no
> undo functionality, that can quickly mess up my search. Distinguishing
> untestable good from untestable bad is really hard automatically. I
> shouldn't have to do that.
>
> Long story short: Going from the root commit typically isn't feasible.
> I've tried it.
>
>
>   Proposal: Exponential search
>
> Instead of going from the root commit, what I do manually before
> starting git bisect is this:
>
> git checkout HEAD~10
> ./test.sh # Says: "Bug is present"
> git checkout HEAD~20
> ./test.sh # Says: "Bug is still present"
> git checkout HEAD~40
> ./test.sh # Says: "Bug is still present"
> [...] # Proceed exponentially
> git checkout HEAD~640
> ./test.sh # Says: "Bug is GONE!"
> git bisect good
>
> This technique is known as
> https://en.wikipedia.org/wiki/Exponential_search, and it works very well
> in practice. I find a good commit long before I enter the "untestable
> good" region. But it's tedious to do this manually. In this example, I
> needed to run the script 8 times manually, but of course it can be more
> often, and compiling and running the test may take time. This is ok for
> a one-off search, but it's tedious for regular usages.
>
> Yes, I could wrap this up in a shell script, but I guess there are
> caveats that I didn't think of when the history isn't linear. Maybe
> someone even already has, and I'm unaware of that. But it feels like
> this could be a proper git bisect feature, and a very useful one.

Let's suppose we have a repository with 700 linear commits:
    
    set -e
    
    cd /tmp
    rm -rf /tmp/test-repo
    mkdir /tmp/test-repo
    cd /tmp/test-repo
    git init
    
    for i in $(seq 1 700)
    do
        touch $i
        git add $i
        git commit -m"add $i"
    done

Then let's bisect it from the root:
    
    git bisect start HEAD HEAD~699

And let's further suppose that the feature wasn't introduced until
commit 650, and it's broken since 653.

With the bisect method of finding this we're going to start our session
with these commits:

    [bef4b96adf5f082b1103c170eb6a41d1526fa919] add 350 => good
    [d271fdb29129dbba723d3eac1035b58b6dc6f583] add 525 => good
    [b0c9b7da646334a6c7eadb333b5ae77ec35388b3] add 612 => good
    [2a78858d04cc5542e176dd8f68fa2660b8b48ab3] add 656 => bad (or skip)

Whereas with this proposed exponential search it'll be commits #:
    
    2
    4
    8
    16
    32
    64
    128
    256
    512

So we're going to test 8 commits before we get past the mid-point that
bisect now starts with. Of course that may be more efficient, but if the
regression is in some random place I don't see why we wouldn't test the
middle instead of weighing things towards the start of the history. If
the relevant commit is an early one like #50 bisect is still faster.

With the disclaimer that I may be missing something, I'm just plowing
ahead:

I don't see the usefulness of this proposed exponential search, but I
definitely *do* see the usefulness of a "bisect undo", and I've been
bitten many times by the lack of that myself. We should definitely have
that.

And as Christian pointed out in [1] it seems you're (understandably, it
can be confusing) conflating skip and "good" in your example. So to
re-state the problem you have, if I were to use "skip" in the above
example bisect for me will do:
    
    [bef4b96adf5f082b1103c170eb6a41d1526fa919] add 350 => skip
    [15c181442dcbd785bf2b28cfddcf1932aaa71c42] add 351 => skip
    [c985feffa2c92b2d3d9804a43b215e26c7275549] add 374 => skip
    [effa691ec5dc2d80c0b070c4d8ac9fa70cbfea9f] add 168 => skip
    [212a5ee3ff55834196661d0632f584098e9f6fb2] add 369 => skip
    [2ca67a4500c9f3cd489b9e529d41eb99252dc8f6] add 179 => skip
    [4067ee067e2298e1b104f4ff9aab15f4e815e101] add 337 => skip
    [...]

If only there was a way to be on 350 and say "skip everything up to this
point", so I implemented it!:

    [bef4b96adf5f082b1103c170eb6a41d1526fa919] add 350 => skip-to-here
    [15c181442dcbd785bf2b28cfddcf1932aaa71c42] add 351 => skip
    [6f7b5ca1dcc21c28af658a172136e503d7a2d0ea] add 420
    [...]

etc. now we're not jumping around in 1..350:

    $ git config alias.bisect-skip-to-here
    !f() { good=$(git for-each-ref refs/bisect/good* --format="%(objectname)"); git bisect skip $good..HEAD; }; f

Great eh? Not really, because I've just discovered a really tedious and
expensive (I created 350 "skip" refs) of re-inventing what I could do if
I just did "good" on commit 350[2] :)

I started looking at how to implement "git bisect undo", it should be
possible by either winding .git/refs/bisect based on the BISECT_LOG, or
simply keeping copies of .git/refs/bisect (and adjusting HEAD), but then
I wondered if this was something we could get for free with the ref
transactions in the reftable. CC'd Han-Wen in case he's got feedback,
maybe it's trivial, or maybe I'm imagining things.

1. https://lore.kernel.org/git/[email-masked]/
2. Not exactly, because in skip-to-here on 350 we'll next test 351, but
   "good" will test 525, but as argued earlier that's a feature.