Sub topic 3
The exit code contract between your script and Git
git bisect run has no way to inspect your code and no knowledge of your test framework. It sees one number per commit: the exit status of the command you gave it. This chapter covers how Git reads that number, which values mean what, and which mistakes make a bisect point at the wrong commit.
This chapter assumes you can start a manual bisect session (git bisect start, good, bad, reset) and read a basic shell script. You should also know that $? holds the exit status of the last command.
What an exit status is
Every process ends with an integer between 0 and 255, called its exit status. The shell stores it in $?. By Unix convention, 0 means success and anything else means failure.
git bisect run reuses this convention. After it checks out a commit, it runs your command and sorts the commit into one of three outcomes, or stops the whole search. Doing this by hand means typing git bisect good or git bisect bad after each test. The exit status replaces those commands.
The contract
| Exit status | Git's interpretation | Equivalent manual command |
|---|---|---|
| 0 | The commit is good (old behavior) | `git bisect good` |
| 1 to 124, 126, 127 | The commit is bad (new behavior) | `git bisect bad` |
| 125 | The commit cannot be tested | `git bisect skip` |
| 128 to 255 | Abort the whole bisect | none |
The same rules apply if you replace "good" and "bad" with custom terms. The statuses keep their meaning for the old and new states.
Why 125 is special
Git needs one value that means "I could not decide". It cannot reuse 0 or 1, because those are real verdicts. It picked 125, which is unlikely to collide with a status that ordinary programs return for a real failure. Chapter material on skipping builds that fail comes later. For now, remember that 125 tells Git to leave the commit unclassified and pick another one nearby.
Why 128 and above aborts
Statuses of 128 and above conventionally mean the process was killed by a signal. The shell reports signal death as 128 plus the signal number. A segmentation fault (signal 11) gives 139. Git treats these as a sign that something is wrong with the environment, not with the code under test, and it stops instead of guessing. It prints a message saying the exit code is out of range, and the session stays active so you can inspect it.
A related trap is exit -1. Exit statuses are truncated to 8 bits, so exit -1 becomes 255, which aborts the bisect. Do not use negative numbers.
The gotchas
Most failed bisects come from a script that returns a status the author did not intend. These are the common ones.
A missing command looks like a bad commit
If the shell cannot find a command, it returns 127. If it finds the file but cannot execute it (for example, a missing execute bit), it returns 126. Both fall in the 1 to 127 range, so Git reads them as "bad".
That means a typo in the script name, or a script that is not executable, marks every commit bad. The search then converges on the oldest commit after your good endpoint. The result looks plausible and is wrong.
chmod +x ./check.sh
./check.sh; echo "exit: $?"Run your script by hand first and confirm it produces the status you expect.
Test runners use their own codes
Many test tools return values other than 0 and 1.
| Tool | Status | Meaning | What Git sees |
|---|---|---|---|
| pytest | 1 | Some tests failed | bad |
| pytest | 2 | Run interrupted | bad |
| pytest | 4 | Usage error (such as a wrong option) | bad |
| pytest | 5 | No tests were collected | bad |
| make | 2 | A build error occurred | bad |
A usage error or an empty test selection is not a regression, but Git counts it as one. Make your script decide which statuses mean "the bug is present" and map everything else deliberately.
A pipeline hides the real status
In a pipeline, the shell reports the status of the last command only.
./run_regression_test.sh | tee test.log
echo "$?"The second line prints the status of tee, which is almost always 0. Every commit would be marked good. Redirect to a file instead (./run_regression_test.sh > test.log 2>&1), or use a shell with set -o pipefail, such as bash.
The last command decides, unless you say otherwise
A shell script exits with the status of its last command. If you add a cleanup line at the end, such as rm -f test.log, its status replaces the test's. Capture the status into a variable right after the test and exit with that variable.
A crash in the program under test aborts the search
Suppose the regression you are hunting is a crash. The program dies from a signal, the shell sees status 139, and your script exits 139 if you pass it through unchanged. Git aborts. A crash is usually the bug itself, so it should be classified as bad.
A script that enforces the contract
This script maps the outcomes explicitly. It does not rely on whatever the test runner happens to return.
#!/bin/sh
# check.sh: exit 0 = good, 1 = bad, 125 = cannot test this commit
# Build. If the build fails, this commit cannot be judged.
make >/dev/null 2>&1 || exit 125
# Run the test and keep its status before doing anything else.
./run_regression_test.sh >test.log 2>&1
status=$?
case "$status" in
0)
exit 0 # the bug is absent
;;
1)
exit 1 # the test failed: the bug is present
;;
*)
# Signals (128 and above), usage errors, or "no tests ran"
# are not a clean verdict. Treat a crash as the bug, and
# anything else as untestable.
if [ "$status" -ge 128 ]; then
exit 1
fi
exit 125
;;
esacThis script assumes run_regression_test.sh returns 1 only for a real failure of the behavior you are chasing. If your test runner uses other statuses for real failures, add them to the 1) branch. If the runner itself can return 125, translate that value before it reaches Git, because Git would otherwise skip the commit.
Using exit 125 for unexpected statuses is a choice, not a rule. It is the safer one: a few skipped commits cost little, while a wrong "bad" verdict sends the search in the wrong direction.
Passing the contract through `sh -c`
You can give git bisect run a short inline command instead of a file. The same rules apply to the status of the whole command line.
git bisect run sh -c 'make || exit 125; ./run_regression_test.sh'Here make || exit 125 ends the script with 125 if the build fails. Otherwise the status of ./run_regression_test.sh becomes the status of the command, because it is the last command run. This works when the test returns only 0 or 1. If it can return other values, use a script file like the one above.
What happens when too many commits are skipped
If Git skips commits next to the real culprit, it cannot name a single first bad commit. It reports a list of candidates instead. Skipping is safe in small amounts, but a script that returns 125 for most commits gives you a range, not an answer.
Summary of the contract
- 0 means good, 125 means skip, 128 and above aborts, and every other value from 1 to 127 means bad.
- Capture the test's status immediately, and exit with it.
- Do not let pipelines, cleanup commands, or the shell's own errors (126, 127) decide the outcome by accident.
- Map unexpected statuses to 125 so the search skips them instead of treating them as bugs.
Exercise
Your script ends with the lines ./run_regression_test.sh | tee test.log and rm -f scratch.tmp. Every commit is marked good, even ones where the test clearly fails. Name two reasons for this and give one change that fixes both.
Show answer
First, the pipeline returns the status of tee, which is 0, so the test's failure is lost. Second, the final rm -f scratch.tmp also exits 0 and replaces whatever came before it as the script's status. Fix both by redirecting instead of piping (./run_regression_test.sh > test.log 2>&1), saving the status with status=$? on the next line, running the cleanup, and ending with exit "$status".
Exercise
A program under test crashes with a segmentation fault on the bad commits. Your script runs it as its last command. What does Git do, and how do you change the script so the crash counts as a bad commit?
Show answer
The shell reports 139 (128 plus signal 11) and the script exits with 139, which is 128 or more, so Git aborts the bisect. Capture the status with status=$? after running the program, then add if [ "$status" -ge 128 ]; then exit 1; fi so that a crash maps to "bad".