耐心算法与默认的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分支——unset-upstream来修复?
- Windows git“警告:LF将被CRLF取代”,这是警告尾巴向后吗?
- git中的哈希冲突
- git可以自动在空格和制表符之间切换吗?
- Git暂存文件列表
- 如何将git配置存储为存储库的一部分?
- 如何修改GitHub拉请求?
- 如何在Github和本地删除最后n次提交?
- 我如何调试git/git-shell相关的问题?
- 错误:无法使用rebase进行拉取:您有未分阶段的更改
- Git隐藏未缓存:如何把所有未分期的变化?
- 真实的恶魔
- 如何从另一个分支获得更改
- Git:权限被拒绝(publickey)致命-无法从远程存储库读取。克隆Git存储库时
- git reflog和log有什么区别?