Sub topic 6
Checking the script on the good and bad endpoints first
A bisect script that gives wrong answers does not fail loudly. Git trusts every exit code it receives, so a faulty script still produces a "first bad commit" at the end. That commit is simply wrong. This chapter covers a two-minute check that catches most of these mistakes before you start the search.
This chapter assumes you can do the following:
- Use basic Git: commits, tags, branches, and
git checkout. - Read and write short shell scripts.
- Run your project's build and test commands from a terminal.
It also assumes you have already seen the exit code contract and have a reproducer script. This chapter calls that script check.sh.
Why the endpoints need checking
git bisect run trusts your script completely. It never looks at the script's output, only its exit status:
0marks the commit good.1to127, except125, marks it bad.125skips the commit.128and above aborts the whole bisect.
The two endpoints are the commits you named when you started: the known good commit (for example the tag v1.4.0) and the known bad commit (for example HEAD). Git assumes both are labeled correctly and never tests them. It tests only the commits between them. If your script would not return the right answer for the endpoints, you have no reason to believe it returns the right answer for the commits in between.
Several common mistakes show up at the endpoints.
- A typo that returns 127. If the script calls
./run_regresion_test.shand the file does not exist, the shell returns 127. That falls in the "bad" range, so every commit looks bad. Git then reports the commit right after your good endpoint as the culprit. - A test that passes everywhere. If the test does not actually reproduce the bug, every commit looks good. Git ends with no useful answer or a misleading one.
- A build that fails on the endpoint itself. If
makefails at the good tag, the script returns 125. A script that returns 125 on a commit you called good has an environment problem, not a code problem. - Output compared to the wrong thing. A grep that matches both the old and new behavior returns the same code everywhere.
Running the script by hand on both endpoints catches all four in a few minutes.
What a correct result looks like
For a script to be trustworthy, the two endpoints must behave like this:
| Endpoint | Expected exit code | What a wrong value means |
|---|---|---|
| Known good commit | `0` | Nonzero means the script, the build, or your choice of good commit is wrong. |
| Known bad commit | `1` to `124`, or `126`, `127` | `0` means the test does not reproduce the bug. `125` means the build is broken at the bad commit. |
Prefer a bad-endpoint exit code of 1 over 126 or 127. Codes 126 and 127 are what the shell uses for "found but not executable" and "command not found". They usually point to a broken script, not a real test failure. If you see them at the bad endpoint, read the script's error output before trusting the result.
Checking by hand
The simplest check is to move to each endpoint and run the script. Print the exit code immediately, because the next command will overwrite $?.
git checkout v1.4.0 && ./check.sh; echo "good endpoint exit: $?"
git checkout main && ./check.sh; echo "bad endpoint exit: $?"Here v1.4.0 is the good endpoint and main is the bad one. The expected output is good endpoint exit: 0 and bad endpoint exit: 1.
Three details matter here.
- The
&&chain. Ifgit checkoutfails, for example because you have uncommitted changes that conflict,./check.shdoes not run. Then$?holds the failure from checkout, and you might read it as a script result. If you see an error from Git, stop and fix that first. - A clean working tree. Commit or stash your changes before checking out the endpoints. A bisect also needs a clean tree, because Git checks out many commits during the search.
- Where you end up. After the check you are on the bad endpoint, in this case
main. If you started somewhere else, return there before you rungit bisect start.
Making the check repeatable
If you reuse a script across several bug hunts, or you want a teammate to run the same check, wrap the two runs in a small helper. This one takes the good commit, the bad commit, and the path to the script. It returns you to where you started, whether that was a branch or a detached commit.
#!/bin/sh
# validate-endpoints.sh GOOD BAD SCRIPT
# Exits 0 if the script behaves correctly on both endpoints.
good=$1
bad=$2
script=$3
orig=$(git symbolic-ref --short -q HEAD || git rev-parse HEAD)
git checkout -q "$good" || exit 2
"$script"
g=$?
git checkout -q "$bad" || { git checkout -q "$orig"; exit 2; }
"$script"
b=$?
git checkout -q "$orig"
echo "good endpoint exit: $g"
echo "bad endpoint exit: $b"
if [ "$g" -eq 0 ] && [ "$b" -ge 1 ] && [ "$b" -le 127 ] && [ "$b" -ne 125 ]; then
echo "OK: endpoints behave as expected"
else
echo "NOT OK: fix the script before bisecting"
exit 1
fiRun it as ./validate-endpoints.sh v1.4.0 main ~/bin/check.sh. The script's exit code is 0 only when both endpoints behave correctly, so you can also use it in a larger shell script or a CI job.
The helper does not check for a dirty working tree. If checkout would overwrite your changes, Git refuses and the helper exits with 2, leaving your files alone.
The script must exist at both endpoints
There is a subtle reason to keep check.sh outside the repository, or at least untracked. When Git checks out the good endpoint, it replaces tracked files with their versions from that commit. If check.sh is committed only on main, it will not exist at v1.4.0. The first checkout then silently removes it, and the shell reports "not found" with exit code 127. That reads as "bad" at an endpoint that should be good.
Validation shows this problem right away, because the good-endpoint run fails. The fix is to store the script somewhere Git does not manage, for example ~/bin/check.sh or /tmp/check.sh, and call it by absolute path. Then your validation and your bisect use the same file no matter which commit is checked out.
Common failures and what they tell you
| Symptom at the endpoints | Likely cause | Fix |
|---|---|---|
| Good endpoint returns `127` | Script or a command it calls is missing at that commit | Move the script outside the repo and use an absolute path. |
| Good endpoint returns `125` | The build fails at the old commit, often due to a changed toolchain | Choose a nearer good commit, or install the older dependencies. |
| Good endpoint returns `1` | The bug already exists there, or the test is too strict | Check the test manually. Pick an older good commit if the bug is older. |
| Bad endpoint returns `0` | The test does not reproduce the bug | Make the test fail on the buggy behavior. |
| Both return `1` | The test checks something unrelated to the regression | Compare output by hand and tighten the check. |
| Both return `0` after a rebuild you skipped | Stale build artifacts hide the change | Add a clean step, such as `make clean`, before the build. |
Checking the behavior you cannot see
Exit codes only tell you the final status. For the endpoints, also look at the output once. Run the script with its output visible and read it:
git checkout v1.4.0
./check.sh
echo "exit: $?"On the good endpoint you want to see the test pass for the right reason, not because the script exited early. On the bad endpoint you want to see the failure message that matches the bug you are hunting. A script that returns 1 because of an unrelated compile error at the bad commit is wrong in a way the exit code cannot show.
If the script contains set -e or || exit 125 on build steps, test that path too. Break the build on purpose in a scratch copy and confirm the script returns 125. The skip path is the one you exercise least, and it matters when the history contains broken commits.
After validation
Once both endpoints behave, start the search from the same place you validated:
git bisect start HEAD v1.4.0
git bisect run ~/bin/check.sh
git bisect resetNote the order in git bisect start: the bad commit comes first, then the good one. The validated script path is the same one you tested by hand.
Exercise
You run your script on the good tag and it exits with 127. On the bad commit it also exits with 127. If you ran git bisect run anyway, what would Git report, and what is the likely cause of the 127?
Show answer
Git would treat every tested commit as bad, so it would report the commit immediately after the good endpoint as the first bad commit, which is wrong. Exit code 127 means "command not found" in the shell, so the likely cause is a missing or misspelled script or command, or a script stored in the repository that does not exist at the checked-out commit. Fix the path, move the script outside the repository, and validate again so the good endpoint returns 0.
Exercise
The good endpoint returns 0 and the bad endpoint also returns 0. What does this tell you, and what should you change before bisecting?
Show answer
The script does not detect the bug, because it reports success on a commit known to be broken. Change the test so it fails when the buggy behavior appears, rerun it on the bad endpoint until it returns a code from 1 to 124 (or 126 or 127, though 1 is preferable), and confirm the good endpoint still returns 0.