Tracking down unused variables with Pyflakes and git bisect

综合技术 2012-09-18

I was working on a Python project when I noticed a variable that was defined but never used. Does this indicate that some important code was accidentally deleted? Or was there just a minor oversight during refactoring?

To answer this question, I wanted to see the Git commit which removed the last use of this variable. This sounds like a job for

git bisect

. And because
can detect unused variables, the whole process is completely automatic.

I made a guess that the
became unused sometime within the past two weeks. Then I told Git to consider a commit "good" if Pyflakes's list of complaints does not mention

$ git bisect start master master@{'2 weeks ago'}
Already on 'master'
Bisecting: 150 revisions left to test after this (roughly 7 steps)
[066327622129dbe863f6e2fc4746ff9e869bd049] Synthesize gravity

$ git bisect run bash -c '! pyflakes | grep mystery_variable'
running bash -c ! pyflakes | grep mystery_variable
Bisecting: 75 revisions left to test after this (roughly 6 steps)
[d3a5665eff478cccfb86d994a8fc289446325fbf] Model object components
running bash -c ! pyflakes | grep mystery_variable
Bisecting: 37 revisions left to test after this (roughly 5 steps)
[6ddcfbf27a1a4548acf972a6b817e485743f6bd9] Time-compress simulator clock

running bash -c ! pyflakes | grep mystery_variable
9c2b2f006207ae9f274f9182efeb3e009d18ed04 is the first bad commit
commit 9c2b2f006207ae9f274f9182efeb3e009d18ed04
Author: Ben Bitdiddle 
Date:   Fri Sep 14 01:38:31 2012 -0400

    Reticulate splines

Now I can examine this commit and see what happened.

责编内容by:main is usually a function (源链)。感谢您的支持!


When You “Git” in Trouble: a Version Control Story... Thank you for laughing at my extremely funny title. But do you know what’s not ...
Issues #2 Jun 12, 2017 Linux Bridge - how it works Linux bridge is a layer 2 virt...
Lessons Learned Teaching Git (Over and over and over) Up until last month, and for a little over a year...
记录一次Git的rerset操作和恢复续-恢复远程分支... 之前有一篇文章记录了Git的 reset 操作: 记录一次Git的rerset操作和恢复 ...
基于 AspNetCore 的 Git HTTP 服务器 关于 Git HTTP 服务器是代码托管服务最重要的组件之一,Git HTTP 服务器将 HTTP 请求的数据写入到 git-upload-pack/git...