Sub topic 3
The exit code contract between your script and Git
This chapter assumes you know basic Git (commits, tags, checkout), can read and write short shell scripts, and can run your project's build and test commands. You should also have seen a manual git bisect session with start, good, bad and reset.
Why a contract is needed
In a manual session, you look at each commit Git checks out and type git bisect good or git bisect bad. With git bisect run, a script gives that answer instead. Git cannot read your test output, so the script reports the verdict through its exit status. This is the number a process returns when it finishes, and the shell stores it in $?.
The mapping from exit status to verdict is the contract. Your script has to follow it exactly, or the search will point at the wrong commit.
The four ranges
| Exit status | Git's interpretation |
|---|---|
| 0 | The commit is good. |
| 1 to 124, 126, 127 | The commit is bad. |
| 125 | The commit cannot be tested, so skip it. |
| 128 and above | Something went wrong, so abort the whole bisect. |
Two details trip people up.
- Zero means good, and nothing else does. Most test runners already follow this: they return 0 when every test passes and non-zero when one fails. That is why a test command can often be passed to
git bisect rundirectly. - 125 is a request to skip. Git marks the commit as untestable and picks a nearby commit instead. Use it when the commit itself is the problem, for example when the project does not compile.
The abort range exists as a safety stop. If the script returns 128 or more, Git ends the run and leaves the session open so you can investigate. Shells and the system use codes in this range for fatal signals. A script killed by a signal reports 128 plus the signal number, so a crash of your test harness stops the search instead of being counted as a bad commit.
Codes 126 and 127 are the shell's way of saying "found the file but could not execute it" and "command not found". Git treats them as bad, as with any other value from 1 to 127. A missing or non-executable script therefore makes every commit look bad, which is one reason to validate the script first. A later chapter covers that check.
Good, bad and skip in a script
The following script follows the contract. It reports a build failure as 125, because a commit that does not build says nothing about the bug. It then passes on the status of the regression test.
#!/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 last line has no explicit exit. A shell script exits with the status of its last command, so the test's status becomes the script's status. If run_regression_test.sh returns 0, the commit is good. If it returns 1, the commit is bad.
You can also write the verdicts out. This makes the intent easier to read.
#!/bin/sh
make >/dev/null 2>&1 || exit 125
if ./run_regression_test.sh; then
exit 0
else
exit 1
fiCommon mistakes
- Using
|| exit 1after the build. A broken build is then reported as a bad commit. Git may name an unrelated commit as the first bad one. - Letting a failing setup step end the script with its own status. If a tool exits with 2 because of a missing dependency, Git sees a bad commit. Check preconditions and return 125 for them.
- Piping the test through another command. In
./test | tee out.log, the status is that ofteein most shells, not the test's. Redirect to a file instead, or useset -o pipefailin shells that support it. - Returning 128 or above by accident. Calling
exit 130or similar aborts the run. Keep your own codes below 125.
How the contract drives the search
The following diagram shows how one run of the script becomes one step of the search.
Exercise
Your script runs make and then the regression test. The build fails on one commit, and the script ends with make || exit 1. What goes wrong, and what should the line be?
Show answer
Git counts the unbuildable commit as bad, so it may report that commit, or one near it, as the first bad commit even though the bug is not there. The line should be make || exit 125, which tells Git to skip the commit.
Exercise
A test harness is killed by a signal and the script returns 137. How does git bisect run treat this, and why is that useful?
Show answer
Git aborts the run, because 128 and above is the abort range. This is useful because a harness crash is not evidence that the commit has the bug, so the search stops instead of recording a false bad commit.