Small Tools: Bisect

2018-02-23 11:59 - Programming

Git provides a great bisect tool. When something has been broken, it will automatically help you find out how and why, by bisecting (splitting into halves) the source control history, incrementally narrowing down on changes. I've sometimes got situations where I'd like to do a similar operation on something else that's not git history, but is instead numbered. A while ago I threw together a python script to help me do this:

#!/usr/bin/env python

def main(argv):
  if len(argv) != 3:
    print "Usage: <good> <bad>"
  good = int(argv[1])
  bad = int(argv[2])

  while True:
    if bad == good + 1:
      print "First bad:", bad

    totry = ((bad - good) / 2) + good
    inp = raw_input('At %s [g/b]: ' % totry)

    if 'g' == inp:
      good = totry
    elif 'b' == inp:
      bad = totry
      print "I don't understand."

if __name__ == '__main__':
  import sys

Save this in your path, mark it executable, and run it with two arguments: the (smaller) number which was good and the (larger) number that is now bad. It will find the midpoint, you test that number and tell it good or bad and it repeats until you've tested immediate neighbors, indicating the first number which was bad: the one that introduced the problem.


No comments!

Post a comment:

  If you do not have an account to log in to yet, register your own account. You will not enter any personal info and need not supply an email address.

You may use Markdown syntax in the comment, but no HTML. Hints:

If you are attempting to contact me, ask me a question, etc, please send me a message through the contact form rather than posting a comment here. Thank you. (If you post a comment anyway when it should be a message to me, I'll probably just delete your comment. I don't like clutter.)