Sub topic 4
Writing a reproducer script
This chapter assumes you can run a project's build and test commands from the shell, read and write short shell scripts, and use basic Git commands such as checkout and tag. It also assumes you 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.
A reproducer script is a small program that answers one question about the commit currently checked out: does the bug exist here? git bisect run calls it at every step and reads only its exit status. A good script is what makes the whole automated search trustworthy.
What a reproducer must do
A reproducer has three jobs.
- Prepare the checkout so the test can run, usually by building the project.
- Exercise the bug and decide whether it is present.
- Report the answer through the exit status, and report "I could not tell" separately from "the bug is present".
The third job is where most scripts fail. If a broken build exits with 1, Git records that commit as bad, even though the bug was never tested. The search then converges on the wrong commit and gives no sign that anything went wrong.
A minimal script
The following script builds the project and runs a regression test. It is the same pattern the chapter's examples use throughout.
#!/bin/sh
# check.sh: exit 0 = good, 1 = bad, 125 = cannot test this commit
make >/dev/null 2>&1 || exit 125
./run_regression_test.shLine by line:
make >/dev/null 2>&1 || exit 125runs the build with all output discarded. If the build fails, the script exits with125, so Git skips the commit instead of calling it bad../run_regression_test.shis the last command. A shell script's exit status is the status of its last command, so the test's status becomes the script's status. If the test exits0, the commit is good. If it exits1, the commit is bad.
This works only if run_regression_test.sh follows the contract itself. Many test runners exit with 0 for success and 1 for failure, which fits. Check the documentation of your runner for other codes, because a runner that exits with 125 or with a value above 127 for its own reasons will confuse Git.
Choosing what the script tests
The script should check for the specific symptom you are hunting, not for general health. Consider these options, from most to least precise.
| Approach | Strength | Risk |
|---|---|---|
| One targeted test that fails because of the bug | Fast, and a failure means the bug is present | Requires that the test exists or that you write it |
| The full test suite | No new test to write | Slow, and unrelated failures in old commits look like the bug |
| A manual command whose output you compare | Works when no test exists | Brittle, because output formats change between commits |
Prefer the targeted test. If the project has no test for the bug, write one first. It is usually a few lines, and you can keep it after the hunt as a regression test.
The test may not exist in old commits
If the regression test is a file in the repository, older commits may not contain it. Checking out an old commit then removes the file, and the script fails for a reason unrelated to the bug. The next chapter covers keeping the script outside the checked-out files. For now, remember that a missing test is an "untestable" condition, not a "bad" one.
Mapping outcomes to exit codes
Think of each step in the script as having three possible outcomes: it worked, it failed because of the bug, or it failed for another reason. Only the middle one means bad.
| Situation at this commit | Exit code |
|---|---|
| Build fails | `125` |
| A required file or tool is missing | `125` |
| Build succeeds, test passes | `0` |
| Build succeeds, test fails because of the bug | `1` |
| Test crashes in a way unrelated to the bug | `125` |
A more defensive version of the script handles these cases explicitly.
#!/bin/sh
# check.sh: exit 0 = good, 1 = bad, 125 = cannot test this commit
# Cannot test: the build is broken at this commit.
make >/dev/null 2>&1 || exit 125
# Cannot test: the program we need was not produced.
[ -x ./app ] || exit 125
# Run the scenario that exposes the bug.
output=$(./app --export sample.csv 2>/dev/null)
status=$?
# The program crashed or was killed by a signal: not the bug we are chasing.
[ "$status" -ge 126 ] && exit 125
# The bug: the export drops the header row.
case "$output" in
id,name,total*) exit 0 ;; # header present: good
*) exit 1 ;; # header missing: bad
esacPoints worth noting:
statusis captured immediately after the command, before any other command can overwrite$?.- A status of
126or above usually means the shell could not run the file, the file was not found, or a signal ended the process. Mapping these to125keeps them out of the "bad" group. Without the mapping,127(command not found) would count as bad, and a status of128or more would abort the whole search. - The final
casechecks the symptom itself. It does not trust the program's own exit status, because this bug produces wrong output without any error.
Making the script deterministic
Bisect assumes that each commit has one stable answer. Anything that varies between runs can mislead it.
- Remove stale build output. If an incremental build reuses objects from a different commit, the result may not match the checked-out source. Use
make cleanor a clean build directory when your build system is not reliable about this. - Fix seeds, dates, and locale. Set values such as
LC_ALL=Cand a fixed random seed inside the script. - Avoid the network. A test that calls an external service can fail for reasons unrelated to the commit.
- Do not depend on state left over from an earlier step, such as a database file created by a previous run. Create what you need at the start and remove it at the end.
#!/bin/sh
# check.sh: deterministic variant
export LC_ALL=C
export TZ=UTC
workdir=$(mktemp -d) || exit 125
trap 'rm -rf "$workdir"' EXIT
make clean >/dev/null 2>&1
make >/dev/null 2>&1 || exit 125
DATA_DIR="$workdir" SEED=42 ./run_regression_test.shThe trap line removes the temporary directory whatever the exit path. It does not change the exit status, because rm -rf runs in a trap handler that does not replace the script's pending status.
Setting the exit status with `set -e` and pipes
Shell scripts can hide failures in two ways, and both affect bisection.
First, a pipeline reports the status of its last command. In ./run_test.sh | tee out.log, a failing test still gives status 0, because tee succeeded. The commit is then called good. Either avoid the pipe, or write the output to a file with > and check the status of the test directly.
Second, set -e makes the script exit on the first failing command, but the status it exits with is that command's own status. A failed make would then exit with 2, which Git reads as bad. If you use set -e, handle the build step explicitly with || exit 125 as shown above.
#!/bin/sh
make >/dev/null 2>&1 || exit 125
./run_regression_test.sh > test.log 2>&1
exit $?Here exit $? is optional but makes the intent clear: the test's status is the answer.
Normalizing the test's status
Sometimes the test exits with codes that do not match the contract. A test may exit with 2 for "assertion failed" and 3 for "setup error". Both would be bad to Git. Map them yourself.
#!/bin/sh
make >/dev/null 2>&1 || exit 125
./run_regression_test.sh
case $? in
0) exit 0 ;; # test passed: good
2) exit 1 ;; # assertion failed: the bug is present
*) exit 125 ;; # setup error or anything unexpected: cannot judge
esacTreating unknown codes as 125 is the safer default. A wrong skip only costs one extra test. A wrong "bad" can send the search down the wrong half of the history.
Testing the script by hand
Before involving Git's search, run the script yourself and look at the status.
./check.sh; echo "exit: $?"Run it on a commit you know is good, then on one you know is bad, and confirm the two statuses differ as expected. The chapter on validating endpoints covers this check in full.
Common mistakes
- Letting a broken build count as bad. Always guard the build with
|| exit 125. - Using
exitcodes of 128 or more by accident. Such codes abort the search. A crash by signal, for example, appears as128plus the signal number. - Leaving the script unexecutable. Run
chmod +x check.sh. Otherwise the shell returns126, which Git treats as bad on every commit. - Reading the wrong status. Capture
$?right after the command, and avoid pipes that mask it. - Testing too much. A long suite makes each step slow and invites unrelated failures. Test the symptom.
Summary of the design rules
- Build first, and map build failure to
125. - Test the specific symptom, not the whole project.
- Return
0only when you are sure the bug is absent, and1only when you are sure it is present. - Return
125for everything you cannot judge. - Keep the run deterministic and free of leftover state.
- Keep pipes and
set -efrom changing the status you meant to return.
Exercise
A reproducer script contains make || exit 1 followed by ./run_regression_test.sh. Several old commits do not compile. What goes wrong during git bisect run, and how do you fix the script?
Show answer
Each commit that fails to build exits with 1, so Git records it as bad even though the bug was never tested. The search can then end on a commit that merely fails to build. Change the build line to make >/dev/null 2>&1 || exit 125 so those commits are skipped.
Exercise
Your test runner exits with 0 when all tests pass, 4 when an assertion fails because of the bug, and 9 when it cannot connect to a database. Write the case statement that maps these codes to the exit codes Git expects.
Show answer
case $? in 0) exit 0 ;; 4) exit 1 ;; *) exit 125 ;; esac. Code 9 falls into the default branch and becomes 125, so the commit is skipped instead of being called bad.