<cfset Differ = CreateObject("component", "org.rickosborne.diff")>
<cfset LeftFile = ListToArray("a,b,c,d,e,f,g")>
<cfset RightFile = ListToArray("a,d,e,1,2,f,g,h")>
<cfset Difference = Differ.DiffArrays(LeftFile, RightFile)>
<cfset Pretty = Differ.Parallelize(Difference, LeftFile, RightFile)>
If you chose the lazy option from the previous list, your call to create the CFC will look like this:
<cfset Differ=CreateObject("component","diff")>
If you chose the third option, then I presume you can figure out how to create the object.
Pretty, is the one you'll want to dump or loop over or whatever. If you prefer a unified diff instead of parallel, you can use UnifiedDiffArrays() instead of Parallelize() with the same arguments.| File | Purpose | Final Location |
|---|---|---|
| cfdiff.css | Example stylesheet for the SVN browser. | SVN Browser root. |
| diff.cfc | The main component that does all of the work. | See above. Maybe web root, maybe not. |
| difftest.cfc | Unit tests for the diff.cfc file. | Not required. |
| svn.cfm | Basic SVN browser front-end. | Web root? |
| svnbrowser.cfc | Back-end for the SVN browser that interfaces with the JavaSVN API. | See above. Maybe web root, maybe not. |
| testdiff.cfm | Used to run the unit tests for diff.cfc. | Not required. |
| 6 files. | ||
If you want to use the SVN Browser, you'll need to install the TMate JavaSVN library. If you just want to run diffs, you do not need this library.
Look at svn.cfm for the easiest way to show a diff. It's going to look something like this:
<cfloop query="Pretty"> <tr> <td><cfif IsNumeric(AtFirst)>#NumberFormat(AtFirst)#<cfelse> </cfif></td> <td><cfif Len(ValueFirst) GT 0>#Replace(HTMLEditFormat(ValueFirst),Chr(9)," ","ALL")#<cfelse> </cfif></td> <td><cfif IsNumeric(AtSecond)>#NumberFormat(AtSecond)#<cfelse> </cfif></td> <td><cfif Len(ValueSecond) GT 0>#Replace(HTMLEditFormat(ValueSecond),Chr(9)," ","ALL")#<cfelse> </cfif></td> </tr> </cfloop>
If you decide you'd prefer to work with the raw difference, that's what that intermediate Difference variable is. Go to town.
This code is licensed under the Mozilla Public License (MPL) version 1.1. Read the license before you use the code!
Original ColdFusion code by Rick Osborne, 2006. The code includes an algorithm translated from the C-Sharp source by Matthias Hertel, which itself was originally released under the Creative Commons Attribution 2.0 Germany license and is based on a paper by Eugene Meyers in Algorithmica Vol. 1 No. 2, 1986, p 251: "An O(ND) Difference Algorithm and its Variations".