Find Regression Commits Automatically with git bisect in WordPress Development

Find the commit that broke a WordPress plugin with git bisect

“It worked in version 2.3” is a good starting point. Between that tag and today there are 80 commits, and one of them introduced the bug. git bisect finds it by binary search: it checks out the commit in the middle, you say whether the bug is there, and after about seven steps git names the guilty commit. With a test script, git does the checking itself.

Problem: A WooCommerce extension throws a TypeError at checkout. Version 2.3.0 was fine, the current main is broken, and there are 80 commits in between, many of them touching the cart code.

Solution: Start git bisect with main as bad and v2.3.0 as good, test the commits git checks out by hand or let git bisect run call a script that returns 0 for good, 1 for bad and 125 for “cannot test”, then read the commit git reports and end with git bisect reset.

By hand

git bisect start
git bisect bad              # the current commit has the bug
git bisect good v2.3.0      # this tag did not
# Bisecting: 40 revisions left to test after this (roughly 6 steps)

# test the checked-out commit (load the checkout in the local site), then one of:
git bisect good
git bisect bad
git bisect skip             # this commit cannot be tested (does not build, other error)

# ... until git prints:
# 3f2a9c1 is the first bad commit

git bisect reset            # back to the branch you started on

Every answer halves the range: 80 commits need at most 7 steps, 1,000 commits about 10. If the test runs in a local WordPress where the plugin folder is the repository, every checkout is live at once; run composer install or the asset build when the commit changes dependencies. git bisect log shows the answers so far, and git bisect visualize (or git log --oneline during a session) shows the remaining range.

Automatic with a test

When the bug can be reproduced by a test, git runs the whole search alone. The script must exit with 0 when the commit is good, with 1 to 127 (except 125) when it is bad, and with 125 when the commit cannot be tested.

#!/usr/bin/env bash
# /tmp/bisect-test.sh, outside the repository, so that checkouts do not change or remove it
composer install --quiet --no-interaction || exit 125
# the new regression test lives outside the repository as well; phpunit.xml of the checkout provides the bootstrap
vendor/bin/phpunit /tmp/CartCouponRegressionTest.php
git bisect start main v2.3.0     # bad first, then good
git bisect run /tmp/bisect-test.sh
# running '/tmp/bisect-test.sh'
# ...
# 3f2a9c1e is the first bad commit
# commit 3f2a9c1e...
#     Cast cart totals to int before applying the coupon
git bisect reset

The test usually does not exist in the old commits, because it is written now, for this bug. That is why it lives outside the repository too and is passed to PHPUnit by path, as above; a test file that is tracked in the repository would disappear when git checks out an older commit. A failing composer install (old lock file, removed package) exits 125, and git skips that commit instead of calling it bad. How to set up the tests in the first place: unit tests for WordPress plugins with PHPUnit.

After the search

git show 3f2a9c1             # the change that introduced the bug
git revert 3f2a9c1           # if the fastest fix is to undo it

Often the commit is a mix of a refactoring and the real change; then the diff shows exactly which line to fix. A history of small commits with one change each makes bisect much more precise, which is one more reason to clean up commits before merging: interactive rebase.

Merges and first-parent

In a repository with feature branches, bisect also walks into the branches and can stop at a commit in the middle of a feature that never existed on main alone. With git bisect start --first-parent (Git 2.29+), git tests only the merge commits on main and names the merged pull request; from there, a second bisect inside that branch finds the single commit if needed.

NOTE: Uncommitted changes go with every checkout and can make good commits look bad. Commit them or put them away with git stash before git bisect start: git stash. And when the database matters (a migration in the plugin), reset the database between steps, or the state of the newer commits leaks into the test of the older ones.