Permalink
Browse files

Working on clj-diff, got something working

  • Loading branch information...
1 parent 93959d0 commit 3ae4e7aa73df543ec08c49d87d094b3a2cd23e01 @mmcgrana committed Jan 25, 2009
Showing with 1,282 additions and 0 deletions.
  1. +5 −0 clj-diff/A1
  2. +5 −0 clj-diff/A2
  3. +8 −0 clj-diff/CASES
  4. +539 −0 clj-diff/TEST1
  5. +536 −0 clj-diff/TEST2
  6. +117 −0 clj-diff/TODO
  7. +72 −0 clj-diff/src/clj_diff/core.clj
View
@@ -0,0 +1,5 @@
+a
+b
+c
+d
+e
View
@@ -0,0 +1,5 @@
+a'
+b
+c
+c'
+d
View
@@ -0,0 +1,8 @@
+a a 3: 3:
+b b c c
+c c +c'
+d c' d d
+e d
+ e 5: 6:
+ f e e
+ +f
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,117 @@
+nuetral return format?
+context can be handled by the caller i think
+port the implementation and tests from java to clojure, to remove the dependency
+and increase future flexability.
+note also: http://www.bmsi.com/java/#diff
+note corner case of add or del start = 0
+diffing things other than strings??
+
+
+Line x: Line y:
+Upper Context Upper Context
+(blank Green added)
+(Red del blank)
+(Red del Green added)
+Lower Context Lower Conext
+
+(diff* '("a" "b" "c" "d" "e") '("a'" "b" "c" "c'" "d"))
+{:del-start 0, :del-end 0, :add-start 0, :add-end 0}
+{:del-start 3, :del-end nil, :add-start 3, :add-end 3}
+{:del-start 4, :del-end 4, :add-start 5, :add-end nil}
+
+[0 0 0 0]
+[3 nil 3 3]
+[4 4 5 nil]
+
+{:change {:old-start 0 :old-end 0 :new-start 0 :new-end 0}} (direct)
+{:addition {:old-start 3 :new-start 3 :new-end 3}}
+{:deletion {:old-start 4 :old-end 4 :new-start 5}}
+
+- a
+---
++ a'
+
++ c'
+
+- e
+nil
+
+
+(diff)
+429,430d428
+< copyrighted by the Free Software Foundation, write to the Free
+< Software Foundation; we sometimes make exceptions for this. Our
+438d435
+< WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+482c479,481
+< but WITHOUT ANY WARRANTY; without even the implied warranty of
+---
+> but WITHOUT ANY WARRANTY
+> ads;lajdf;lajd;flajdfad;lfkja;dlsfja;lsdkfja;ldkfja;lsdkjfad
+> ; without even the implied warranty of
+516,521c515,518
+< * Redistributions of source code must retain the above copyright
+< notice, this list of conditions and the following disclaimer.
+< * Redistributions in binary form must reproduce the above copyright
+< notice, this list of conditions and the following disclaimer in the
+< documentation and/or other materials provided with the distribution.
+< * The end-user documentation included with the redistribution, if
+---
+> asd;lakjdfl;kajdfljadflad
+> ads;lfkja;sldfjal;dfkja;ldfkj
+> ;alskdf;alkjdsf;alkjdf;aldkjf
+> ;lakjdsf;ladjf;lakjdf;laksdjfed with the redistribution, if
+
+(diff -u)
+--- TEST1 2009-01-24 20:49:37.000000000 -0500
++++ TEST2 2009-01-24 20:49:37.000000000 -0500
+@@ -426,8 +426,6 @@
+ 14. If you wish to incorporate parts of the Library into other free
+ programs whose distribution conditions are incompatible with these,
+ write to the author to ask for permission. For software which is
+-copyrighted by the Free Software Foundation, write to the Free
+-Software Foundation; we sometimes make exceptions for this. Our
+ decision will be guided by the two goals of preserving the free status
+ of all derivatives of our free software and of promoting the sharing
+ and reuse of software generally.
+@@ -435,7 +433,6 @@
+ NO WARRANTY
+
+ 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+-WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+ EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+ OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+ KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+@@ -479,7 +476,9 @@
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+- but WITHOUT ANY WARRANTY; without even the implied warranty of
++ but WITHOUT ANY WARRANTY
++ ads;lajdf;lajd;flajdfad;lfkja;dlsfja;lsdkfja;ldkfja;lsdkjfad
++ ; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+@@ -513,12 +512,10 @@
+ modification, are permitted provided that the following conditions are
+ met:
+
+- * Redistributions of source code must retain the above copyright
+-notice, this list of conditions and the following disclaimer.
+- * Redistributions in binary form must reproduce the above copyright
+-notice, this list of conditions and the following disclaimer in the
+-documentation and/or other materials provided with the distribution.
+- * The end-user documentation included with the redistribution, if
++ asd;lakjdfl;kajdfljadflad
++ ads;lfkja;sldfjal;dfkja;ldfkj
++ ;alskdf;alkjdsf;alkjdf;aldkjf
++ ;lakjdsf;ladjf;lakjdf;laksdjfed with the redistribution, if
+ any, must include the following acknowledgement:
+ "This product includes software developed in part by support from
+ the Defense Advanced Research Project Agency (DARPA)"
+
+http://en.wikipedia.org/w/index.php?title=Clojure&diff=265301807&oldid=262845359
+
+
+
@@ -0,0 +1,72 @@
+(ns clj-diff.core
+ (:use
+ (clojure.contrib str-utils))
+ (:import
+ (org.incava.util.diff Diff Difference)))
+
+(defn- num-unless-neg [x]
+ (if-not (neg? x) x))
+
+(defn- dtuple [d]
+ [(num-unless-neg (.getDeletedStart d))
+ (num-unless-neg (.getDeletedEnd d))
+ (num-unless-neg (.getAddedStart d))
+ (num-unless-neg (.getAddedEnd d))])
+
+(defn diff [coll-a coll-b]
+ "Returns the difference tuples for the diff from coll-a to coll-b."
+ (map dtuple (.diff (Diff. coll-a coll-b))))
+
+(defn diff-text [text-a text-b]
+ "Returns a 3-tuple: a vector of the lines from text-a, a vector of the lines
+ from text-b, and the difference tuples for diff from the a lines to the
+ b lines."
+ (let [lines-a (vec (re-split #"\n" text-a))
+ lines-b (vec (re-split #"\n" text-b))]
+ [lines-a lines-b (diff lines-a lines-b)]))
+
+; TODO: coallesced context (can skip for now)
+; TODO: line isn't quite right for cases with upper context
+; TODO: seperate parsing from printing
+
+(defn print-pair [left right]
+ (printf "%-8s%-8s\n" (or left "") (or right "")))
+
+(defn sub-lines [lines start end]
+ (subvec lines start (inc end)))
+
+(defn print-pairs [lefts rights]
+ (doseq [i (range (max (count lefts) (count rights)))]
+ (print-pair (if-let [left (get lefts i)] (str "- " left))
+ (if-let [right (get rights i)] (str "+ " right)))))
+
+(defn print-hunk [[start-a end-a start-b end-b] lines-a lines-b]
+ (println)
+ (print-pair (str "Line " (if (> start-a 0) start-a 1) ":")
+ (str "Line " (if (> start-b 0) start-b 1) ":"))
+ (let [line-a (and (> start-a 0) (nth lines-a (dec start-a)))
+ line-b (and (> start-b 0) (nth lines-b (dec start-b)))]
+ (if (or line-a line-b)
+ (print-pair (if line-a (str "* " line-a)) (if line-b (str "* " line-b)))))
+ (cond (and end-a end-b)
+ (print-pairs (sub-lines lines-a start-a end-a)
+ (sub-lines lines-b start-b end-b))
+ end-a
+ (print-pairs (sub-lines lines-a start-a end-a) nil)
+ end-b
+ (print-pairs nil (sub-lines lines-b start-b end-b)))
+ (let [end-a* (or end-a (dec start-a))
+ end-b* (or end-a (dec start-a))
+ line-a (and (< (inc end-a*) (count lines-a)) (nth lines-a (inc end-a*)))
+ line-b (and (< (inc end-b*) (count lines-b)) (nth lines-b (inc end-b*)))]
+ (if (or line-a line-b)
+ (print-pair (if line-a (str "* " line-a)) (if line-b (str "* " line-b))))))
+
+(defn print-diff-files [path-a path-b]
+ (let [[lines-a lines-b dtuples] (diff-text (slurp path-a) (slurp path-b))
+ cdiff (column-diff lines-a lines-b dtuples)]
+ (prn dtuples)
+ (prn cdiff)
+ (doseq [dtuple dtuples]
+ (print-hunk dtuple lines-a lines-b))))
+

0 comments on commit 3ae4e7a

Please sign in to comment.