mirror of
https://github.com/java-diff-utils/java-diff-utils.git
synced 2026-03-13 10:11:17 +08:00
removed due to good existing implementations and integrated jgit diff parser
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -66,6 +66,11 @@
|
||||
<type>jar</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jgit</groupId>
|
||||
<artifactId>org.eclipse.jgit</artifactId>
|
||||
<version>4.4.1.201607150455-r</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
90
src/main/java/difflib/algorithm/jgit/JGitDiff.java
Normal file
90
src/main/java/difflib/algorithm/jgit/JGitDiff.java
Normal file
@@ -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<T> implements DiffAlgorithm<T> {
|
||||
|
||||
@Override
|
||||
public Patch diff(List<T> original, List<T> revised) throws DiffException {
|
||||
EditList diffList = new EditList();
|
||||
diffList.addAll(new HistogramDiff().diff(new DataListComparator<>(), new DataList<>(original), new DataList<>(revised)));
|
||||
Patch<T> patch = new Patch<>();
|
||||
for (Edit edit : diffList) {
|
||||
System.out.println(edit);
|
||||
|
||||
Chunk<T> orgChunk = new Chunk<>(edit.getBeginA(), original.subList(edit.getBeginA(), edit.getEndA()));
|
||||
Chunk<T> 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<T> extends SequenceComparator<DataList<T>> {
|
||||
|
||||
@Override
|
||||
public boolean equals(DataList<T> original, int orgIdx, DataList<T> revised, int revIdx) {
|
||||
return original.data.get(orgIdx).equals(revised.data.get(revIdx));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hash(DataList<T> s, int i) {
|
||||
return s.data.get(i).hashCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class DataList<T> extends Sequence {
|
||||
|
||||
final List<T> data;
|
||||
|
||||
public DataList(List<T> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return data.size();
|
||||
}
|
||||
}
|
||||
@@ -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<T> {
|
||||
public final XDFile<T> xdf1 = new XDFile<>();
|
||||
public final XDFile<T> xdf2 = new XDFile<>();
|
||||
}
|
||||
@@ -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<T> {
|
||||
|
||||
int dend;
|
||||
|
||||
int dstart;
|
||||
public int ha[];
|
||||
public List<T> data;
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -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<T> implements DiffAlgorithm<T> {
|
||||
|
||||
XPParam xpp;
|
||||
final XDFEnv env = new XDFEnv();
|
||||
|
||||
@Override
|
||||
public Patch<T> diff(List<T> original, List<T> 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) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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 <T> void prepareContext(List<T> data, XPParam xpp, XDLClassifier cf, XDFile xdf) {
|
||||
xdf.data = data;
|
||||
xdf.ha = new int[data.size()];
|
||||
for (int i=0;i<data.size();i++) {
|
||||
xdf.ha[i] = data.get(i).hashCode();
|
||||
}
|
||||
xdf.dstart = 0;
|
||||
xdf.dend = data.size()-1;
|
||||
}
|
||||
|
||||
|
||||
public static <T> void prepareEnvironment(List<T> original, List<T> 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);
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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<String> orgList = Arrays.asList("A","B","C","A","B","B","A");
|
||||
List<String> revList = Arrays.asList("C","B","A","B","A","C");
|
||||
final Patch<String> 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<String> patched = patch.applyTo(orgList);
|
||||
assertEquals(revList, patched);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> patch = new XHistogram<String>().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<String> patch = new XHistogram<String>().diff(
|
||||
readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta"))),
|
||||
readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb"))));
|
||||
|
||||
System.out.println("done");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user