8000 brought back diffutils to life · java-diff-utils/java-diff-utils@33f2d74 · GitHub
[go: up one dir, main page]

Skip to content

Commit 33f2d74

Browse files
committed
brought back diffutils to life
1 parent 696d22b commit 33f2d74

File tree

19 files changed

+279
-148
lines changed

19 files changed

+279
-148
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ This is a test ~senctence~**for diffutils**.
5252
But it can easily replaced by any other which is better for handing your texts. I have plan to add implementation of some in future.
5353

5454
### Changelog ###
55+
* Version 3.0-SNAPSHOT
56+
* Due to licensing issues Delta.java and DiffAlgorithm.java were removed.
5557
* Version 2.3-SNAPSHOT
5658
* Introduced a process listener to diff algorithms. For long running
5759
diffs one could implement some progress information.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<groupId>com.github.wumpz</groupId>
44
<artifactId>diffutils</artifactId>
55
<packaging>jar</packaging>
6-
<version>2.3-SNAPSHOT</version>
6+
<version>3.0-SNAPSHOT</version>
77
<name>java-diff-utils</name>
88
<description>The DiffUtils library for computing diffs, applying patches, generationg side-by-side view in Java.</description>
99
<url>https://github.com/wumpz/java-diff-utils</url>

src/main/java/com/github/difflib/DiffUtils.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
*/
2020
package com.github.difflib;
2121

22-
import com.github.difflib.algorithm.DiffAlgorithm;
22+
import com.github.difflib.algorithm.DiffAlgorithmI;
2323
import com.github.difflib.algorithm.DiffAlgorithmListener;
2424
import com.github.difflib.algorithm.DiffException;
2525
import com.github.difflib.algorithm.myers.MyersDiff;
26-
import com.github.difflib.patch.Delta;
26+
import com.github.difflib.patch.AbstractDelta;
2727
import com.github.difflib.patch.Patch;
2828
import com.github.difflib.patch.PatchFailedException;
2929
import java.util.ArrayList;
@@ -61,27 +61,30 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised) throws DiffEx
6161
/**
6262
* Computes the difference between the original and revised text.
6363
*/
64-
public static Patch<String> diff(String originalText, String revisedText, DiffAlgorithmListener progress) throws DiffException {
65-
return DiffUtils.diff(Arrays.asList(originalText.split("\n")), Arrays.asList(revisedText.split("\n")), progress);
64+
public static Patch<String> diff(String sourceText, String targetText,
65+
DiffAlgorithmListener progress) throws DiffException {
66+
return DiffUtils.diff(
67+
Arrays.asList(sourceText.split("\n")),
68+
Arrays.asList(targetText.split("\n")), progress);
6669
}
6770

6871
/**
6972
* Computes the difference between the original and revised list of elements with default diff algorithm
7073
*
71-
* @param original The original text. Must not be {@code null}.
72-
* @param revised The revised text. Must not be {@code null}.
74+
* @param source The original text. Must not be {@code null}.
75+
* @param target The revised text. Must not be {@code null}.
7376
*
7477
* @param equalizer the equalizer object to replace the default compare algorithm (Object.equals). If {@code null}
7578
* the default equalizer of the default algorithm is used..
7679
* @return The patch describing the difference between the original and revised sequences. Never {@code null}.
7780
*/
78-
public static <T> Patch<T> diff(List<T> original, List<T> revised,
81+
public static <T> Patch<T> diff(List<T> source, List<T> target,
7982
BiPredicate<T, T> equalizer) throws DiffException {
8083
if (equalizer != null) {
81-
return DiffUtils.diff(original, revised,
84+
return DiffUtils.diff(source, target,
8285
new MyersDiff<>(equalizer));
8386
}
84-
return DiffUtils.diff(original, revised, new MyersDiff<>());
87+
return DiffUtils.diff(source, target, new MyersDiff<>());
8588
}
8689

8790
/**
@@ -94,12 +97,12 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
9497
* @return The patch describing the difference between the original and revised sequences. Never {@code null}.
< 85EA /code>
9598
*/
9699
public static <T> Patch<T> diff(List<T> original, List<T> revised,
97-
DiffAlgorithm<T> algorithm, DiffAlgorithmListener progress) throws DiffException {
100+
DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress) throws DiffException {
98101
Objects.requireNonNull(original, "original must not be null");
99102
Objects.requireNonNull(revised, "revised must not be null");
100103
Objects.requireNonNull(algorithm, "algorithm must not be null");
101104

102-
return Patch.generate(original, revised, algorithm.diff(original, revised, progress));
105+
return Patch.generate(original, revised, algorithm.computeDiff(original, revised, progress));
103106
}
104107

105108
/**
@@ -111,7 +114,7 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
111114
* @return The patch describing the difference between the original and revised sequences. Never {@code null}.
112115
*/
113116
public static <T> Patch<T> diff(List<T> original, List<T> revised,
114-
DiffAlgorithm<T> algorithm) throws DiffException {
117+
DiffAlgorithmI<T> algorithm) throws DiffException {
115118
return diff(original, revised, algorithm, null);
116119
}
117120

@@ -133,9 +136,9 @@ public static Patch<String> diffInline(String original, String revised) throws D
133136
revList.add(character.toString());
134137
}
135138
Patch<String> patch = DiffUtils.diff(origList, revList);
136-
for (Delta<String> delta : patch.getDeltas()) {
137-
delta.getOriginal().setLines(compressLines(delta.getOriginal().getLines(), ""));
138-
delta.getRevised().setLines(compressLines(delta.getRevised().getLines(), ""));
139+
for (AbstractDelta<String> delta : patch.getDeltas()) {
140+
delta.getSource().setLines(compressLines(delta.getSource().getLines(), ""));
141+
delta.getTarget().setLines(compressLines(delta.getTarget().getLines(), ""));
139142
}
140143
return patch;
141144
}

src/main/java/com/github/difflib/UnifiedDiffUtils.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import com.github.difflib.patch.ChangeDelta;
1919
import com.github.difflib.patch.Chunk;
20-
import com.github.difflib.patch.Delta;
20+
import com.github.difflib.patch.AbstractDelta;
2121
import com.github.difflib.patch.Patch;
2222
import java.util.ArrayList;
2323
import java.util.List;
@@ -146,21 +146,21 @@ public static List<String> generateUnifiedDiff(String originalFileName,
146146
ret.add("--- " + originalFileName);
147147
ret.add("+++ " + revisedFileName);
148148

149-
List<Delta<String>> patchDeltas = new ArrayList<>(
149+
List<AbstractDelta<String>> patchDeltas = new ArrayList<>(
150150
patch.getDeltas());
151151

152152
// code outside the if block also works for single-delta issues.
153-
List<Delta<String>> deltas = new ArrayList<>(); // current
153+
List<AbstractDelta<String>> deltas = new ArrayList<>(); // current
154154
// list
155155
// of
156156
// Delta's to
157157
// process
158-
Delta<String> delta = patchDeltas.get(0);
158+
AbstractDelta<String> delta = patchDeltas.get(0);
159159
deltas.add(delta); // add the first Delta to the current set
160160
// if there's more than 1 Delta, we may need to output them together
161161
if (patchDeltas.size() > 1) {
162162
for (int i = 1; i < patchDeltas.size(); i++) {
163-
int position = delta.getOriginal().getPosition(); // store
163+
int position = delta.getSource().getPosition(); // store
164164
// the
165165
// current
166166
// position
@@ -170,9 +170,9 @@ public static List<String> generateUnifiedDiff(String originalFileName,
170170
// Check if the next Delta is too close to the current
171171
// position.
172172
// And if it is, add it to the current set
173-
Delta<String> nextDelta = patchDeltas.get(i);
174-
if ((position + delta.getOriginal().size() + contextSize) >= (nextDelta
175-
.getOriginal().getPosition() - contextSize)) {
173+
AbstractDelta<String> nextDelta = patchDeltas.get(i);
174+
if ((position + delta.getSource().size() + contextSize) >= (nextDelta
175+
.getSource().getPosition() - contextSize)) {
176176
deltas.add(nextDelta);
177177
} else {
178178
// if it isn't, output the current set,
@@ -207,65 +207,65 @@ public static List<String> generateUnifiedDiff(String originalFileName,
207207
* @author Bill James (tankerbay@gmail.com)
208208
*/
209209
private static List<String> processDeltas(List<String> origLines,
210-
List<Delta<String>> deltas, int contextSize) {
210+
List<AbstractDelta<String>> deltas, int contextSize) {
211211
List<String> buffer = new ArrayList<>();
212212
int origTotal = 0; // counter for total lines output from Original
213213
int revTotal = 0; // counter for total lines output from Original
214214
int line;
215215

216-
Delta<String> curDelta = deltas.get(0);
216+
AbstractDelta<String> curDelta = deltas.get(0);
217217

218218
// NOTE: +1 to overcome the 0-offset Position
219-
int origStart = curDelta.getOriginal().getPosition() + 1 - contextSize;
219+
int origStart = curDelta.getSource().getPosition() + 1 - contextSize;
220220
if (origStart < 1) {
221221
origStart = 1;
222222
}
223223

224-
int revStart = curDelta.getRevised().getPosition() + 1 - contextSize;
224+
int revStart = curDelta.getTarget().getPosition() + 1 - contextSize;
225225
if (revStart < 1) {
226226
revStart = 1;
227227
}
228228

229229
// find the start of the wrapper context code
230-
int contextStart = curDelta.getOriginal().getPosition() - contextSize;
230+
int contextStart = curDelta.getSource().getPosition() - contextSize;
231231
if (contextStart < 0) {
232232
contextStart = 0; // clamp to the start of the file
233233
}
234234

235235
// output the context before the first Delta
236-
for (line = contextStart; line < curDelta.getOriginal().getPosition(); line++) { //
236+
for (line = contextStart; line < curDelta.getSource().getPosition(); line++) { //
237237
buffer.add(" " + origLines.get(line));
238238
origTotal++;
239239
revTotal++;
240240
}
241241

242242
// output the first Delta
243243
buffer.addAll(getDeltaText(curDelta));
244-
origTotal += curDelta.getOriginal().getLines().size();
245-
revTotal += curDelta.getRevised().getLines().size();
244+
origTotal += curDelta.getSource().getLines().size();
245+
revTotal += curDelta.getTarget().getLines().size();
246246

247247
int deltaIndex = 1;
248248
while (deltaIndex < deltas.size()) { // for each of the other Deltas
249-
Delta<String> nextDelta = deltas.get(deltaIndex);
250-
int intermediateStart = curDelta.getOriginal().getPosition()
251-
+ curDelta.getOriginal().getLines().size();
252-
for (line = intermediateStart; line < nextDelta.getOriginal()
249+
AbstractDelta<String> nextDelta = deltas.get(deltaIndex);
250+
int intermediateStart = curDelta.getSource().getPosition()
251+
+ curDelta.getSource().getLines().size();
252+
for (line = intermediateStart; line < nextDelta.getSource()
253253
.getPosition(); line++) {
254254
// output the code between the last Delta and this one
255255
buffer.add(" " + origLines.get(line));
256256
origTotal++;
257257
revTotal++;
258258
}
259259
buffer.addAll(getDeltaText(nextDelta)); // output the Delta
260-
origTotal += nextDelta.getOriginal().getLines().size();
261-
revTotal += nextDelta.getRevised().getLines().size();
260+
origTotal += nextDelta.getSource().getLines().size();
261+
revTotal += nextDelta.getTarget().getLines().size();
262262
curDelta = nextDelta;
263263
deltaIndex++;
264264
}
265265

266266
// Now output the post-Delta context code, clamping the end of the file
267-
contextStart = curDelta.getOriginal().getPosition()
268-
+ curDelta.getOriginal().getLines().size();
267+
contextStart = curDelta.getSource().getPosition()
268+
+ curDelta.getSource().getLines().size();
269269
for (line = contextStart; (line < (contextStart + contextSize))
270270
&& (line < origLines.size()); line++) {
271271
buffer.add(" " + origLines.get(line));
@@ -275,7 +275,7 @@ private static List<String> processDeltas(List<String> origLines,
275275

276276
// Create and insert the block header, conforming to the Unified Diff
277277
// standard
278-
StringBuffer header = new StringBuffer();
278+
StringBuilder header = new StringBuilder();
279279
header.append("@@ -");
280280
header.append(origStart);
281281
header.append(",");
@@ -297,12 +297,12 @@ private static List<String> processDeltas(List<String> origLines,
297297
* @return list of String lines of code.
298298
* @author Bill James (tankerbay@gmail.com)
299299
*/
300-
private static List<String> getDeltaText(Delta<String> delta) {
300+
private static List<String> getDeltaText(AbstractDelta<String> delta) {
301301
List<String> buffer = new ArrayList<>();
302-
for (String line : delta.getOriginal().getLines()) {
302+
for (String line : delta.getSource().getLines()) {
303303
buffer.add("-" + line);
304304
}
305-
for (String line : delta.getRevised().getLines()) {
305+
for (String line : delta.getTarget().getLines()) {
306306
buffer.add("+" + line);
307307
}
308308
return buffer;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2018 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 com.github.difflib.algorithm;
17+
18+
import java.util.List;
19+
20+
/**
21+
* Interface of a diff algorithm.
22+
*
23+
* @author Tobias Warneke (t.warneke@gmx.net)
24+
*/
25+
public interface DiffAlgorithmI<T> {
26+
27+
/**
28+
* Computes the changeset to patch the source list to the target list.
29+
*
30+
* @param source source data
31+
* @param target target data
32+
* @param progress progress listener
33+
* @return
34+
* @throws DiffException
35+
*/
36+
List<Change> computeDiff(List<T> source, List<T> target, DiffAlgorithmListener progress) throws DiffException;
37+
}

src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package com.github.difflib.algorithm.jgit;
1717

1818
import com.github.difflib.algorithm.Change;
19-
import com.github.difflib.algorithm.DiffAlgorithm;
19+
import com.github.difflib.algorithm.DiffAlgorithmI;
2020
import com.github.difflib.algorithm.DiffAlgorithmListener;
2121
import com.github.difflib.algorithm.DiffException;
2222
import com.github.difflib.patch.DeltaType;
@@ -34,17 +34,17 @@
3434
*
3535
* @author toben
3636
*/
37-
public class HistogramDiff<T> implements DiffAlgorithm<T> {
37+
public class HistogramDiff<T> implements DiffAlgorithmI<T> {
3838

3939
@Override
40-
public List<Change> diff(List<T> original, List<T> revised, DiffAlgorithmListener progress) throws DiffException {
41-
Objects.requireNonNull(original, "original list must not be null");
42-
Objects.requireNonNull(revised, "revised list must not be null");
40+
public List<Change> computeDiff(List<T> source, List<T> target, DiffAlgorithmListener progress) throws DiffException {
41+
Objects.requireNonNull(source, "source list must not be null");
42+
Objects.requireNonNull(target, "target list must not be null");
4343
if (progress != null) {
4444
progress.diffStart();
4545
}
4646
EditList diffList = new EditList();
47-
diffList.addAll(new org.eclipse.jgit.diff.HistogramDiff().diff(new DataListComparator<>(progress), new DataList<>(original), new DataList<>(revised)));
47+
diffList.addAll(new org.eclipse.jgit.diff.HistogramDiff().diff(new DataListComparator<>(progress), new DataList<>(source), new DataList<>(target)));
4848
List<Change> patch = new ArrayList<>();
4949
for (Edit edit : diffList) {
5050
DeltaType type = DeltaType.EQUAL;

src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package com.github.difflib.algorithm.myers;
2121

2222
import com.github.difflib.algorithm.Change;
23-
import com.github.difflib.algorithm.DiffAlgorithm;
23+
import com.github.difflib.algorithm.DiffAlgorithmI;
2424
import com.github.difflib.algorithm.DiffAlgorithmListener;
2525
import com.github.difflib.algorithm.DiffException;
2626
import com.github.difflib.algorithm.DifferentiationFailedException;
@@ -34,7 +34,7 @@
3434
/**
3535
* A clean-room implementation of Eugene Myers greedy differencing algorithm.
3636
*/
37-
public final class MyersDiff<T> implements DiffAlgorithm<T> {
37+
public final class MyersDiff<T> implements DiffAlgorithmI<T> {
3838

3939
private final BiPredicate<T, T> DEFAULT_EQUALIZER = Object::equals;
4040
private final BiPredicate<T, T> equalizer;
@@ -54,15 +54,15 @@ public MyersDiff(final BiPredicate<T, T> equalizer) {
5454
* Return empty diff if get the error while procession the difference.
5555
*/
5656
@Override
57-
public List<Change> diff(final List<T> original, final List<T> revised, DiffAlgorithmListener progress) throws DiffException {
58-
Objects.requireNonNull(original, "original list must not be null");
59-
Objects.requireNonNull(revised, "revised list must not be null");
57+
public List<Change> computeDiff(final List<T> source, final List<T> target, DiffAlgorithmListener progress) throws DiffException {
58+
Objects.requireNonNull(source, "source list must not be null");
59+
Objects.requireNonNull(target, "target list must not be null");
6060

6161
if (progress != null) {
6262
progress.diffStart();
6363
}
64-
PathNode path = buildPath(original, revised, progress);
65-
List<Change> result = buildRevision(path, original, revised);
64+
PathNode path = buildPath(source, target, progress);
65+
List<Change> result = buildRevision(path, source, target);
6666
if (progress != null) {
6767
progress.diffEnd();
6868
}
@@ -177,18 +177,7 @@ private List<Change> buildRevision(PathNode actualPath, List<T> orig, List<T> re
177177
} else {
178178
changes.add(new Change(DeltaType.CHANGE, ianchor, i, janchor, j));
179179
}
180-
// Chunk<T> original = new Chunk<>(ianchor, copyOfRange(orig, ianchor, i));
181-
// Chunk<T> revised = new Chunk<>(janchor, copyOfRange(rev, janchor, j));
182-
// Delta<T> delta = null;
183-
// if (original.size() == 0 && revised.size() != 0) {
184-
// delta = new InsertDelta<>(original, revised);
185-
// } else if (original.size() > 0 && revised.size() == 0) {
186-
// delta = new DeleteDelta<>(original, revised);
187-
// } else {
188-
// delta = new ChangeDelta<>(original, revised);
189-
// }
190-
//
191-
// patch.addDelta(delta);
180+
192181
if (path.isSnake()) {
193182
path = path.prev;
194183
}

0 commit comments

Comments
 (0)
0