不区分功能的git diff算法? (语言意识差异) [英] git diff algorithm that does not rip functions apart? (language-aware diff)

查看:308
本文介绍了不区分功能的git diff算法? (语言意识差异)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



EG git diff通常会切换函数和docblock,如下所示:

  class C {

/ * *
+ *去酒吧。
+ * /
+函数bar(){
+ return'bar';
+}
+
+ / **
*获取您的foo最新信息。
* /
函数foo(){

当我更喜欢 p>

  class C {
+
+ / **
+ *去吧。
+ * /
+函数bar(){
+ return'bar';
+}

/ **
*获取您的foo最新信息。
* /
函数foo(){

在这个例子中,它仍然相当无害,但有一些例子,功能和它们的docblock由于贪婪和天真的diff实现而被真正的撕开。

注意:我已经配置了 *。php diff = php 〜/ .gitattributes



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

  / ** 
- * @var int
+ * @param string $ str
$ /


解决方案

我不知道如何在git中做到这一点,但至少有一个商业工具(即它花钱)处理这种问题,称为 SemanticMerge



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



(我不属于)。


Is it possible to configure git diff to respect indentation and syntax? I am not talking about ignoring indentation and spaces, but rather to use blank lines, indentation levels and possibly brackets, to help matching the old lines to new lines.

E.g. git diff often cuts through functions and their docblock, like this:

 class C {

   /**
+   * Goes to the bar.
+   */
+  function bar() {
+    return 'bar';
+  }
+
+  /**
    * Gets your foo up to date.
    */
   function foo() {

When I would prefer

 class C {
+
+  /**
+   * Goes to the bar.
+   */
+  function bar() {
+    return 'bar';
+  }

   /**
    * Gets your foo up to date.
    */
   function foo() {

In this example it is still quite harmless, but there are examples where functions and their docblock are really ripped apart due to the greedy and naive diff implementation.

Note: I already configured *.php diff=php in ~/.gitattributes.

EDIT: Another example: Here git diff mixes a property docblock with a method docblock:

   /**
-   * @var int
+   * @param string $str
    */

解决方案

I do not know how to do that in git alone, but there is at least one commercial tool (i.e. it costs money) which deals with that kind of problems, called SemanticMerge.

It can handle quite a lot of cool cases, and supports C#, Java, and partially C. You can configure git to use it as merge tool.

(I'm not affiliated.)

这篇关于不区分功能的git diff算法? (语言意识差异)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆