Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.

Commit 295f37e

Browse files
committed
removed due to good existing implementations and integrated jgit diff parser
1 parent 4a57b5d commit 295f37e

File tree

10 files changed

+116
-297
lines changed

10 files changed

+116
-297
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
<type>jar</type>
6767
<scope>test</scope>
6868
</dependency>
69+
<dependency>
70+
<groupId>org.eclipse.jgit</groupId>
71+
<artifactId>org.eclipse.jgit</artifactId>
72+
<version>4.4.1.201607150455-r</version>
73+
</dependency>
6974
</dependencies>
7075

7176
<build>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.algorithm.jgit;
17+
18+
import difflib.algorithm.DiffAlgorithm;
19+
import difflib.algorithm.DiffException;
20+
import difflib.patch.ChangeDelta;
21+
import difflib.patch.Chunk;
22+
import difflib.patch.DeleteDelta;
23+
import difflib.patch.InsertDelta;
24+
import difflib.patch.Patch;
25+
import java.util.List;
26+
import org.eclipse.jgit.diff.Edit;
27+
import org.eclipse.jgit.diff.EditList;
28+
import org.eclipse.jgit.diff.HistogramDiff;
29+
import org.eclipse.jgit.diff.Sequence;
30+
import org.eclipse.jgit.diff.SequenceComparator;
31+
32+
/**
33+
*
34+
* @author toben
35+
*/
36+
public class JGitDiff<T> implements DiffAlgorithm<T> {
37+
38+
@Override
39+
public Patch diff(List<T> original, List<T> revised) throws DiffException {
40+
EditList diffList = new EditList();
41+
diffList.addAll(new HistogramDiff().diff(new DataListComparator<>(), new DataList<>(original), new DataList<>(revised)));
42+
Patch<T> patch = new Patch<>();
43+
for (Edit edit : diffList) {
44+
System.out.println(edit);
45+
46+
Chunk<T> orgChunk = new Chunk<>(edit.getBeginA(), original.subList(edit.getBeginA(), edit.getEndA()));
47+
Chunk<T> revChunk = new Chunk<>(edit.getBeginA(), revised.subList(edit.getBeginB(), edit.getEndB()));
48+
switch (edit.getType()) {
49+
case DELETE:
50+
patch.addDelta(new DeleteDelta<>(orgChunk, revChunk));
51+
break;
52+
case INSERT:
53+
patch.addDelta(new InsertDelta<>(orgChunk, revChunk));
54+
break;
55+
case REPLACE:
56+
patch.addDelta(new ChangeDelta<>(orgChunk, revChunk));
57+
break;
58+
}
59+
}
60+
return patch;
61+
}
62+
}
63+
64+
class DataListComparator<T> extends SequenceComparator<DataList<T>> {
65+
66+
@Override
67+
public boolean equals(DataList<T> original, int orgIdx, DataList<T> revised, int revIdx) {
68+
return original.data.get(orgIdx).equals(revised.data.get(revIdx));
69+
}
70+
71+
@Override
72+
public int hash(DataList<T> s, int i) {
73+
return s.data.get(i).hashCode();
74+
}
75+
76+
}
77+
78+
class DataList<T> extends Sequence {
79+
80+
final List<T> data;
81+
82+
public DataList(List<T> data) {
83+
this.data = data;
84+
}
85+
86+
@Override
87+
public int size() {
88+
return data.size();
89+
}
90+
}

src/main/java/difflib/algorithm/xdiff/XDFEnv.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/main/java/difflib/algorithm/xdiff/XDFile.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/main/java/difflib/algorithm/xdiff/XDLClassifier.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/main/java/difflib/algorithm/xdiff/XHistogram.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/main/java/difflib/algorithm/xdiff/XPParam.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/main/java/difflib/algorithm/xdiff/XPrepare.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/test/java/difflib/algorithm/xdiff/XPrepareTest.java renamed to src/test/java/difflib/algorithm/jgit/JGitDiffTest.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package difflib.algorithm.xdiff;
16+
package difflib.algorithm.jgit;
1717

18+
import difflib.DiffUtils;
19+
import difflib.algorithm.DiffException;
20+
import difflib.patch.Patch;
21+
import difflib.patch.PatchFailedException;
1822
import java.util.Arrays;
23+
import java.util.List;
1924
import org.junit.After;
2025
import org.junit.AfterClass;
2126
import org.junit.Before;
@@ -25,11 +30,11 @@
2530

2631
/**
2732
*
28-
* @author tw
33+
* @author toben
2934
*/
30-
public class XPrepareTest {
35+
public class JGitDiffTest {
3136

32-
public XPrepareTest() {
37+
public JGitDiffTest() {
3338
}
3439

3540
@BeforeClass
@@ -49,18 +54,20 @@ public void tearDown() {
4954
}
5055

5156
/**
52-
* Test of prepareEnvironment method, of class XPrepare.
57+
* Test of diff method, of class JGitDiff.
5358
*/
5459
@Test
55-
public void testPrepareEnvironment() {
56-
XDFEnv<String> env = new XDFEnv<>();
57-
XPrepare.prepareEnvironment(
58-
Arrays.asList("A","B","C","A","B","B","A"),
59-
Arrays.asList("C","B","A","B","A","C"),
60-
new XPParam(),
61-
env);
62-
System.out.println(Arrays.toString(env.xdf1.ha));
63-
System.out.println(Arrays.toString(env.xdf2.ha));
60+
public void testDiff() throws DiffException, PatchFailedException {
61+
List<String> orgList = Arrays.asList("A","B","C","A","B","B","A");
62+
List<String> revList = Arrays.asList("C","B","A","B","A","C");
63+
final Patch<String> patch = new JGitDiff().diff(orgList, revList);
64+
System.out.println(patch);
65+
assertNotNull(patch);
66+
assertEquals(3, patch.getDeltas().size());
67+
assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [DeleteDelta, position: 3, lines: [A, B]], [InsertDelta, position: 7, lines: [B, A, C]]]}", patch.toString());
68+
69+
List<String> patched = patch.applyTo(orgList);
70+
assertEquals(revList, patched);
6471
}
6572

6673
}

0 commit comments

Comments
 (0)