Context-aware merge?

2020-01-28 02:38发布

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.

8条回答
一夜七次
2楼-- · 2020-01-28 03:17

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

查看更多
迷人小祖宗
3楼-- · 2020-01-28 03:22

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

查看更多
狗以群分
4楼-- · 2020-01-28 03:26

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.

查看更多
Ridiculous、
5楼-- · 2020-01-28 03:30

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

查看更多
Bombasti
6楼-- · 2020-01-28 03:31

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.

查看更多
▲ chillily
7楼-- · 2020-01-28 03:31

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.

查看更多
登录 后发表回答