Sub topic 2
Where the Script Lives While Git Changes the Checkout
Assume you know basic Git commands, can write simple shell scripts, and can run a project’s build and tests from a terminal. In this chapter, the script means a reproducible check that says whether the current commit contains the regression.
Keep the Script Outside the Repository
During a bisect, Git repeatedly checks out different commits. A script tracked in the repository can be deleted, replaced, or become incompatible at an older commit. Store it outside the working tree, such as ~/bin/check-regression.sh, and call it with an absolute or stable path.
#!/bin/sh
# Exit 0 for good, 1 for bad, and 125 when this commit cannot be tested.
make >/dev/null 2>&1 || exit 125
./run_regression_test.shGit runs the command from the repository’s top-level directory, so relative paths inside the script refer to the checked-out project. The script’s own location does not change.
Check Both Endpoints First
Validate the checker before allowing it to drive the search. These commands should produce a good result at the known good endpoint and a bad result at the known bad endpoint.
git checkout v1.4.0
~/bin/check-regression.sh
echo "good endpoint exit: $?"
git checkout main
~/bin/check-regression.sh
echo "bad endpoint exit: $?"If either result is wrong, fix the script or choose better endpoints. A flaky test is unsafe here because Git assumes each result describes the commit consistently.
Let Git Run the Search
Start with the known bad commit, HEAD, and the known good tag, v1.4.0. Git then checks roughly log2(N) commits for a range of N commits.
git bisect start HEAD v1.4.0
git bisect run ~/bin/check-regression.shGit prints the first bad commit after the search. Inspect it with git show <commit> before drawing conclusions.
Skip Commits That Cannot Be Tested
Use exit code 125 when a commit cannot build or cannot run the test. Git skips that commit instead of treating it as good or bad. A normal failing regression test should return a nonzero code other than 125.
Exercise
Why should the bisect checker live outside the repository?
Show answer
Git changes the checkout at every step, so a tracked checker could be deleted or changed.