git bisect

Git bisect is a fantastic tool that can help make debugging a breeze. But very few people use it actively.

Git bisect是一个很棒的工具,可以帮助您轻松进行调试。 但是很少有人积极使用它。

In this quick article, I will showcase how git bisect can easily point out the where your bugs lie.

在这篇简短的文章中,我将展示git bisect如何轻松指出您的错误所在。

But first, lets talk about...

但首先,让我们谈谈...

台达调试 (Delta debugging)

Delta debugging is the process of completing many debugging steps and in each one your goal is to eliminate half the "problem". You can think of it as the binary search of debugging. Or as Andreas Zeller (who coined the term) said:

Delta调试是完成许多调试步骤的过程,在每一步中,您的目标是消除一半的“问题”。 您可以将其视为调试的二进制搜索。 或正如Andreas Zeller (创造了该术语)所说:

Delta Debugging automates the scientific method of debugging. The basic idea of the scientific method is to establish a hypothesis on why something does not work. You test this hypothesis, and you refine or reject it depending on the test outcome. When debugging, people are doing this all the time. Manually. Delta Debugging automates this process.

Delta Debugging使自动化的科学调试方法自动化。 科学方法的基本思想是建立关于某些东西为什么不起作用的假设。 您测试此假设,然后根据测试结果对其进行完善或拒绝。 调试时,人们一直在这样做。 手动。 Delta调试可自动执行此过程。

Git bisect is how we apply delta debugging with Git.

Git bisect是我们在Git中应用增量调试的方式。

Let's assume we have a bug and we try to find the root cause. In every step of our investigation for a solution, we eliminate half the solution space. Configuration, code, input...anything. Here's an example to make it clearer.

假设我们有一个错误,我们试图找到根本原因。 在研究解决方案的每一步中,我们消除了一半的解决方案空间。 配置,代码,输入...任何内容。 这是一个使它更清楚的例子。

Git二等分示例 (Git bisect example)

First, we need to initialize a repository to track our work.

首先,我们需要初始化一个存储库以跟踪我们的工作。

mkdir test_git_bisect && cd test_git_bisect && git init

Let's say we are going to make a script that gets an epoch and converts it to

假设我们要制作一个脚本,该脚本获取一个时期并将其转换为

datetime

We do that by using an input file (named epochs.txt) that should contain only epochs.

我们通过使用输入文件(名为epochs.txt) 应该只包含时期。

Please note, that in order to run a git bisect smoothly, we need to have quite a few commits.

请注意,为了顺利运行git bisect ,我们需要进行多次提交。

The python script parse_epochs.py we will use here is nothing special.

我们将在此处使用的python脚本parse_epochs.py没什么特别的。

from time import localtime, strftimewith open('epochs.txt', 'r') as handler:epochs = handler.readlines()for epoch in epochs:current_datetime = strftime('%Y-%m-%d %H:%M:%S', localtime(int(epoch)))print(current_datetime)

Let's commit the first change:

让我们进行第一个更改:

git add . && git commit -m "Created epoch parser"

git add . && git commit -m "Created epoch parser"

then create the input:

然后创建输入:

for i in {1..100}; do   sleep 3;   date +%s >> epochs.txt; done

for i in {1..100}; do sleep 3; date +%s >> epochs.txt; done

This is essentially all epochs from the time we started the script (plus 3 seconds) until five minutes later, with a 3 second step.

从启动脚本的时间(加上3秒)到五分钟后的3秒,这基本上就是所有时期。

Again we commit the change:

我们再次提交更改:

git add . && git commit -m "Generated the first version of input"

git add . && git commit -m "Generated the first version of input"

If we now run the initial script, we get all inputs parsed to dates:

如果现在运行初始脚本,则将所有输入解析为日期:

$ python3 parse_epochs.py
2020-07-21 16:08:39
2020-07-21 16:10:40
2020-07-21 16:10:43
2020-07-21 16:10:46
2020-07-21 16:10:49
2020-07-21 16:10:52
...

Let's amend the input now to make it faulty:

现在让我们修改输入以使其出错:

echo "random string" >> epochs.txt

and commit again:

然后再次提交:

git add . && git commit -m "Added faulty input"

For the sake of entropy, to make the example more complex, let's add more faulty inputs - commits.

为了熵,为了使示例更复杂,让我们添加更多错误的输入-提交。

echo "This is not an epoch" >> epochs.txt
&& git add . && git commit -m "Added faulty input v2"
echo "Stop this, the script will break" >> epochs.txt
&& git add . && git commit -m "Added faulty input v3"

Here is the commit log we have created:

这是我们创建的提交日志:

$ git log --pretty=format:"%h - %an, %ar : %s"
b811d35 - Periklis Gkolias, 2 minutes ago: Added faulty input v3
dbf75cd - Periklis Gkolias, 2 minutes ago: Added faulty input v2
cbfa2f5 - Periklis Gkolias, 8 minutes ago: Added faulty input
d02eae8 - Periklis Gkolias, 20 minutes ago: Generated first version of input
a969f3d - Periklis Gkolias, 26 minutes ago: Created epoch parser

If we run the script again, it will obviously fail with the following error:

如果再次运行该脚本,显然它将失败,并显示以下错误:

Traceback (most recent call last):File "parse_epochs.py", line 6, in <module>current_datetime = strftime('%Y-%m-%d %H:%M:%S', localtime(int(epoch)))
ValueError: invalid literal for int() with base 10: 'random string\n'

Looks like we need git bisect to fix this. To do so we need to start the investigation:

看来我们需要git bisect来解决这个问题。 为此,我们需要开始调查:

git bisect start

git bisect start

and mark one commit as bad (usually the last one) and one commit as good. This would be the second commit when we generated the input:

并将一个提交标记为不好(通常是最后一个),将一个提交标记为好。 这将是我们生成输入时的第二次提交:

git bisect bad b811d35 && git bisect good d02eae8

After that, git bisect will split the history between the good and the bad commit in two. You can see that by doing git bisect visualize to see the commits that are considered the culprits, and

之后,git bisect将历史记录分为好提交和坏提交两部分。 您可以通过git bisect visualize来查看被视为罪魁祸首的提交,然后

git show

to print the currently checked out one, in our case this one:

打印当前已签出的一个,在我们的示例中为:

dbf75cd

If we run the script it will still fail. So we mark the current commit as bad:

如果我们运行脚本,它将仍然失败。 因此,我们将当前提交标记为错误:

git bisect bad dbf75cd

git bisect bad dbf75cd

It is worth mentioning the output of Git in that case:

在这种情况下,值得一提的是Git的输出:

git bisect bad dbf75cd
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[cbfa2f5f52b7e8a0c3a510a151ac7653377cfae1] Added faulty input

Git knows we are almost there. Yay!

Git知道我们快到了。 好极了!

If we run the script again, it of course fails. And if we mark it as bad, Git says:

如果我们再次运行该脚本,则它当然会失败。 如果我们将其标记为不良,Git说:

git bisect bad cbfa2f5
cbfa2f5f52b7e8a0c3a510a151ac7653377cfae1 is the first bad commit

By then, you may either fix the bug or contact whomever committed the bad code/input/configuration. Here is how to get the details:

届时,您可以修复错误或与提交错误代码/输入/配置的人员联系。 这是获取详细信息的方法:

$ git show -s --format='%an, %ae' cbfa2f5
Periklis Gkolias, myemail@domain.com

结论 (Conclusion)

Thank you for reading this article. Feel free to share your thoughts on this great tool.

感谢您阅读本文。 随时分享您对这个出色工具的看法。

翻译自: https://www.freecodecamp.org/news/how-git-bisect-makes-debugging-easier/

git bisect

git bisect_Git Bisect如何使调试更容易相关推荐

  1. c++ console 取实时输入_灵活使用 console 让 js 调试更简单

    译者:前端小智 原文: https://medium.com/@mattburgess/beyond-console-log-2400fdf4a9d8https://medium.freecodeca ...

  2. console application_灵活使用 console 让 js 调试更简单

    摘要: 玩转console. 原文:灵活使用 console 让 js 调试更简单 作者:前端小智 Fundebug经授权转载,版权归原作者所有. Web 开发最常用的高度就是 console.log ...

  3. js空格占位符_灵活使用 console 让 js 调试更简单

    Web开发最常用的高度就是 console.log ,虽然 console.log 占有一席之地,但很多人并没有意识到 console 本身除了基本 log 方法之外还有很多其他方法.适当使用这些方法 ...

  4. 灵活使用 console 让 js 调试更简单

    译者:前端小智 原文: https://medium.com/@mattburgess/beyond-console-log-2400fdf4a9d8 https://medium.freecodec ...

  5. 使嵌入式系统调试更容易:有用的硬件和软件提示

    使嵌入式系统调试更容易:有用的硬件和软件提示 Making embedded system debug easier: useful hardware & software tips 嵌入式系 ...

  6. nginx反向代理解决跨域问题,使本地调试更方便

    nginx反向代理解决跨域问题,使本地调试更方便 参考文章: (1)nginx反向代理解决跨域问题,使本地调试更方便 (2)https://www.cnblogs.com/gwf93/p/102951 ...

  7. Git笔记(34) 调试

    Git笔记(34) 调试 1. 文件标注 2. 二分查找 1. 文件标注 如果在追踪代码中的一个 bug,并且想知道是什么时候以及为何会引入 文件标注通常是最好用的工具 它能 显示任何文件中每行最后一 ...

  8. [转]九个Console命令,让js调试更简单

    转自:九个Console命令,让js调试更简单 一.显示信息的命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!DOCTYPE html> <html ...

  9. 三插头内部结构图_三方面维护硬度计才能使寿命更长久

    硬度计是光机电一体化的高新技术产品.与其他精密仪器一样,定期的保养维护少不了.轻则导致操作不顺畅,或试验结果有偏差;重则机器损坏,返厂维修耽误时间又费钱.如何才能使用寿命才能更长?现在为大家介绍一下在 ...

  10. 自定义linux命令工具栏,如何自定义Mac终端并使其更有用!

    原标题:如何自定义Mac终端并使其更有用! 终端应用程序是您在macOS中访问命令行的网关.它提供了带有外壳程序或命令解释器的接口,该接口可接收您的命令并调用其他命令来执行例行任务和复杂任务.如果您只 ...

最新文章

  1. 常见JSP中文乱码例子及其解决方法
  2. 应用Strong Name保存.NET应用程序集
  3. [Java基础]字符缓冲流
  4. 前端学习(1284):node开发概述
  5. 使用sshpass借助scp自动输入密码传输一个文件夹下的全部内容
  6. 70. 爬楼梯(JavaScript)
  7. 如何通过 Shell 监控异常等待事件和活跃会话
  8. Changing the IP Address of a Domain Controller
  9. atitit.避免NullPointerException 总结and 最佳实践 o99
  10. xy坐标转换经纬度C语言,经纬度与坐标转换公式
  11. linux ibm多路径软件,如何安装配置IBM存储多路径软件
  12. 基于matlab算法的可靠度分析,参考基于matlab算法的可靠度分析
  13. 清明节出游图鉴:热门目的地的网络舆论与口碑分析
  14. 解决“由于文件许可权错误 word无法完成保存”问题
  15. 计算机三级网络技术(补充)
  16. flash builder (fb) 与flash professional cs6(fla) 联合调试
  17. HTML-背景和图片
  18. [cocos2d-x] -- Cocos2d-x简介
  19. jvisualvm监控java,JVM-jvisualvm运行监控工具使用
  20. React取色器组件

热门文章

  1. 独家:程序员必备Java API和类搜索辅助工具发布
  2. 提供资产证券化投行业务的“点石金融”能在中国点石成金吗?
  3. 阴阳师1月服务器维护,阴阳师1月15日更新维护公告 鬼童丸降临平安京
  4. rtx服务器限制文件传输,rtx 服务器 文件传输 配置
  5. unity 使用像素实现墙面子弹留孔效果(给已有贴图模型叠加贴图)
  6. php里ajax提交form表单图片上传,PHPAJAXFORM提交图片上传并显示图片源码
  7. html闪星星特效,jquery和canvas炫酷星星闪烁特效插件
  8. CSS:全屏星星闪烁动画CSS3特效源码
  9. uni-app入门教程
  10. DeepLearning论文阅读笔记(一):Cyclical Learning Rates for Training Neural Networks(CLR)