Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context-aware merge?

Is there any diff/merge tool for programming languages, that works in a syntax-aware way (like XML Diff Tool), doing more than compare line-by-line (and optionally ignoring whitespace).

I'm interested in a program actually following the language syntax and delimeters, suggesting changes without breaking syntactic correctness, or bundling statements separated over multiple lines. Example behavior would be:

*upon finding an if(){ which introduces an extra nesting level automatically bundle the closing brace } several lines below with it.)

*keep matching syntax elements together, avoid silliness like removing a block tends to create:

 int function_A()
 { 
     int ret;
     ret = something;
     ret += something_else;

      return ret;
  }

  int function_B()
  { 
     if(valid)
     {
         int ret;
         ret = something;
         ret += something_else;

          return ret;
      }

       else return -1;
  }

Personally, I'd love to find software capable of handling C++ syntax, but knowing about solutions for other languages would be interesting too.

like image 542
SF. Avatar asked Feb 21 '11 15:02

SF.


2 Answers

Semantic Merge.
Languages supported, from the website:

We started with C# and Vb.net, then added Java. Now C is already supported and then we’ll focus on C++, Objective-C and JavaScript, depending on your feedback

like image 158
Myster Avatar answered Nov 13 '22 16:11

Myster


While KDiff3 does not compare syntax elements in a grammar context, it does have a higher granularity than "the whole line changed", and it will highlighting exactly what parts within a line that is changed.

And in my experience it has a very good algorithm for detecting changes. Given your example above, it correctly compares function_A and function_B out of the box:

Comparision of function_A and function_B

And even so, should the algorithm fail to match what you want, for instance like the following:

Comparision of old and new function_A

you can always override manually by placing sync marks where you want to have it perform the comparision.

Alternative 1:

Comparision of old and new function_A with sync1

Alternative 2:

Comparision of old and new function_A with sync2

like image 14
hlovdal Avatar answered Nov 13 '22 16:11

hlovdal