4.11 Debugging With git bisect#

Estimated time to complete this notebook: 5 minutes

You can use

git bisect

to find out which commit caused a bug.

An example repository#

In a nice open source example, I found an arbitrary exemplar on github

import os

top_dir = os.getcwd()
git_dir = os.path.join(top_dir, "learning_git")
os.chdir(git_dir)
%%bash
rm -rf bisectdemo
git clone https://github.com/shawnsi/bisectdemo.git
Cloning into 'bisectdemo'...
bisect_dir = os.path.join(git_dir, "bisectdemo")
os.chdir(bisect_dir)
%%bash
python squares.py 2 # 4
4

This has been set up to break itself at a random commit, and leave you to use bisect to work out where it has broken:

%%bash
./breakme.sh > break_output
error: branch 'buggy' not found.
Switched to a new branch 'buggy'

Which will make a bunch of commits, of which one is broken, and leave you in the broken final state

python squares.py 2 # Error message
  Cell In[6], line 1
    python squares.py 2 # Error message
           ^
SyntaxError: invalid syntax

Bisecting manually#

%%bash
git bisect start
git bisect bad # We know the current state is broken
git checkout master
git bisect good # We know the master branch state is OK
status: waiting for both good and bad commits
status: waiting for good commit(s), bad commit known
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
Bisecting: 500 revisions left to test after this (roughly 9 steps)
[09101c02a2bcb250be5ca336579b30fbfe449404] Comment 500

Bisect needs one known good and one known bad commit to get started

Solving Manually#

python squares.py 2 # 4
git bisect good
python squares.py 2 # 4
git bisect good
python squares.py 2 # 4
git bisect good
python squares.py 2 # Crash
git bisect bad
python squares.py 2 # Crash
git bisect bad
python squares.py 2 # Crash
git bisect bad
python squares.py 2 #Crash
git bisect bad
python squares.py 2 # 4
git bisect good
python squares.py 2 # 4
git bisect good
python squares.py 2 # 4
git bisect good

And eventually:

git bisect good
    Bisecting: 0 revisions left to test after this (roughly 0 steps)

python squares.py 2
    4

git bisect good
2777975a2334c2396ccb9faf98ab149824ec465b is the first bad commit
commit 2777975a2334c2396ccb9faf98ab149824ec465b
Author: Shawn Siefkas <shawn.siefkas@meredith.com>
Date:   Thu Nov 14 09:23:55 2013 -0600

    Breaking argument type
git bisect end

Solving automatically#

If we have an appropriate unit test, we can do all this automatically:

%%bash
git bisect start
git bisect bad HEAD # We know the current state is broken
git bisect good master # We know master is good
git bisect run python squares.py 2
Previous HEAD position was 09101c0 Comment 500
Switched to branch 'buggy'
status: waiting for both good and bad commits
status: waiting for good commit(s), bad commit known
Bisecting: 500 revisions left to test after this (roughly 9 steps)
[09101c02a2bcb250be5ca336579b30fbfe449404] Comment 500
running 'python' 'squares.py' '2'
4
Bisecting: 250 revisions left to test after this (roughly 8 steps)
[cd7aa3dd08a178238a565bb73bdab04b2794005c] Comment 750
running 'python' 'squares.py' '2'
4
Bisecting: 125 revisions left to test after this (roughly 7 steps)
[d6ebdcfde2a81bb7a0da460de8532c7ade963646] Comment 875
running 'python' 'squares.py' '2'
4
Bisecting: 62 revisions left to test after this (roughly 6 steps)
[44b4622328c07fee40d25f7ce4c555da0d8928b0] Comment 937
running 'python' 'squares.py' '2'
Traceback (most recent call last):
  File "squares.py", line 9, in <module>
    print(integer**2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
Bisecting: 31 revisions left to test after this (roughly 5 steps)
[eacd319505bb4c4c34d3268c069e549d30968981] Comment 905
running 'python' 'squares.py' '2'
Traceback (most recent call last):
  File "squares.py", line 9, in <module>
    print(integer**2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
Bisecting: 15 revisions left to test after this (roughly 4 steps)
[93c7d1e9f1551f7bfe72ec8cb9d1afb0b95f2e58] Comment 890
running 'python' 'squares.py' '2'
4
Bisecting: 7 revisions left to test after this (roughly 3 steps)
[944da50272fa80e14e6f78aed6fbb898649170bb] Comment 898
running 'python' 'squares.py' '2'
4
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[28f9671ec7102a577a735662c61cb2e3e849d428] Comment 902
running 'python' 'squares.py' '2'
4
Bisecting: 1 revision left to test after this (roughly 1 step)
[a48813f9b6581f80f80dc078bf9d8e005328843a] Breaking argument type
running 'python' 'squares.py' '2'
Traceback (most recent call last):
  File "squares.py", line 9, in <module>
    print(integer**2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[3c096dabfe5379e0b2b7d54bead4407e94747690] Comment 903
running 'python' 'squares.py' '2'
4
a48813f9b6581f80f80dc078bf9d8e005328843a is the first bad commit
commit a48813f9b6581f80f80dc078bf9d8e005328843a
Author: Shawn Siefkas <shawn.siefkas@meredith.com>
Date:   Thu Nov 14 09:23:55 2013 -0600

    Breaking argument type

 squares.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
bisect found first bad commit

Boom!