Frage

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.

War es hilfreich?

Lösung

Beyond Compare does some of what you're asking. It doesn't maintain syntactical correctness or compare language blocks at a time, but it can do the following:

  • Some understanding of language syntax, so it can do syntax highlighting of compared files, and it can also recognize and optionally ignore unimportant differences (like comments, including multiline comments).
  • Support for using external conversion programs for loading and saving data. Out of the box, it supports using this to prettify XML and HTML before comparing it. You could set up GNU Indent to standardize syntax before comparing two C files.
  • Optional line weights to let you give a higher weight to matching, e.g., closing braces. I've not tried this feature.
  • Replacements, to ignore for a single session every place where old_variable_name on the left was replaced with new_variable_name on the right.

It's by far the best diff-and-merge tool that I've used. It's also cross platform, cheap ($30 for standard, $50 for pro), and has a very generous evaluation period, so it's worth a try.

Andere Tipps

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

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

Sounds like you'd be interested in Bram Cohen's (BitTorrent creator) Patience Diff algorithm (which is used in the bazaar version control system).

See The diff problem has been solved and especially Patience Diff Advantages:

Excerpt from second link:

Another advantage of patience diff is that it frequently doesn't match lines which just plain shouldn't match. For example, if you've completely rewritten a section of code it shouldn't match up the blank lines in each version, as this example shows. Finally, there's this example:

 void func1() {
     x += 1
 }

+void functhreehalves() {
+    x += 1.5
+}
+
 void func2() {
     x += 2
 }

Which is straightforward and obvious, but frequently diff algorithms will interpret it like this:

 void func1() {
     x += 1
+}
+
+void functhreehalves() {
+    x += 1.5
 }

 void func2() {
     x += 2
 }

See our SmartDifferencer tools.

SmartDifferencers are language specific, driven by production quality language parsers, build ASTs, and compare the trees. This makes them completely indepedent of text layout and intervening comments; remarkably, they are immune to changes in the text of literals (radix, move decimal point+change exponent, different escape sequences) if the actual value represented by the literal isn't different. The result is reported in language syntax terms, and plausible editing actions (move, copy, insert, delete, rename-identifier-within-block).

There are versions for C#, Java, C++, Python, and a variety of other languages. There are examples of each of these at the website.

A SmartDifferencer exists for C, but parsing C files without the full compiler command line is sometimes problematic, so sometimes it fails and you have to fall back to more primitive compare tools, like diff. We are working to improve this situation.

Please look at Compare++.

It can do language-aware structured comparison for C/C++, Java, C#, Javascript, CSS, ... and Optionally ignore comment, pure formatted, white-space and case changes and have unique ability to align moved sections such as C++ function, Java namespace, C# method, CSS selector, ...

If you are using eclipse, the integrated compare editor provides syntax aware diff/merge, at least for Java. Check "Open Structure Compare automatically" under the "General/Compare/Patch" preferences, then choose "Java Structure Compare" in the compare editor.

Look at https://en.wikipedia.org/wiki/Comparison_of_file_comparison_tools especially column Structured comparison.

Currently there are only two tools who understand language structure.

  • Compare++ (Works great for C++)
  • Pretty Diff (Language aware code comparison tool for several web based languages. It also beautifies, minifies, and a few other things..)

Unfortunately many tools have this column still empty.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top