diff --git a/pom.xml b/pom.xml index be8b610..151cbea 100644 --- a/pom.xml +++ b/pom.xml @@ -66,6 +66,11 @@ jar test + + org.eclipse.jgit + org.eclipse.jgit + 4.4.1.201607150455-r + diff --git a/src/main/java/difflib/algorithm/jgit/JGitDiff.java b/src/main/java/difflib/algorithm/jgit/JGitDiff.java new file mode 100644 index 0000000..5d338e8 --- /dev/null +++ b/src/main/java/difflib/algorithm/jgit/JGitDiff.java @@ -0,0 +1,90 @@ +/* + * Copyright 2017 java-diff-utils. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package difflib.algorithm.jgit; + +import difflib.algorithm.DiffAlgorithm; +import difflib.algorithm.DiffException; +import difflib.patch.ChangeDelta; +import difflib.patch.Chunk; +import difflib.patch.DeleteDelta; +import difflib.patch.InsertDelta; +import difflib.patch.Patch; +import java.util.List; +import org.eclipse.jgit.diff.Edit; +import org.eclipse.jgit.diff.EditList; +import org.eclipse.jgit.diff.HistogramDiff; +import org.eclipse.jgit.diff.Sequence; +import org.eclipse.jgit.diff.SequenceComparator; + +/** + * + * @author toben + */ +public class JGitDiff implements DiffAlgorithm { + + @Override + public Patch diff(List original, List revised) throws DiffException { + EditList diffList = new EditList(); + diffList.addAll(new HistogramDiff().diff(new DataListComparator<>(), new DataList<>(original), new DataList<>(revised))); + Patch patch = new Patch<>(); + for (Edit edit : diffList) { + System.out.println(edit); + + Chunk orgChunk = new Chunk<>(edit.getBeginA(), original.subList(edit.getBeginA(), edit.getEndA())); + Chunk revChunk = new Chunk<>(edit.getBeginA(), revised.subList(edit.getBeginB(), edit.getEndB())); + switch (edit.getType()) { + case DELETE: + patch.addDelta(new DeleteDelta<>(orgChunk, revChunk)); + break; + case INSERT: + patch.addDelta(new InsertDelta<>(orgChunk, revChunk)); + break; + case REPLACE: + patch.addDelta(new ChangeDelta<>(orgChunk, revChunk)); + break; + } + } + return patch; + } +} + +class DataListComparator extends SequenceComparator> { + + @Override + public boolean equals(DataList original, int orgIdx, DataList revised, int revIdx) { + return original.data.get(orgIdx).equals(revised.data.get(revIdx)); + } + + @Override + public int hash(DataList s, int i) { + return s.data.get(i).hashCode(); + } + +} + +class DataList extends Sequence { + + final List data; + + public DataList(List data) { + this.data = data; + } + + @Override + public int size() { + return data.size(); + } +} diff --git a/src/main/java/difflib/algorithm/xdiff/XDFEnv.java b/src/main/java/difflib/algorithm/xdiff/XDFEnv.java deleted file mode 100644 index a50d81f..0000000 --- a/src/main/java/difflib/algorithm/xdiff/XDFEnv.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2017 java-diff-utils. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package difflib.algorithm.xdiff; - -/** - * - * @author tw - */ -public class XDFEnv { - public final XDFile xdf1 = new XDFile<>(); - public final XDFile xdf2 = new XDFile<>(); -} diff --git a/src/main/java/difflib/algorithm/xdiff/XDFile.java b/src/main/java/difflib/algorithm/xdiff/XDFile.java deleted file mode 100644 index c307dc1..0000000 --- a/src/main/java/difflib/algorithm/xdiff/XDFile.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2017 java-diff-utils. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package difflib.algorithm.xdiff; - -import java.util.List; - -/** - * - * @author tw - */ -public class XDFile { - - int dend; - - int dstart; - public int ha[]; - public List data; -} diff --git a/src/main/java/difflib/algorithm/xdiff/XDLClassifier.java b/src/main/java/difflib/algorithm/xdiff/XDLClassifier.java deleted file mode 100644 index 2b9673c..0000000 --- a/src/main/java/difflib/algorithm/xdiff/XDLClassifier.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2017 java-diff-utils. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package difflib.algorithm.xdiff; - -/** - * - * @author tw - */ -public class XDLClassifier { - -} diff --git a/src/main/java/difflib/algorithm/xdiff/XHistogram.java b/src/main/java/difflib/algorithm/xdiff/XHistogram.java deleted file mode 100644 index 96a8df4..0000000 --- a/src/main/java/difflib/algorithm/xdiff/XHistogram.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2017 java-diff-utils. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package difflib.algorithm.xdiff; - -import difflib.algorithm.DiffAlgorithm; -import difflib.algorithm.DiffException; -import difflib.patch.Patch; -import java.util.List; - -/** - * Histogram diff implementation. - * @author tw - */ -public class XHistogram implements DiffAlgorithm { - - XPParam xpp; - final XDFEnv env = new XDFEnv(); - - @Override - public Patch diff(List original, List revised) throws DiffException { - XPrepare.prepareEnvironment(original, revised, xpp, env); - - histogramDiff(xpp, env, - env.xdf1.dstart + 1, env.xdf1.dend - env.xdf1.dstart + 1, - env.xdf2.dstart + 1, env.xdf2.dend - env.xdf2.dstart + 1); - return null; - } - - private void histogramDiff(XPParam xpp, XDFEnv env, int line1, int count1, int line2, int count2) { - - } -} diff --git a/src/main/java/difflib/algorithm/xdiff/XPParam.java b/src/main/java/difflib/algorithm/xdiff/XPParam.java deleted file mode 100644 index 494bcc9..0000000 --- a/src/main/java/difflib/algorithm/xdiff/XPParam.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2017 java-diff-utils. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package difflib.algorithm.xdiff; - -/** - * - * @author tw - */ -public class XPParam { - public DiffAlgorithmType type; - public long flags; - - public static enum DiffAlgorithmType { - HISTOGRAM_DIFF - } -} diff --git a/src/main/java/difflib/algorithm/xdiff/XPrepare.java b/src/main/java/difflib/algorithm/xdiff/XPrepare.java deleted file mode 100644 index 5aabd49..0000000 --- a/src/main/java/difflib/algorithm/xdiff/XPrepare.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2017 java-diff-utils. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package difflib.algorithm.xdiff; - -import java.util.List; - -/** - * - * @author tw - */ -public final class XPrepare { - - private static void prepareContext(List data, XPParam xpp, XDLClassifier cf, XDFile xdf) { - xdf.data = data; - xdf.ha = new int[data.size()]; - for (int i=0;i void prepareEnvironment(List original, List revised, XPParam xpp, XDFEnv env) { - //sample calculation not needed, due to already List - long enl1 = original.size() + 1; - long enl2 = revised.size() + 1; - XDLClassifier cf = new XDLClassifier(); - - prepareContext(original, xpp, cf, env.xdf1); - prepareContext(revised, xpp, cf, env.xdf2); - } -} diff --git a/src/test/java/difflib/algorithm/xdiff/XPrepareTest.java b/src/test/java/difflib/algorithm/jgit/JGitDiffTest.java similarity index 51% rename from src/test/java/difflib/algorithm/xdiff/XPrepareTest.java rename to src/test/java/difflib/algorithm/jgit/JGitDiffTest.java index 45d07f7..bfef1a4 100644 --- a/src/test/java/difflib/algorithm/xdiff/XPrepareTest.java +++ b/src/test/java/difflib/algorithm/jgit/JGitDiffTest.java @@ -13,9 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package difflib.algorithm.xdiff; +package difflib.algorithm.jgit; +import difflib.DiffUtils; +import difflib.algorithm.DiffException; +import difflib.patch.Patch; +import difflib.patch.PatchFailedException; import java.util.Arrays; +import java.util.List; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -25,11 +30,11 @@ import static org.junit.Assert.*; /** * - * @author tw + * @author toben */ -public class XPrepareTest { +public class JGitDiffTest { - public XPrepareTest() { + public JGitDiffTest() { } @BeforeClass @@ -49,18 +54,20 @@ public class XPrepareTest { } /** - * Test of prepareEnvironment method, of class XPrepare. + * Test of diff method, of class JGitDiff. */ @Test - public void testPrepareEnvironment() { - XDFEnv env = new XDFEnv<>(); - XPrepare.prepareEnvironment( - Arrays.asList("A","B","C","A","B","B","A"), - Arrays.asList("C","B","A","B","A","C"), - new XPParam(), - env); - System.out.println(Arrays.toString(env.xdf1.ha)); - System.out.println(Arrays.toString(env.xdf2.ha)); + public void testDiff() throws DiffException, PatchFailedException { + List orgList = Arrays.asList("A","B","C","A","B","B","A"); + List revList = Arrays.asList("C","B","A","B","A","C"); + final Patch patch = new JGitDiff().diff(orgList, revList); + System.out.println(patch); + assertNotNull(patch); + assertEquals(3, patch.getDeltas().size()); + assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [DeleteDelta, position: 3, lines: [A, B]], [InsertDelta, position: 7, lines: [B, A, C]]]}", patch.toString()); + + List patched = patch.applyTo(orgList); + assertEquals(revList, patched); } } diff --git a/src/test/java/difflib/algorithm/xdiff/XHistogramTest.java b/src/test/java/difflib/algorithm/xdiff/XHistogramTest.java deleted file mode 100644 index 84a5656..0000000 --- a/src/test/java/difflib/algorithm/xdiff/XHistogramTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2017 java-diff-utils. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package difflib.algorithm.xdiff; - -import difflib.DiffUtils; -import static difflib.DiffUtilsTest.readStringListFromInputStream; -import difflib.TestConstants; -import difflib.algorithm.DiffException; -import difflib.algorithm.myers.MyersDiff; -import difflib.patch.Patch; -import java.io.IOException; -import java.util.Arrays; -import java.util.zip.ZipFile; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; -import org.junit.Ignore; - -/** - * - * @author tw - */ -public class XHistogramTest { - - public XHistogramTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of diff method, of class XHistogram. - */ - @Test - public void testDiff() throws Exception { - final Patch patch = new XHistogram().diff( - Arrays.asList("A","B","C","A","B","B","A"), - Arrays.asList("C","B","A","B","A","C")); - assertNotNull(patch); - assertEquals(4, patch.getDeltas().size()); - assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [InsertDelta, position: 3, lines: [B]], [DeleteDelta, position: 5, lines: [B]], [InsertDelta, position: 7, lines: [C]]]}", patch.toString()); - } - - @Test - public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, DiffException { - ZipFile zip = new ZipFile(TestConstants.MOCK_FOLDER + "/large_dataset1.zip"); - - Patch patch = new XHistogram().diff( - readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta"))), - readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb")))); - - System.out.println("done"); - } -}