git diff 算法不会将函数分开? (语言感知差异)

问题描述 投票:0回答:3

是否可以配置 git diff 以尊重缩进和语法? 我不是在谈论忽略缩进和空格,而是使用空行、缩进级别和可能的括号,以帮助将旧行与新行匹配。

例如git diff 通常会切入函数及其文档块,如下所示:

 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() {

在这个例子中,它仍然是无害的,但是有一些例子,由于贪婪和幼稚的 diff 实现,函数及其文档块确实被撕裂了。

注意:我已经在

*.php diff=php
中配置了
~/.gitattributes

编辑:另一个例子: 这里 git diff 将属性 docblock 与方法 docblock 混合在一起:

   /**
-   * @var int
+   * @param string $str
    */
git language-agnostic diff semantic-diff
3个回答
5
投票

我不知道如何单独在 git 中做到这一点,但是至少有一个商业工具(即它需要花钱)可以处理此类问题,称为 SemanticMerge

它可以处理很多很酷的情况,并且支持 C#、Java 和部分 C。您可以配置 git 将其用作合并工具。

(我不隶属。)


2
投票

首先使用更复杂的 diff 算法,例如:

git config --global diff.algorithm histogram

还有语义差异工具,如https://github.com/GumTreeDiff/gumtree,其算法也已在 clang-diff 中实现:https://github.com/krobelus/clang-diff-playground


0
投票

请看我-比较 它具有大括号意识等等。运行需要一些时间,但结果很准确

© www.soinside.com 2019 - 2024. All rights reserved.