8

In a project, I found some css files that "smell" like there are copy-pasted rules in them.

I wonder what are your strategies for detecting copy-paste stuff in files.

Just of curiosity i'd like to hear your tips and tricks for showing file similarities!

skaffman
  • 398,947
  • 96
  • 818
  • 769
Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155

3 Answers3

5

Try Simian.

It is used for copy-paste-detection in source code (Java, C#, C, C++, COBOL, Ruby, JSP, ASP, HTML, XML, Visual Basic, Groovy), but you can run this on plain text files too.

Sorcy
  • 2,587
  • 5
  • 26
  • 34
The Chairman
  • 7,087
  • 2
  • 36
  • 44
  • is there a way to make simian show the actual similarities? On my test-run, it only shows a summary that "lines a to b in file X are similar to lines c to d in file Y"??? Showing the actual lines will help clarify a lot – Jesper Rønn-Jensen Oct 08 '09 at 12:02
  • Or better yet: is there a way to make it show diffs for the lines that are similar? – Jesper Rønn-Jensen Oct 08 '09 at 12:02
  • I'm afraid you'll have to check the results manually or write a tool that evaluates Simian results and attaches them to a file viewer. Maybe you could utilize a free diff tool (e.g. WinMerge, http://winmerge.org/) for that purpose. If you decide for the latter: I would be interested in such a solution too... – The Chairman Oct 11 '09 at 10:08
  • See my answer on CloneDR for a tool that explicitly shows similarities and differences. – Ira Baxter Feb 01 '10 at 18:45
  • @Jesper: Simian has a `-reportDuplicateText` option that instructs it to include the duplicated text in reports. – John Gietzen Mar 11 '11 at 18:44
4

There is a Copy-Paste Detection (CPD) project on sourceforge; http://pmd.sourceforge.net/cpd.html

But even in large projects I find my own knowledge of the code to be a reliable (although not foolproof) detection mechanism.

Also see this question for other suggestions.

Community
  • 1
  • 1
Ed Guiness
  • 34,602
  • 16
  • 110
  • 145
2

Our Semantic Designs CloneDR is a tool that detects copy-paste-edit blocks of code, for many languages: C, C++, Java, C++, COBOL, ECMAScript, PHP, VB6, VB.net, ...

It does using language-accurate parsers to build abstract syntax trees, corresponding to exact program structures, which are then compared for similarity. This means it is not confused in any way by whitespace, formmatting, comments, or even different "spelling" of literals (e.g., 3.14159 is the same as .00314150E3).

It generates a report that shows exactly how the blocks of code are similar, and precisely how they vary. You can see sample reports at the link.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341