简体   繁体   中英

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?

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.


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.

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

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

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

Would you be willing to do the compare on compiled assemblies? If so, .NET Reflector has an Add-In available called Diff that will allow you to compare 2 assemblies. This would definitely not care where/how your objects are arranged inside of the source file.

The name of this tool is CodeCompare. Try it - CodeCompare . Required feature called as structure comparison .

It's something I've been wondering also. I don't think it exists yet. There are 'functional' (as opposed to merely text-based) diff tools available for other scenario's. Eg Microsoft Word integrates it's own diff/merge functionality (which can be scripted, so it can be integrated with eg TortoiseSVN), and there are also several tools available for XML which interpret xml files and do not simply consider them as text.

I'm not sure the added value of such a tool over a good text-based diff/merge tool would be compelling enough to merit developing it. OTOH this may just be the missing link solving the 'merge pain' we all feel when confronted with a difficult merge situation.

The difficulty is of course that you have to interpret the code in the same way the C# compiler does. For now the tool that comes the closest is indeed NDepend, I think. This blog post explains some of its capabilities in this regard.

超越比较Scooter软件为此而且价格便宜(30美元)。

I find SemanticMerge to be very helpful. It displays the differences in a nice visual way, and is a breeze for comparing complex C# code.

I know you are looking for a tool. But since I know of none, here is how I would accomplish this:

  1. Read a book on compiler design
  2. Lex/Parse your code
  3. Create an Abstract Syntax Tree
  4. Invent an algorithm for performing diffs on the Syntax tree
  5. Profit!

I use KDiff and find that it works pretty well. And is of course free.

As many have suggested, I guess this kind of comparison will not be possible with available diff tools. Because their purpose is to say whether something changed and if you move a method from the beginning to the end, there did actually change something.

But what comes to my mind now is that you could do it by yourself. Basically invoking some diff tool, which will then return the newly inserted lines, basically the method that you have moved to the end of your file. Then you could do a comparison yourself, whether the changed lines where already present in your previous file.

I think for comparing files online you should use http://www.diffnow.com . I am using this having option to choose language while comparing, its very nice

DiffMerge from SourceGear is excellent, and is available at no charge.

http://www.sourcegear.com/diffmerge/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM