Bisect 基于二分查找算法。给定一个有序的元素序列,它会返回要你要查找的元素的序号(或者告诉你该元素是否在序列中)。它基于如下的不变式:当你对比完一个元素,你就能根据比较的结果抛弃掉它之前或者之后的所有元素(从而大大缩小下次查找的范围)。如果你有去过图书馆,那你可能注意到了每个专区内的书籍都是按作者姓名来排序的。比方说你要找的一本书的作者的姓为 Martin,而你刚刚看到某个书架上的最后一本书的作者的姓是 F 开头的,那么你就能确定你要找的书一定放在后面的某个书架上。依此方法继续下次,你就能快速的缩小搜索范围直到找到你要的书为止。
二分查找法亦是如此。如果你想在有 n 个元素的序列(有序的)中查找元素 x,你挑出第 n/2 个元素并将其与元素 x 比较。如果 x 大,那么就对从 n/2+1 到 n 的子序列重复上述步骤,反之,就对从 1 到 n/2-1 的子序列重复上述步骤, 这样一直递归下去。这里就有一个 关于此算法的有趣的演示。二分查找算法所需的比较次数通常都比你傻傻的去拿序列中的每个元素与 x 比较所需的比较次数少得多。事实证明在有序序列中查找元素,二分查找更快更有用。
if [[ `grep 1013 file.txt` ]]; then exit 1 else exit 0 fi保存上述代码为 test.sh,并赋予它执行权限。接着在给 bisect 命令指定了初始的好/坏提交点之后,你只需要运行:git bisect run ./test.sh running ./ola.sh Bisecting: 255 revisions left to test after this (roughly 8 steps) [a01ba83f3500b48da97c5f5c33052623aaa4161a] added 768 running ./ola.sh Bisecting: 127 revisions left to test after this (roughly 7 steps) [4a4a668bf3363d09af5fd1906bc4272aacdb4495] added 896 running ./ola.sh Bisecting: 63 revisions left to test after this (roughly 6 steps) [9059c5b8b898159e8d1d797bff3b1febd1fd6a1c] added 960 running ./ola.sh Bisecting: 31 revisions left to test after this (roughly 5 steps) [0c844d0b33ef297b742206ebc293f4925705b083] added 992 running ./ola.sh Bisecting: 15 revisions left to test after this (roughly 4 steps) [0ee17eb17bd96b321a01c73eb13a8929a68b1239] added 1008 running ./ola.sh Bisecting: 7 revisions left to test after this (roughly 3 steps) [dfb1e71736dcfffa2a30aecd7299f45f757c057e] added 1016 running ./ola.sh Bisecting: 3 revisions left to test after this (roughly 2 steps) [6e6d08c374df5162fed65fed82859b69f86b936e] added 1012 running ./ola.sh Bisecting: 1 revision left to test after this (roughly 1 step) [1d23b7045a8accd254efa859d7fc66f1f58a59f0] added 1014 running ./ola.sh Bisecting: 0 revisions left to test after this (roughly 0 steps) [458eab0eb8d808e16d98ec7039a7c53855dd9ed6] added 1013 running ./ola.sh 458eab0eb8d808e16d98ec7039a7c53855dd9ed6 is the first bad commit commit 458eab0eb8d808e16d98ec7039a7c53855dd9ed6 Author: Rodrigo Flores <mail@rodrigoflores.org> Date: Tue Oct 21 22:31:05 2014 -0200