Sub topic 4
Writing a reproducer script
This chapter assumes you can use basic Git (commits, tags, checkout), read and write short shell scripts, and run your project's build and test commands. You have also seen 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.
A reproducer script is a small program that answers one question about the current checkout: does the bug show up here? Git runs it at every step of the search and reads only the exit status.
What a good reproducer does
A useful script has three properties.
- Deterministic. The same commit always gives the same answer.
- Narrow. It checks the one symptom you are hunting, not the whole test suite.
- Honest about failure to test. If it cannot build or run, it says so with 125 instead of reporting "bad".
The last point is the most common source of wrong results. A commit that does not compile has not shown the bug, so it must not be marked bad.
A first script
This script builds the project, then runs the regression test. The build step is separated from the test step so each can report differently.
#!/bin/sh
# check.sh: exit 0 = good, 1 = bad, 125 = cannot test this commit
make >/dev/null 2>&1 || exit 125
./run_regression_test.shThe line make >/dev/null 2>&1 || exit 125 hides the build output and exits with 125 if the build fails. If the build succeeds, the script reaches its last line. A shell script exits with the status of its last command, so the exit status of run_regression_test.sh becomes the script's exit status. If that test exits 0, the commit is good. If it exits with 1, the commit is bad.
Keep the script outside the checkout
Git rewrites the working tree at every step. If check.sh is a tracked file, it may change or vanish when Git checks out an older commit. Keep it outside the repository, or at least untracked and ignored.
cp check.sh /tmp/check.sh
chmod +x /tmp/check.shYou then pass /tmp/check.sh to Git. The script still runs inside the repository directory, so relative paths such as ./run_regression_test.sh refer to the checked-out files.
An inline alternative
For a short check, skip the file and use sh -c.
git bisect run sh -c 'make || exit 125; ./run_regression_test.sh'Quoting matters here. The single quotes pass the whole string to sh -c as one argument. Once the logic grows past two or three commands, move it into a file so you can test it by hand.
Common errors
| Mistake | Effect | Fix | Column | Column |
|---|---|---|---|---|
| Build failure returns 1 | Broken commits are marked bad, so the result is wrong | Use `make \ | \ | exit 125` |
| Script is not executable | Git cannot run it and the session aborts | Run `chmod +x` | ||
| Script uses `exit 130` or higher for "bad" | Codes of 128 and above abort the search | Use 1 for bad | ||
| Test depends on files that change between commits | Results vary by commit for the wrong reason | Keep test data outside the repository | ||
| Script prints to stdout | Harmless, but it buries Git's own progress lines | Redirect noisy commands |
Test the script by hand first
Before Git uses the script, run it yourself at both ends of the range. It must return 0 on the known good commit and a non-zero code on the known bad commit.
git checkout v1.4.0 && ./check.sh; echo "good endpoint exit: $?"
git checkout main && ./check.sh; echo "bad endpoint exit: $?"If the good endpoint does not print 0, or the bad endpoint prints 0 or 125, fix the script before you start the search. The next chapters cover this check in more detail.
Exercise
Your script runs make && ./run_regression_test.sh, with no || exit 125. Several old commits fail to compile. What does git bisect do with those commits, and why is the result unreliable?
Show answer
When make fails, the && stops the script and it exits with make's non-zero status, usually 2. Git reads any code from 1 to 127 other than 125 as bad, so it marks the uncompilable commits as bad even though they never ran the test. The search can then report a commit that only broke the build, not the one that introduced the regression. Adding || exit 125 after make makes Git skip those commits.