Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone know of an advanced diff tool for C#?

I'm looking for a diff tool that can analyse my code and tell me what has changed on a construct by construct basis.

For instance, if I cut and paste a method from the start of my file and put it at the end but leave that method unchanged, I don't want it flagged. If however I insert a line of code or change something inside that method, it would flag it as changed.

I've used various diff tools, but all of them seem to fall short at telling you that lines have been inserted, removed or changed but couldn't tell what the changes were in any kind of logical fashion. It would be nice if when I periodically rearrange the layout of my code file the diff tool could keep up.

Does anyone have such a tool?

like image 275
BenAlabaster Avatar asked Jul 21 '09 19:07

BenAlabaster


People also ask

How do you compare two code bases?

Any decent diff tool (e.g. WinMerge, DiffMerge, etc.) will be able to report on the differences between two folder structures. Just put version A in a folder called VersionA , and version B in a folder called Version B and run the diff tool on them. To generate a report in WinMerge select "Tools" -> "Generate Report".

How do I use code compare tool?

Code Compare can be easily integrated into any version control system that supports external comparators. You can set up Code Compare as a comparison and merge tool with the help of the command line. Colored blocks for inserted, deleted, and modified text. Detailed highlights of changes within lines.

What is a diff in code?

Alternatively referred to as compare, diff is short for different or difference and describes a program's ability to show the difference between two or more files. A diff is an invaluable tool in programming as it enables a developer to see what has changed in-between versions.


2 Answers

I use http://winmerge.org/

I don't think you can do what you are asking, because of the way the longest common subsequence algorithms work for these tools.

Even if your functions get re-arranged, and your source file's functionality remains the same, it will still show up as a difference because of the nature of the LCS.

EDIT:
This is a bit far fetched, but if you were feeling extra ambitious, you could write your own that tailors to your exact needs.

you could use regular expressions to pull out each method in a source file, and do the LCS diff on each method individually based on its name. You could store your code a Dictionary (key,value) so that the key is the name of the method and the value is the string of the function. Then you just diff your dictionary_orig['method'] with your dictionary_new['method'].

Other than that, I don't know how you'd accomplish what you are looking for.

like image 186
Robert Greiner Avatar answered Sep 20 '22 05:09

Robert Greiner


Check out our Smart Differencer tool, which compares abstract syntax trees, and reports differences in terms of the nonterminals ("language constructs") that the ASTs represent, and plauible editing actions (insert, delete, move), as well as discovering consistent renaming.

At present, it only handles Java and COBOL, but it is based on DMS, which has parsers for a wide variaty of languages, including C#.

EDIT 9/8/2009: C# SmartDifferencer now available for beta testers.

The tool already handles a consistent rename across the entire file as being semantically trivial (on the assumption that other files reference the renamed symbol accordingly), as well as renames within a scope. We plan on taking into account semantically trivial changes, such as moving a method declaration around in a class for Java and C#.

EDIT October 2010: Production versions available. Eval downloads accessible at the website.

EDIT May 2012: You can see a C# example at this page.

One of the things it presently does not do is ignore semantically null edits. A particular case in point is shuffling methods about in a class body; we all know this has no impact on semantics for C#. Our tool compares syntax (via ASTs), not semantics, so it doesn't understand this particular nuance, and will consequently tell a user that "this has been moved" rather than being silent. We have plans to handle cases like this sometime in the future, but hey, every product has to have a version 1 :-} [As a subtle point, shuffling methods in a Java class is also semantically null, but shuffling fields is not due to order of evaluation of initializers. I don't know if this is true also for C# but it wouldn't surprise me.]

like image 31
11 revs Avatar answered Sep 19 '22 05:09

11 revs