git diff algorithm that does not rip functions apart? (language-aware diff)
Asked Answered
H

3

52

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
    */
Howlyn answered 11/6, 2014 at 12:15 Comment(10)
I have a suspicion the answer will be in which diff algorithm you pick, but I couldn't find one that worked in the way you want.Enrich
How do you pick an algorithm?Howlyn
Does --patience have anything to do with it?Howlyn
Good info toward an answer here: fabiensanglard.net/git_code_review/diff.php #4045517Repeater
I just tested this out using the --patience option, but it doesn't work in this case (--patience will sometimes work, but not always).Predetermine
This is a similar question, so this might be a duplicate.Predetermine
To help people answer this, here is a list of resources that I created.Predetermine
so, it seems the current answer is either "don't know" or "git can't do that (yet).". The similar question may go in the same direction, although the question itself is not directly focused on a git algorithm, even dismisses the --patience as "seems to just be for git diff". And I like my question better :)Howlyn
Maybe the first answer in the following topic will explain some of the mechanics going on: How to apply diff rules of the languages in gitattributesLittle
Interesting search keyword: "semantic diff". See also https://mcmap.net/q/188369/-semantic-diff-utilities-closed. I was not necessarily looking for a 100% language-aware algorithm, but if that's what it takes then why not.Howlyn
N
5

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.)

Naseby answered 11/7, 2014 at 11:44 Comment(5)
Still it would be nice to have something in git that is based on indentation levels or some other kind of cheating, that pretends to be language-aware but really is not.Howlyn
@Howlyn You can probably set up SemanticMerge as a git difftool so that it would be "in git"Spurn
@donquixote: Googling around a bit it looks like you can use SemanticMerge as a git diff tool (algorithm) - users.semanticmerge.com/documentation/how-to-configure/…Spurn
@donquixote: once you set it up like in the link above all you need to do is change your own behaviour to type git difftool instead of git diffSpurn
It seems this tool has been retired and made unavailable by its owner. Is there another similar tool?Kaitlinkaitlyn
S
2

First of all use a more sophisticated diff algorithm like:

git config --global diff.algorithm histogram

Then there are also semantic diff tools like https://github.com/GumTreeDiff/gumtree whose algorithm has also been implemented in clang-diff: https://github.com/krobelus/clang-diff-playground

Stralka answered 13/11, 2018 at 1:3 Comment(0)
R
0

Please look at I-Compare It is curly braces aware and more. It takes some time to run but the result is accurate

Resect answered 6/3 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.