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, strftime
with 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