XClose

COMP0023: Research Software Engineering With Python

Home
Menu

Debugging With Git Bisect

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

In [1]:
import os
top_dir = os.getcwd()
git_dir = os.path.join(top_dir, 'learning_git')
os.chdir(git_dir)
In [2]:
%%bash
rm -rf bisectdemo
git clone https://github.com/UCL-RITS/bisectdemo.git
Cloning into 'bisectdemo'...
In [3]:
bisect_dir=os.path.join(git_dir,'bisectdemo')
os.chdir(bisect_dir)
In [4]:
%%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:

In [5]:
%%bash
./breakme.sh > break_output
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

In [6]:
%%bash
python squares.py 2 # Error message
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'
---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
Cell In[6], line 1
----> 1 get_ipython().run_cell_magic('bash', '', 'python squares.py 2 #\xa0Error message\n')

File /opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/IPython/core/interactiveshell.py:2417, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
   2415 with self.builtin_trap:
   2416     args = (magic_arg_s, cell)
-> 2417     result = fn(*args, **kwargs)
   2418 return result

File /opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/IPython/core/magics/script.py:153, in ScriptMagics._make_script_magic.<locals>.named_script_magic(line, cell)
    151 else:
    152     line = script
--> 153 return self.shebang(line, cell)

File /opt/hostedtoolcache/Python/3.8.15/x64/lib/python3.8/site-packages/IPython/core/magics/script.py:305, in ScriptMagics.shebang(self, line, cell)
    300 if args.raise_error and p.returncode != 0:
    301     # If we get here and p.returncode is still None, we must have
    302     # killed it but not yet seen its return code. We don't wait for it,
    303     # in case it's stuck in uninterruptible sleep. -9 = SIGKILL
    304     rc = p.returncode or -9
--> 305     raise CalledProcessError(rc, cell)

CalledProcessError: Command 'b'python squares.py 2 #\xc2\xa0Error message\n'' returned non-zero exit status 1.

Bisecting manually

In [7]:
%%bash
git bisect start
git bisect bad # We know the current state is broken
git switch main
git bisect good # We know the main branch state is OK
status: waiting for both good and bad commits
status: waiting for good commit(s), bad commit known
warning: you are switching branch while bisecting
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Bisecting: 500 revisions left to test after this (roughly 9 steps)
[6107edf08c5cde383884454f950746c8074f5501] Comment 499

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

Stop the bisect process with:

git bisect reset

Solving automatically

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

(NOTE: You don't need to redirect the stderr and stdout (with &>) of git bisect run to a file when running these commands outside a jupyter notebook (i.e., on a shell). This is done here so the errors appears with the right commits)

In [8]:
%%bash
git bisect start
git bisect bad HEAD # We know the current state is broken
git bisect good main # We know main is good
git bisect run python squares.py 2 &> gitbisect.out
cat gitbisect.out
Previous HEAD position was 6107edf Comment 499
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)
[6107edf08c5cde383884454f950746c8074f5501] Comment 499
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: 249 revisions left to test after this (roughly 8 steps)
[9135553ef757cd70ee0e9a03a3e14fbd3192a784] Comment 249
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)
[03f4c7ed4dbd268a6f3f77456613a27fbfc7a02e] Comment 124
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)
[c26e3d965779c3d67dbe5e1697a82deb570bbd19] Comment 62
running  'python' 'squares.py' '2'
4
Bisecting: 31 revisions left to test after this (roughly 5 steps)
[643fcbd33501c6042fa2a39bebb7497f011e1380] Comment 93
running  'python' 'squares.py' '2'
4
Bisecting: 15 revisions left to test after this (roughly 4 steps)
[06b8548a1b97ee162903323ec2517357df1639b1] Comment 109
running  'python' 'squares.py' '2'
4
Bisecting: 7 revisions left to test after this (roughly 3 steps)
[818ba8b3b136beba355e1adb6951fbb3b0339e82] Comment 116
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)
[80f3846397766435093b2739026368ad46d1fb0e] Comment 113
running  'python' 'squares.py' '2'
4
Bisecting: 1 revision left to test after this (roughly 1 step)
[c2a7c53265249df6af6d1f587f0ba04fd0d03873] Comment 115
running  'python' 'squares.py' '2'
4
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[5d14a3759b355026593740b68421faaa72d3c6a6] 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'
5d14a3759b355026593740b68421faaa72d3c6a6 is the first bad commit
commit 5d14a3759b355026593740b68421faaa72d3c6a6
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!