Sub topic 6
Checking the script on the good and bad endpoints first
This chapter assumes you know basic Git (commits, tags, checkout), can read a short shell script, and can run your project's build and tests from the command line. You should also know the exit code contract from the previous chapter: 0 means good, 1 to 127 (except 125) means bad, 125 means skip, and 128 or higher aborts the search.
Why validate before you search
git bisect run trusts your script completely. If the script is wrong, Git still finishes and prints a "first bad commit". That commit will be wrong, and nothing in the output will tell you so.
Two mistakes are common:
- The script returns 0 on the bad commit. It fails to detect the bug, so every commit looks good and the search drifts toward the wrong end of the range.
- The script returns non-zero on the good commit. A broken build, a missing tool, or a wrong path makes every commit look bad, and Git blames the oldest commit in the range.
Running the script by hand on the two endpoints takes seconds. A wrong result from a full search costs far more.
The check
The endpoints are the commits you will give to git bisect start. Here the good one is the tag v1.4.0 and the bad one is main. Check out each one, run the script, and print its exit status.
git checkout v1.4.0 && ./check.sh; echo "good endpoint exit: $?"
git checkout main && ./check.sh; echo "bad endpoint exit: $?"Read the output like this:
| Endpoint | Expected exit | If you see something else |
|---|---|---|
| Good (`v1.4.0`) | 0 | The script has a bug, or the environment is wrong. Fix it first. |
| Bad (`main`) | 1 to 127, but not 125 | The script does not detect the regression. Fix it first. |
Exit 125 on either endpoint is also a failure of the check. It means the script could not test that commit, so it says nothing about the bug.
The echo must come directly after the script on the same line, because $? holds the status of the most recent command only. In the first line, && chains the checkout and the script. The echo runs after both and reports the script's status. If the checkout itself fails, the script is skipped and $? shows the checkout's failure. Treat that as a failed check too.
Details that affect the result
Keep check.sh outside the files Git swaps during checkout, or copy it somewhere stable. Otherwise the checkout can replace it with another version or remove it, and your endpoint results will not match what the search sees.
Run the check from the same directory and with the same environment that git bisect run will use. A script that passes in one shell and fails in another has an environment dependency you need to find now.
Watch for a dirty working tree. Build output left over from the previous checkout can make a bad commit look good. If your build leaves artifacts behind, clean them inside the script before testing.
Return to your branch when you are done. A detached HEAD from the endpoint checkouts is harmless but easy to forget.
Exercise
Exercise
You run git checkout v1.4.0 && ./check.sh; echo "good endpoint exit: $?" and see good endpoint exit: 125. Should you start git bisect run with this script? Why or why not?
Show answer
No. The good endpoint must exit 0. Exit 125 means the script could not test that commit, for example because the build failed. Fix the build or the script first, and confirm exit 0 on the good commit and a non-zero code other than 125 on the bad commit before you start the search.