耐心算法与默认的git差异算法有什么不同,什么时候我想使用它?
你可以阅读Bram Cohen的一篇文章,他是耐心差异算法的作者,但我发现这篇博客文章很好地总结了耐心差异算法:
相反,Patience Diff将精力集中在低频高内容的线条上,这些线条作为文本中重要内容的标记或签名。它的核心仍然是一个基于lcs的diff,但有一个重要的区别,因为它只考虑签名行的最长公共子序列: 找出所有在两边只出现一次的行,然后对这些行进行最长公共子序列,将它们匹配起来。
什么时候你应该使用不同的耐心?根据Bram的说法,耐心在这种情况下是有好处的:
The really bad cases are ones where two versions have diverged dramatically and the developer isn't being careful to keep patch sizes under control. Under those circumstances a diff algorithm can occasionally become 'misaligned' in that it matches long sections of curly brackets together, but it winds up correlating the curly brackets of functions in one version with the curly brackets of the next later function in the other version. This situation is very ugly, and can result in a totally unusable conflict file in the situation where you need such things to be presented coherently the most.
耐心差分算法是一种较慢的差分算法,在某些情况下显示出更好的结果。
假设你有以下文件签入git:
.foo1 {
margin: 0;
}
.bar {
margin: 0;
}
现在我们重新排序部分,并添加一个新的行:
.bar {
margin: 0;
}
.foo1 {
margin: 0;
color: green;
}
默认的diff算法声称节标题已经改变:
$ git diff --diff-algorithm=myers
diff --git a/example.css b/example.css
index 7f1bd1e..6a64c6f 100755
--- a/example.css
+++ b/example.css
@@ -1,7 +1,8 @@
-.foo1 {
+.bar {
margin: 0;
}
-.bar {
+.foo1 {
margin: 0;
+ color: green;
}
而耐心差异显示的结果可以说是更直观的:
$ git diff --diff-algorithm=patience
diff --git a/example.css b/example.css
index 7f1bd1e..6a64c6f 100755
--- a/example.css
+++ b/example.css
@@ -1,7 +1,8 @@
-.foo1 {
- margin: 0;
-}
-
.bar {
margin: 0;
}
+
+.foo1 {
+ margin: 0;
+ color: green;
+}
这里对主观差异质量进行了很好的讨论,git 2.11进一步探索了差异启发式。
请注意,耐心差异算法仍然有一些已知的病理病例。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别