是否可以配置git diff来尊重缩进和语法? 我不是在谈论忽略缩进和空格,而是使用空行,缩进级别和可能的括号,以帮助将旧行与新行匹配。
E.g。 git diff经常切换函数及其docblock,如下所示:
class C {
/**
+ * Goes to the bar.
+ */
+ function bar() {
+ return 'bar';
+ }
+
+ /**
* Gets your foo up to date.
*/
function foo() {
我希望
class C {
+
+ /**
+ * Goes to the bar.
+ */
+ function bar() {
+ return 'bar';
+ }
/**
* Gets your foo up to date.
*/
function foo() {
在这个例子中,它仍然是非常无害的,但有些例子表明函数及其docblock由于贪婪和天真的差异实现而被撕裂。
注意:我已在*.php diff=php
中配置了~/.gitattributes
。
/**
- * @var int
+ * @param string $str
*/
答案 0 :(得分:4)
我不知道如何在git中单独执行此操作,但至少有一个商业工具(即花钱)处理这类问题,称为SemanticMerge。
它可以处理很多很酷的案例,并且支持C#,Java和部分C.你可以配置git将它用作合并工具。
(我没有附属。)
答案 1 :(得分:1)
首先使用更复杂的差异算法,例如:
git config --global diff.algorithm histogram
然后还有诸如https://github.com/GumTreeDiff/gumtree之类的语义差异工具,其算法也已在clang-diff中实现:https://github.com/krobelus/clang-diff-playground