XClose

COMP0233: Research Software Engineering With Python

Home
Menu

Debugging With Git Bisect

NOTE: using bash/git commands is not fully supported on jupyterlite yet (due to single thread/process restriction), and the cells below might error out on the browser (jupyterlite) version of this notebook

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-ARC-RSEing-with-Python/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 "/home/runner/work/rsd-engineeringcourse/rsd-engineeringco
urse/ch00git/learning_git/bisectdemo/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.12.12/x64/lib/python3.12/site-packages/IPython/core/interactiveshell.py:2572, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
   2570 with self.builtin_trap:
   2571     args = (magic_arg_s, cell)
-> 2572     result = fn(*args, **kwargs)
   2574 # The code below prevents the output from being displayed
   2575 # when using magics with decorator @output_can_be_silenced
   2576 # when the last Python token in the expression is a ';'.
   2577 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File /opt/hostedtoolcache/Python/3.12.12/x64/lib/python3.12/site-packages/IPython/core/magics/script.py:160, in ScriptMagics._make_script_magic.<locals>.named_script_magic(line, cell)
    158 else:
    159     line = script
--> 160 return self.shebang(line, cell)

File /opt/hostedtoolcache/Python/3.12.12/x64/lib/python3.12/site-packages/IPython/core/magics/script.py:348, in ScriptMagics.shebang(self, line, cell)
    343 if args.raise_error and p.returncode != 0:
    344     # If we get here and p.returncode is still None, we must have
    345     # killed it but not yet seen its return code. We don't wait for it,
    346     # in case it's stuck in uninterruptible sleep. -9 = SIGKILL
    347     rc = p.returncode or -9
--> 348     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)
[aa1ccf65f67e3541693e2af6f79c8995d9314cea] 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

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 aa1ccf6 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)
[aa1ccf65f67e3541693e2af6f79c8995d9314cea] Comment 500
running 'python' 'squares.py' '2'
4
Bisecting: 250 revisions left to test after this (roughly 8 step
s)
[0d2d583a69feda2545afd5a50eda5b947f63a970] Comment 750
running 'python' 'squares.py' '2'
4
Bisect
ing: 125 revisions left to test after this (roughly 7 steps)
[76e6a7a616fd671dbb3c5b18c3ae3aec2f286c
00] Comment 874
running 'python' 'squares.py' '2'
Traceback (most recent call last):
  File "/home/r
unner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch00git/learning_git/bisectdemo/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)
[dc2db94a4976bbd2648d8b438d17b34a2b804e8a] Comment 812
running 'python' 'squares.py' '2'
4
Bisectin
g: 31 revisions left to test after this (roughly 5 steps)
[55bafa75847fc4df53c4d52586d3241b87980012]
 Comment 843
running 'python' 'squares.py' '2'
4
Bisecting: 15 revisions left to test after this (ro
ughly 4 steps)
[3cba640b5828cbec9cde879e07c926e8a14299ef] Comment 858
running 'python' 'squares.py' 
'2'
Traceback (most recent call last):
  File "/home/runner/work/rsd-engineeringcourse/rsd-engineeri
ngcourse/ch00git/learning_git/bisectdemo/squares.py", line 9, in <module>
    print(integer**2)
    
      ~~~~~~~^^~
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
Bisecting: 
7 revisions left to test after this (roughly 3 steps)
[46580b050569bfc42e4be25074950166e22663a3] Bre
aking argument type
running 'python' 'squares.py' '2'
Traceback (most recent call last):
  File "/ho
me/runner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch00git/learning_git/bisectdemo/squares.p
y", line 9, in <module>
    print(integer**2)
          ~~~~~~~^^~
TypeError: unsupported operand ty
pe(s) for ** or pow(): 'str' and 'int'
Bisecting: 3 revisions left to test after this (roughly 2 ste
ps)
[0acc8ebcd98ce4fc67d50f877080fd46cfd52364] Comment 847
running 'python' 'squares.py' '2'
4
Bisec
ting: 1 revision left to test after this (roughly 1 step)
[aaabed838c1a8b77893a2cbdec5dae206cc792ff]
 Comment 849
running 'python' 'squares.py' '2'
4
Bisecting: 0 revisions left to test after this (rou
ghly 0 steps)
[f67c0287525fe7db04fa9622dd50831f0ab4b140] Comment 850
running 'python' 'squares.py' '
2'
4
46580b050569bfc42e4be25074950166e22663a3 is the first bad commit
commit 46580b050569bfc42e4be25
074950166e22663a3
Author: Shawn Siefkas <shawn.siefkas@meredith.com>
Date:   Thu Nov 14 09:23:55 201
3 -0600

    Breaking argument type

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

Boom!