Sub topic 1
Automating a Bug Hunt with `git bisect run`
Prerequisites
You should know commits, branches, tags, and basic shell scripts. You should also be able to build and test the project from a terminal.
Why binary search finds a regression quickly
A regression is a new failure caused by a previously working change. git bisect finds the first bad commit by binary search.
You provide one known good commit and one known bad commit. Git checks the commit halfway between them. If that commit is bad, Git searches the earlier half. If it is good, Git searches the later half. Each test removes about half the remaining commits.
The number of tests is about log₂(N) for N candidate commits. For example, 1,024 commits require about 10 tests, not 1,024.
A manual bisect session: start, good, bad, reset
Start with the current commit as bad and a known working tag as good:
git bisect start HEAD v1.4.0Git checks out a midpoint. Run the regression test, then mark that commit:
./run_regression_test.sh
git bisect good # use this if the test passes
# or:
git bisect bad # use this if the test failsRepeat until Git reports the first bad commit. End the session and return to the original branch:
git bisect resetThe exit code contract between your script and Git
With git bisect run, Git interprets the script's exit status:
| Exit status | Meaning |
|---|---|
| `0` | The commit is good |
| `1`–`127`, except `125` | The commit is bad |
| `125` | Skip this commit because it cannot be tested |
| `128` or higher | Abort because an error occurred |
Writing a reproducer script
Put the script outside files that Git will replace during checkout, such as in /tmp. Make it build the project and run one deterministic regression test:
#!/bin/sh
# check.sh: 0 = good, 1–124 or 126–127 = bad, 125 = skip
make >/dev/null 2>&1 || exit 125
./run_regression_test.shMake it executable, then run it with:
chmod +x /tmp/check.sh
git bisect run /tmp/check.shExercise
Why does Git need only about 10 tests for 1,024 candidate commits?
Show answer
Each test removes about half the candidates, and log₂(1,024) equals 10.