Sub topic 10
Automating bisect with a test script
This chapter assumes you can run git bisect by hand: start, good, bad, and reset. It also assumes you can write a short shell script and know that a command's exit code of 0 means success. Here you hand the good/bad decisions to a script, so Git can search history without you.
Why automate
A manual bisect over 1,000 commits takes about 10 rounds. Each round means building, testing, and typing an answer. Git can run a command for each round and read its exit code. You only need to write a script that reports whether the bug is present.
The exit code contract
git bisect run runs your command on every commit it checks out. It reads the exit code like this:
| Exit code | Meaning |
|---|---|
| 0 | The commit is good |
| 1 to 127, except 125 | The commit is bad |
| 125 | The commit cannot be tested, so skip it |
| 128 or higher | Abort the bisect |
A code of 128 or higher stops the whole run. This protects you from a broken script, such as one killed by a signal, being treated as a verdict.
Write the test script
The script must exit 0 when the bug is absent and non-zero when it is present. It must also handle commits that cannot be built.
#!/bin/sh
# test-regression.sh
make -s || exit 125 # cannot build: tell bisect to skip this commit
./run-tests.sh # exit 0 = good, exit 1 to 127 (except 125) = badThe build step matters. Without || exit 125, a failed build returns a non-zero code. Bisect would then mark that commit as bad, even though the bug was never tested. That points you at the wrong commit.
Make the script executable and keep it outside the repository, or untracked. Bisect checks out old commits, and a tracked script could be replaced by an older version or disappear.
chmod +x test-regression.shRun the bisect
Give start the bad commit first and the good commit second. Then run the script.
git bisect start HEAD v1.2.0
git bisect run ./test-regression.sh
git bisect log
git bisect resetHere HEAD is the bad commit and v1.2.0 is the last known good one. Git checks out the middle commit, runs the script, reads the exit code, and halves the range. This repeats until Git prints the first bad commit.
git bisect log prints every decision made. git bisect reset returns you to the branch you started on. Always run it when you finish.
Use a one-line command instead
You do not always need a script file. You can pass a command directly.
git bisect start HEAD v1.2.0
git bisect run sh -c 'make -s || exit 125; ./run-tests.sh'
git bisect resetUse a file when the logic grows. Use sh -c for quick checks.
Common errors
- Reversed verdict. A script that exits 0 when the bug is present finds the wrong commit. Test the script by hand on a known good commit and a known bad commit first.
- Test passes everywhere. If the first commit you mark good is actually bad, bisect reports a nonsense result. Confirm the good commit with your script before you start.
- Flaky tests. A test that fails at random sends the search the wrong way. Run it several times in the script, or fix the flakiness first.
- Script inside the repository. Old commits may lack the script or contain a different version. Keep it outside the working tree.
- Stale build files. Leftover build output from another commit can hide or fake the bug. Clean when needed, for example with
make cleanbeforemake.
Exercise
Your script runs make -s and then ./run-tests.sh. On one old commit the project fails to compile. What should the script do for that commit, which exit code should it use, and what goes wrong if it uses plain make -s without handling the failure?
Show answer
The script should skip the commit with make -s || exit 125. Exit code 125 tells bisect the commit cannot be tested. Without it, make fails with a non-zero code in the 1 to 127 range, and bisect marks the commit as bad even though the bug was never tested. That can lead bisect to blame the wrong commit.
Exercise
Write the commands that start a bisect with HEAD as bad and the tag v2.0.0 as good, run ./check.sh automatically, and return to your original branch afterward.
Show answer
git bisect start HEAD v2.0.0 git bisect run ./check.sh git bisect reset