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)
[b56f6abddea71d5ef15b66a00c615d2a35544ff2] 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 b56f6ab 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)
[b56f6abddea71d5ef15b66a00c615d2a35544ff2] Comment 500
running 'python' 'squares.py' '2'
4
Bisecting: 250 revisions left to test after this (roughly 8 steps)
[0e6cdcc829d375142f62ebd51bc1ef4d45b28016] Comment 749
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: 124 revisions left to test after this (roughly 7 steps)
[c2218bf8c1c349f97406b76088b01d29664bb6cb] Comment 624
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: 62 revisions left to test after this (roughly 6 steps)
[60f95d84dd6683d7dc3d1731e3f6a65dd70e4f1e] Comment 562
running 'python' 'squares.py' '2'
4
Bisecting: 31 revisions left to test after this (roughly 5 steps)
[0cd330767a405c4347a315845965e8a08544d902] Comment 593
running 'python' 'squares.py' '2'
4
Bisecting: 15 revisions left to test after this (roughly 4 steps)
[543ab00ad7edb40861cd0e21b96e38ca1ea42fc0] Comment 609
running 'python' 'squares.py' '2'
4
Bisecting: 7 revisions left to test after this (roughly 3 steps)
[5cb38adfaa6a7c2005a01353c744cadd55eeabc5] Comment 616
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: 3 revisions left to test after this (roughly 2 steps)
[b93d1774d79e8748db566865711634a70d61d9a5] Comment 613
running 'python' 'squares.py' '2'
4
Bisecting: 1 revision left to test after this (roughly 1 step)
[46b7f8909f55c4072cb925f0c77d70bc09fe01df] Comment 614
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)
[f24e909bb869aded2c8bdc2872c86507e0c84d2b] 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'
f24e909bb869aded2c8bdc2872c86507e0c84d2b is the first bad commit
commit f24e909bb869aded2c8bdc2872c86507e0c84d2b
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!