53

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
    */
10
  • 1
    How do you pick an algorithm? Commented Jun 11, 2014 at 12:55
  • 2
    Does --patience have anything to do with it? Commented Jun 13, 2014 at 23:25
  • 2
    Good info toward an answer here: fabiensanglard.net/git_code_review/diff.php stackoverflow.com/questions/4045017/… Commented Jun 19, 2014 at 3:13
  • 2
    I just tested this out using the --patience option, but it doesn't work in this case (--patience will sometimes work, but not always). Commented Jun 22, 2014 at 2:36
  • 1
    This is a similar question, so this might be a duplicate. Commented Jun 22, 2014 at 2:37

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@donquixote You can probably set up SemanticMerge as a git difftool so that it would be "in git"
@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/…
@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 diff
It seems this tool has been retired and made unavailable by its owner. Is there another similar tool?
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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.