From c9a2c9e7f01e85f9f889979bd22d21e0affb4b6c Mon Sep 17 00:00:00 2001
From: wumpz
Date: Thu, 22 Nov 2018 21:50:54 +0100
Subject: [PATCH 001/266] [maven-release-plugin] prepare for next development
iteration
---
pom.xml | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/pom.xml b/pom.xml
index 5a85f49f..200c4d14 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
io.github.java-diff-utils
java-diff-utils
jar
- 4.0
+ 4.1-SNAPSHOT
java-diff-utils
The DiffUtils library for computing diffs, applying patches, generationg side-by-side view in Java.
https://github.com/java-diff-utils/java-diff-utils
@@ -24,7 +24,7 @@
scm:git:https://github.com/java-diff-utils/java-diff-utils.git
scm:git:ssh://git@github.com:java-diff-utils/java-diff-utils.git
https://github.com/java-diff-utils/java-diff-utils.git
- java-diff-utils-4.0
+ HEAD
From 785c368f01264ed871578f9b5d7324be6ce4c089 Mon Sep 17 00:00:00 2001
From: Tobias
Date: Fri, 23 Nov 2018 08:15:08 +0100
Subject: [PATCH 002/266] Update README.md
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index e62f0e5c..e5c731ef 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
## Status ##
[](https://travis-ci.org/java-diff-utils/java-diff-utils) [](https://www.codacy.com/app/wumpz/java-diff-utils?utm_source=github.com&utm_medium=referral&utm_content=java-diff-utils/java-diff-utils&utm_campaign=Badge_Grade)
-[](http://maven-badges.herokuapp.com/maven-central/com.github.wumpz/diffutils)
+[](http://maven-badges.herokuapp.com/maven-central/io.github.java-diff-utils/java-diff-utils)
## Intro ##
From 5fe7e58f7640cc85c0c9528f4863f75706d4e48d Mon Sep 17 00:00:00 2001
From: Tobias
Date: Fri, 23 Nov 2018 08:16:05 +0100
Subject: [PATCH 003/266] Update README.md
---
README.md | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index e5c731ef..7cd6ee1b 100644
--- a/README.md
+++ b/README.md
@@ -109,9 +109,9 @@ This is a valid piece of source code:
Just add the code below to your maven dependencies:
```
- com.github.wumpz
- diffutils
- 3.0
+ io.github.java-diff-utils
+ java-diff-utils
+ 4.0
```
From ab05a8bdb4dcdd899d9d7aef3e2e7302476bfbcb Mon Sep 17 00:00:00 2001
From: Tobias
Date: Fri, 23 Nov 2018 08:19:56 +0100
Subject: [PATCH 004/266] Update README.md
---
README.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 7cd6ee1b..c3c335ac 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,8 @@
# java-diff-utils
## Status ##
-[](https://travis-ci.org/java-diff-utils/java-diff-utils) [](https://www.codacy.com/app/wumpz/java-diff-utils?utm_source=github.com&utm_medium=referral&utm_content=java-diff-utils/java-diff-utils&utm_campaign=Badge_Grade)
+[](https://travis-ci.org/java-diff-utils/java-diff-utils)
+[](https://www.codacy.com/app/wumpz/java-diff-utils?utm_source=github.com&utm_medium=referral&utm_content=java-diff-utils/java-diff-utils&utm_campaign=Badge_Grade)
[](http://maven-badges.herokuapp.com/maven-central/io.github.java-diff-utils/java-diff-utils)
From fc1e247de206bf8a6f0b8b05d59d103b2b1e5592 Mon Sep 17 00:00:00 2001
From: heisluft
Date: Sat, 8 Dec 2018 14:56:40 +0100
Subject: [PATCH 005/266] Exchange "0 += 1" for "0 = 1"
---
src/main/java/com/github/difflib/UnifiedDiffUtils.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/main/java/com/github/difflib/UnifiedDiffUtils.java b/src/main/java/com/github/difflib/UnifiedDiffUtils.java
index fe5b6f69..d5812c31 100644
--- a/src/main/java/com/github/difflib/UnifiedDiffUtils.java
+++ b/src/main/java/com/github/difflib/UnifiedDiffUtils.java
@@ -83,10 +83,10 @@ public static Patch parseUnifiedDiff(List diff) {
new_ln = m.group(3) == null ? 1 : Integer.parseInt(m.group(3));
if (old_ln == 0) {
- old_ln += 1;
+ old_ln = 1;
}
if (new_ln == 0) {
- new_ln += 1;
+ new_ln = 1;
}
} else {
if (line.length() > 0) {
From de2f19497a86f1be638890cc443053095a83db52 Mon Sep 17 00:00:00 2001
From: Stephen Kitt
Date: Thu, 7 Feb 2019 18:51:58 +0100
Subject: [PATCH 006/266] Remove unused DiffException declarations
Signed-off-by: Stephen Kitt
---
.../com/github/difflib/algorithm/jgit/HistogramDiff.java | 3 +--
.../com/github/difflib/algorithm/jgit/HistogramDiffTest.java | 5 ++---
.../github/difflib/algorithm/jgit/LRHistogramDiffTest.java | 3 +--
3 files changed, 4 insertions(+), 7 deletions(-)
diff --git a/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java b/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java
index c2e941f2..e3f6cd5b 100644
--- a/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java
+++ b/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java
@@ -18,7 +18,6 @@
import com.github.difflib.algorithm.Change;
import com.github.difflib.algorithm.DiffAlgorithmI;
import com.github.difflib.algorithm.DiffAlgorithmListener;
-import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.DeltaType;
import java.util.ArrayList;
import java.util.List;
@@ -37,7 +36,7 @@
public class HistogramDiff implements DiffAlgorithmI {
@Override
- public List computeDiff(List source, List target, DiffAlgorithmListener progress) throws DiffException {
+ public List computeDiff(List source, List target, DiffAlgorithmListener progress) {
Objects.requireNonNull(source, "source list must not be null");
Objects.requireNonNull(target, "target list must not be null");
if (progress != null) {
diff --git a/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java b/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java
index 0009f18b..e1030242 100644
--- a/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java
+++ b/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java
@@ -16,7 +16,6 @@
package com.github.difflib.algorithm.jgit;
import com.github.difflib.algorithm.DiffAlgorithmListener;
-import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.Patch;
import com.github.difflib.patch.PatchFailedException;
import java.util.ArrayList;
@@ -58,7 +57,7 @@ public void tearDown() {
* Test of diff method, of class HistogramDiff.
*/
@Test
- public void testDiff() throws DiffException, PatchFailedException {
+ public void testDiff() throws 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 = Patch.generate(orgList, revList, new HistogramDiff().computeDiff(orgList, revList, null));
@@ -72,7 +71,7 @@ public void testDiff() throws DiffException, PatchFailedException {
}
@Test
- public void testDiffWithListener() throws DiffException, PatchFailedException {
+ public void testDiffWithListener() throws PatchFailedException {
List orgList = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
List revList = Arrays.asList("C", "B", "A", "B", "A", "C");
diff --git a/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java b/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java
index 5f93570d..9aee685a 100644
--- a/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java
+++ b/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java
@@ -18,7 +18,6 @@
import static com.github.difflib.DiffUtilsTest.readStringListFromInputStream;
import com.github.difflib.TestConstants;
import com.github.difflib.algorithm.DiffAlgorithmListener;
-import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.Patch;
import com.github.difflib.patch.PatchFailedException;
import java.io.IOException;
@@ -58,7 +57,7 @@ public void tearDown() {
}
@Test
- public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, DiffException, PatchFailedException {
+ public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, PatchFailedException {
ZipFile zip = new ZipFile(TestConstants.MOCK_FOLDER + "/large_dataset1.zip");
List original = readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta")));
List revised = readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb")));
From 2d116d8c540c472205e5d1fa552f86835fce7920 Mon Sep 17 00:00:00 2001
From: Stephen Kitt
Date: Thu, 7 Feb 2019 18:55:41 +0100
Subject: [PATCH 007/266] Use String.join instead of streaming/collecting
Signed-off-by: Stephen Kitt
---
src/main/java/com/github/difflib/DiffUtils.java | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/src/main/java/com/github/difflib/DiffUtils.java b/src/main/java/com/github/difflib/DiffUtils.java
index 8b144642..b57a0d2f 100644
--- a/src/main/java/com/github/difflib/DiffUtils.java
+++ b/src/main/java/com/github/difflib/DiffUtils.java
@@ -28,7 +28,6 @@
import java.util.List;
import java.util.Objects;
import java.util.function.BiPredicate;
-import static java.util.stream.Collectors.joining;
/**
* Implements the difference and patching engine
@@ -143,7 +142,7 @@ private static List compressLines(List lines, String delimiter)
if (lines.isEmpty()) {
return Collections.emptyList();
}
- return Collections.singletonList(lines.stream().collect(joining(delimiter)));
+ return Collections.singletonList(String.join(delimiter, lines));
}
/**
From 47c6e9b417af69153a64ea27f04de82707fe7ee8 Mon Sep 17 00:00:00 2001
From: Stephen Kitt
Date: Thu, 7 Feb 2019 18:56:44 +0100
Subject: [PATCH 008/266] Drop unnecessary Integer.toString
Signed-off-by: Stephen Kitt
---
.../java/com/github/difflib/algorithm/myers/PathNode.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/main/java/com/github/difflib/algorithm/myers/PathNode.java b/src/main/java/com/github/difflib/algorithm/myers/PathNode.java
index a3f2070c..f3c153f8 100644
--- a/src/main/java/com/github/difflib/algorithm/myers/PathNode.java
+++ b/src/main/java/com/github/difflib/algorithm/myers/PathNode.java
@@ -102,9 +102,9 @@ public String toString() {
PathNode node = this;
while (node != null) {
buf.append("(");
- buf.append(Integer.toString(node.i));
+ buf.append(node.i);
buf.append(",");
- buf.append(Integer.toString(node.j));
+ buf.append(node.j);
buf.append(")");
node = node.prev;
}
From b41e2d5b5a6efbd8deb6a56bfa6e3a478f74faec Mon Sep 17 00:00:00 2001
From: Stephen Kitt
Date: Thu, 7 Feb 2019 18:57:00 +0100
Subject: [PATCH 009/266] Use assertEquals for comparison
... instead of assertTrue(a == b)
Signed-off-by: Stephen Kitt
---
src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
index 89ab32f6..53aade3e 100644
--- a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
+++ b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
@@ -10,7 +10,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
@@ -114,7 +114,7 @@ private void verify(List origLines, List revLines,
List patchedLines;
try {
patchedLines = (List) fromUnifiedPatch.applyTo(origLines);
- assertTrue(revLines.size() == patchedLines.size());
+ assertEquals(revLines.size(), patchedLines.size());
for (int i = 0; i < revLines.size(); i++) {
String l1 = revLines.get(i);
String l2 = patchedLines.get(i);
From 3443d55a19ecb2ece0f4fac31bd3273a158e3efe Mon Sep 17 00:00:00 2001
From: Stephen Kitt
Date: Thu, 7 Feb 2019 18:59:01 +0100
Subject: [PATCH 010/266] Drop redundant casts
Signed-off-by: Stephen Kitt
---
src/main/java/com/github/difflib/text/DiffRowGenerator.java | 4 ++--
src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/main/java/com/github/difflib/text/DiffRowGenerator.java b/src/main/java/com/github/difflib/text/DiffRowGenerator.java
index 2ed2662e..e7246a4e 100644
--- a/src/main/java/com/github/difflib/text/DiffRowGenerator.java
+++ b/src/main/java/com/github/difflib/text/DiffRowGenerator.java
@@ -199,7 +199,7 @@ public List generateDiffRows(final List original, Patch
// Inserted DiffRow
if (delta instanceof InsertDelta) {
endPos = orig.last() + 1;
- for (String line : (List) rev.getLines()) {
+ for (String line : rev.getLines()) {
diffRows.add(buildDiffRow(Tag.INSERT, "", line));
}
continue;
@@ -208,7 +208,7 @@ public List generateDiffRows(final List original, Patch
// Deleted DiffRow
if (delta instanceof DeleteDelta) {
endPos = orig.last() + 1;
- for (String line : (List) orig.getLines()) {
+ for (String line : orig.getLines()) {
diffRows.add(buildDiffRow(Tag.DELETE, line, ""));
}
continue;
diff --git a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
index 53aade3e..49497e33 100644
--- a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
+++ b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
@@ -113,7 +113,7 @@ private void verify(List origLines, List revLines,
Patch fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
List patchedLines;
try {
- patchedLines = (List) fromUnifiedPatch.applyTo(origLines);
+ patchedLines = fromUnifiedPatch.applyTo(origLines);
assertEquals(revLines.size(), patchedLines.size());
for (int i = 0; i < revLines.size(); i++) {
String l1 = revLines.get(i);
From a312ec14ea5d41cf2d249b71afec125f077959fd Mon Sep 17 00:00:00 2001
From: Stephen Kitt
Date: Thu, 7 Feb 2019 19:00:02 +0100
Subject: [PATCH 011/266] Use foreach loop
Signed-off-by: Stephen Kitt
---
src/main/java/com/github/difflib/text/DiffRowGenerator.java | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/src/main/java/com/github/difflib/text/DiffRowGenerator.java b/src/main/java/com/github/difflib/text/DiffRowGenerator.java
index e7246a4e..1f2d8e02 100644
--- a/src/main/java/com/github/difflib/text/DiffRowGenerator.java
+++ b/src/main/java/com/github/difflib/text/DiffRowGenerator.java
@@ -187,8 +187,7 @@ public List generateDiffRows(final List original, Patch
List diffRows = new ArrayList<>();
int endPos = 0;
final List> deltaList = patch.getDeltas();
- for (int i = 0; i < deltaList.size(); i++) {
- AbstractDelta delta = deltaList.get(i);
+ for (AbstractDelta delta : deltaList) {
Chunk orig = delta.getSource();
Chunk rev = delta.getTarget();
From 765dc29386a2e0e3cf00cda0cf4c4560d7a8260a Mon Sep 17 00:00:00 2001
From: Stephen Kitt
Date: Thu, 7 Feb 2019 19:00:44 +0100
Subject: [PATCH 012/266] Use List.sort instead of Collections.sort
Signed-off-by: Stephen Kitt
---
src/main/java/com/github/difflib/patch/Patch.java | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/src/main/java/com/github/difflib/patch/Patch.java b/src/main/java/com/github/difflib/patch/Patch.java
index 9bbb30dc..74e99f81 100644
--- a/src/main/java/com/github/difflib/patch/Patch.java
+++ b/src/main/java/com/github/difflib/patch/Patch.java
@@ -19,12 +19,9 @@
*/
package com.github.difflib.patch;
+import static java.util.Comparator.comparing;
import com.github.difflib.algorithm.Change;
-import static com.github.difflib.patch.DeltaType.DELETE;
-import static com.github.difflib.patch.DeltaType.INSERT;
import java.util.ArrayList;
-import java.util.Collections;
-import static java.util.Comparator.comparing;
import java.util.List;
import java.util.ListIterator;
@@ -93,7 +90,7 @@ public void addDelta(AbstractDelta delta) {
* @return the deltas
*/
public List> getDeltas() {
- Collections.sort(deltas, comparing(d -> d.getSource().getPosition()));
+ deltas.sort(comparing(d -> d.getSource().getPosition()));
return deltas;
}
From e8c015a2ec6ec724c417e1125b85b162473a8c7e Mon Sep 17 00:00:00 2001
From: Stephen Kitt
Date: Fri, 8 Feb 2019 09:17:43 +0100
Subject: [PATCH 013/266] Fix Javadoc errors
Signed-off-by: Stephen Kitt
---
.../difflib/algorithm/DifferentiationFailedException.java | 4 ++--
.../com/github/difflib/algorithm/myers/MyersDiff.java | 2 +-
.../java/com/github/difflib/algorithm/myers/PathNode.java | 8 ++------
src/main/java/com/github/difflib/patch/ChangeDelta.java | 2 +-
src/main/java/com/github/difflib/patch/Chunk.java | 2 +-
src/main/java/com/github/difflib/patch/DeleteDelta.java | 2 +-
src/main/java/com/github/difflib/patch/InsertDelta.java | 2 +-
src/main/java/com/github/difflib/patch/Patch.java | 2 +-
.../java/com/github/difflib/text/DiffRowGenerator.java | 6 ++----
src/test/java/com/github/difflib/TestConstants.java | 2 +-
10 files changed, 13 insertions(+), 19 deletions(-)
diff --git a/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java b/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java
index 682a9b48..e201e440 100644
--- a/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java
+++ b/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java
@@ -18,8 +18,8 @@
/**
* Thrown whenever the differencing engine cannot produce the differences between two revisions of ta text.
*
- * @see MyersDiff
- * @see difflib.DiffAlgorithm
+ * @see com.github.difflib.algorithm.myers.MyersDiff
+ * @see DiffAlgorithmI
*/
public class DifferentiationFailedException extends DiffException {
diff --git a/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java b/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java
index 8c183411..abcf31e2 100644
--- a/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java
+++ b/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java
@@ -138,7 +138,7 @@ private PathNode buildPath(final List orig, final List rev, DiffAlgorithmL
/**
* Constructs a {@link Patch} from a difference path.
*
- * @param path The path.
+ * @param actualPath The path.
* @param orig The original sequence.
* @param rev The revised sequence.
* @return A {@link Patch} script corresponding to the path.
diff --git a/src/main/java/com/github/difflib/algorithm/myers/PathNode.java b/src/main/java/com/github/difflib/algorithm/myers/PathNode.java
index f3c153f8..fe8fd03a 100644
--- a/src/main/java/com/github/difflib/algorithm/myers/PathNode.java
+++ b/src/main/java/com/github/difflib/algorithm/myers/PathNode.java
@@ -19,10 +19,6 @@
* A node in a diffpath.
*
* @author Juanco Anez
- *
- * @see DiffNode
- * @see Snake
- *
*/
public final class PathNode {
@@ -78,10 +74,10 @@ public boolean isBootstrap() {
}
/**
- * Skips sequences of {@link DiffNode DiffNodes} until a {@link Snake} or bootstrap node is found, or the end of the
+ * Skips sequences of {@link PathNode PathNodes} until a snake or bootstrap node is found, or the end of the
* path is reached.
*
- * @return The next first {@link Snake} or bootstrap node in the path, or null if none found.
+ * @return The next first {@link PathNode} or bootstrap node in the path, or null if none found.
*/
public final PathNode previousSnake() {
if (isBootstrap()) {
diff --git a/src/main/java/com/github/difflib/patch/ChangeDelta.java b/src/main/java/com/github/difflib/patch/ChangeDelta.java
index 43f68b35..7c1cd98b 100644
--- a/src/main/java/com/github/difflib/patch/ChangeDelta.java
+++ b/src/main/java/com/github/difflib/patch/ChangeDelta.java
@@ -22,7 +22,7 @@
* Describes the change-delta between original and revised texts.
*
* @author Dmitry Naumenko
- * @param T The type of the compared elements in the data 'lines'.
+ * @param The type of the compared elements in the data 'lines'.
*/
public final class ChangeDelta extends AbstractDelta {
diff --git a/src/main/java/com/github/difflib/patch/Chunk.java b/src/main/java/com/github/difflib/patch/Chunk.java
index ed70e20d..6c1a9918 100644
--- a/src/main/java/com/github/difflib/patch/Chunk.java
+++ b/src/main/java/com/github/difflib/patch/Chunk.java
@@ -29,7 +29,7 @@
*
*
* @author Dmitry Naumenko
- * @param T The type of the compared elements in the 'lines'.
+ * @param The type of the compared elements in the 'lines'.
*/
public final class DeleteDelta extends AbstractDelta {
diff --git a/src/main/java/com/github/difflib/patch/InsertDelta.java b/src/main/java/com/github/difflib/patch/InsertDelta.java
index 08de5f1f..12164fa4 100644
--- a/src/main/java/com/github/difflib/patch/InsertDelta.java
+++ b/src/main/java/com/github/difflib/patch/InsertDelta.java
@@ -21,7 +21,7 @@
* Describes the add-delta between original and revised texts.
*
* @author Dmitry Naumenko
- * @param T The type of the compared elements in the 'lines'.
+ * @param The type of the compared elements in the 'lines'.
*/
public final class InsertDelta extends AbstractDelta {
diff --git a/src/main/java/com/github/difflib/patch/Patch.java b/src/main/java/com/github/difflib/patch/Patch.java
index 74e99f81..688e9f16 100644
--- a/src/main/java/com/github/difflib/patch/Patch.java
+++ b/src/main/java/com/github/difflib/patch/Patch.java
@@ -29,7 +29,7 @@
* Describes the patch holding all deltas between the original and revised texts.
*
* @author Dmitry Naumenko
- * @param T The type of the compared elements in the 'lines'.
+ * @param The type of the compared elements in the 'lines'.
*/
public final class Patch {
diff --git a/src/main/java/com/github/difflib/text/DiffRowGenerator.java b/src/main/java/com/github/difflib/text/DiffRowGenerator.java
index 1f2d8e02..c0267345 100644
--- a/src/main/java/com/github/difflib/text/DiffRowGenerator.java
+++ b/src/main/java/com/github/difflib/text/DiffRowGenerator.java
@@ -98,8 +98,7 @@ protected final static List splitStringPreserveDelimiter(String str, Pat
*
* @param startPosition the position from which tag should start. The counting start from a zero.
* @param endPosition the position before which tag should should be closed.
- * @param tag the tag name without angle brackets, just a word
- * @param cssClass the optional css class
+ * @param tagGenerator the tag generator
*/
static void wrapInTag(List sequence, int startPosition,
int endPosition, Function tagGenerator) {
@@ -179,7 +178,6 @@ public List generateDiffRows(List original, List revise
* for displaying side-by-side diff.
*
* @param original the original text
- * @param revised the revised text
* @param patch the given patch
* @return the DiffRows between original and revised texts
*/
@@ -400,7 +398,7 @@ public Builder reportLinesUnchanged(final boolean val) {
/**
* Generator for Old-Text-Tags.
*
- * @param tag the tag to set. Without angle brackets. Default: span.
+ * @param generator the tag generator
* @return builder with configured ignoreBlankLines parameter
*/
public Builder oldTag(Function generator) {
diff --git a/src/test/java/com/github/difflib/TestConstants.java b/src/test/java/com/github/difflib/TestConstants.java
index 56f5bf66..ba6d754e 100644
--- a/src/test/java/com/github/difflib/TestConstants.java
+++ b/src/test/java/com/github/difflib/TestConstants.java
@@ -10,7 +10,7 @@ public final class TestConstants {
public static final String BASE_FOLDER_RESOURCES = "target/test-classes/";
/**
- * The base folder containing the test files. Ends with {@link #FS}.
+ * The base folder containing the test files.
*/
public static final String MOCK_FOLDER = BASE_FOLDER_RESOURCES + "/mocks/";
From c0a1a3777c5fc084e73f8bbc1d2470efc87f1d66 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Thu, 28 Feb 2019 23:59:26 +0100
Subject: [PATCH 014/266] first start unified diff
---
.../difflib/unifieddiff/UnifiedDiff.java | 44 +++++++++++++
.../difflib/unifieddiff/UnifiedDiffTest.java | 57 +++++++++++++++++
.../unifieddiff/jsqlparser_patch_1.diff | 61 +++++++++++++++++++
3 files changed, 162 insertions(+)
create mode 100644 src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
create mode 100644 src/test/java/com/github/difflib/unifieddiff/UnifiedDiffTest.java
create mode 100644 src/test/resources/com/github/difflib/unifieddiff/jsqlparser_patch_1.diff
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
new file mode 100644
index 00000000..9fb51846
--- /dev/null
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2019 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 com.github.difflib.unifieddiff;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author Tobias Warneke (t.warneke@gmx.net)
+ */
+public final class UnifiedDiff {
+
+ private UnifiedDiff(InputStream stream) throws IOException {
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream));) {
+ while (reader.ready()) {
+ String line = reader.readLine();
+ LOG.info(line);
+ }
+ }
+
+ }
+ private static final Logger LOG = Logger.getLogger(UnifiedDiff.class.getName());
+
+ public static UnifiedDiff parse(InputStream stream) throws IOException {
+ return new UnifiedDiff(stream);
+ }
+}
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffTest.java
new file mode 100644
index 00000000..380eb8e9
--- /dev/null
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffTest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2019 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 com.github.difflib.unifieddiff;
+
+import java.io.IOException;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ *
+ * @author Tobias Warneke (t.warneke@gmx.net)
+ */
+public class UnifiedDiffTest {
+
+ public UnifiedDiffTest() {
+ }
+
+ @BeforeClass
+ public static void setUpClass() {
+ }
+
+ @AfterClass
+ public static void tearDownClass() {
+ }
+
+ @Before
+ public void setUp() {
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ @Test
+ public void testSimpleParse() throws IOException {
+ UnifiedDiff diff = UnifiedDiff.parse(UnifiedDiffTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));
+
+ System.out.println(diff);
+ }
+
+}
diff --git a/src/test/resources/com/github/difflib/unifieddiff/jsqlparser_patch_1.diff b/src/test/resources/com/github/difflib/unifieddiff/jsqlparser_patch_1.diff
new file mode 100644
index 00000000..c868759e
--- /dev/null
+++ b/src/test/resources/com/github/difflib/unifieddiff/jsqlparser_patch_1.diff
@@ -0,0 +1,61 @@
+From 3209a16c55c1976d5b772c607fd4b9d5fb9f9483 Mon Sep 17 00:00:00 2001
+From: wumpz
+Date: Tue, 19 Feb 2019 01:35:14 +0100
+Subject: [PATCH] fixes #753
+
+---
+ src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt | 5 +++--
+ .../net/sf/jsqlparser/statement/select/SelectTest.java | 7 +++++++
+ 2 files changed, 10 insertions(+), 2 deletions(-)
+
+diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
+index cd9bcd1..5f4b2b7 100644
+--- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
++++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
+@@ -189,6 +189,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
+ |
+ |
+ |
++|
+ |
+ |
+ |
+@@ -1039,7 +1040,7 @@ String RelObjectNameWithoutValue() :
+ | tk= | tk= | tk= | tk=
+ | tk=
+ | tk= | tk= | tk= | tk=
+- | tk= | tk= | tk=
++ | tk= | tk= | tk= | tk=
+ /* | tk= | tk= | tk= | tk= */
+ )
+
+@@ -3118,7 +3119,7 @@ Function Function() #Function:
+ Expression expr1 = null;
+ }
+ {
+- ["{fn" { retval.setEscaped(true); } ]
++ ["{" { retval.setEscaped(true); } ]
+
+ funcName=RelObjectNameExt()
+
+diff --git a/src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java b/src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
+index 7ee9b38..d39bfd3 100644
+--- a/src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
++++ b/src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
+@@ -1063,6 +1063,13 @@ public class SelectTest {
+ assertSqlCanBeParsedAndDeparsed("SELECT {fn concat(a, b)} AS COL");
+ }
+
++ @Test
++ public void testEscapedFunctionsIssue753() throws JSQLParserException {
++ Statement stmt = CCJSqlParserUtil.parse("SELECT { fn test(0)} AS COL");
++ assertEquals("SELECT {fn test(0)} AS COL", stmt.toString());
++ assertSqlCanBeParsedAndDeparsed("SELECT fn FROM fn");
++ }
++
+ @Test
+ public void testNamedParametersPR702() throws JSQLParserException {
+ assertSqlCanBeParsedAndDeparsed("SELECT substring(id, 2, 3), substring(id from 2 for 3), substring(id from 2), trim(BOTH ' ' from 'foo bar '), trim(LEADING ' ' from 'foo bar '), trim(TRAILING ' ' from 'foo bar '), trim(' ' from 'foo bar '), position('foo' in 'bar'), overlay('foo' placing 'bar' from 1), overlay('foo' placing 'bar' from 1 for 2) FROM my table");
+--
+2.17.1.windows.2
+
From 4caf63c19370a0f35d3a42764e88aa7b44e50616 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Wed, 6 Mar 2019 01:34:39 +0100
Subject: [PATCH 015/266]
---
pom.xml | 6 +
.../difflib/unifieddiff/UnifiedDiff.java | 35 ++--
.../difflib/unifieddiff/UnifiedDiffFile.java | 60 +++++++
.../unifieddiff/UnifiedDiffParser.java | 167 ++++++++++++++++++
...ffTest.java => UnifiedDiffParserTest.java} | 13 +-
5 files changed, 262 insertions(+), 19 deletions(-)
create mode 100644 src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java
create mode 100644 src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
rename src/test/java/com/github/difflib/unifieddiff/{UnifiedDiffTest.java => UnifiedDiffParserTest.java} (62%)
diff --git a/pom.xml b/pom.xml
index 200c4d14..82ccd481 100644
--- a/pom.xml
+++ b/pom.xml
@@ -105,6 +105,12 @@
+
+ org.assertj
+ assertj-core
+ 3.11.1
+ test
+
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
index 9fb51846..76bd2740 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
@@ -15,30 +15,33 @@
*/
package com.github.difflib.unifieddiff;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.util.logging.Logger;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
/**
*
* @author Tobias Warneke (t.warneke@gmx.net)
*/
-public final class UnifiedDiff {
+public class UnifiedDiff {
- private UnifiedDiff(InputStream stream) throws IOException {
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream));) {
- while (reader.ready()) {
- String line = reader.readLine();
- LOG.info(line);
- }
- }
+ private String header;
+ private final List files = new ArrayList<>();
+ public String getHeader() {
+ return header;
}
- private static final Logger LOG = Logger.getLogger(UnifiedDiff.class.getName());
- public static UnifiedDiff parse(InputStream stream) throws IOException {
- return new UnifiedDiff(stream);
+ public void setHeader(String header) {
+ this.header = header;
}
+
+ void addFile(UnifiedDiffFile file) {
+ files.add(file);
+ }
+
+ public List getFiles() {
+ return Collections.unmodifiableList(files);
+ }
+
}
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java
new file mode 100644
index 00000000..c1faa5ab
--- /dev/null
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2019 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 com.github.difflib.unifieddiff;
+
+/**
+ *
+ * @author Tobias Warneke (t.warneke@gmx.net)
+ */
+public class UnifiedDiffFile {
+
+ private String diffCommand;
+ private String fromFile;
+ private String toFile;
+ private String index;
+
+ public String getDiffCommand() {
+ return diffCommand;
+ }
+
+ public void setDiffCommand(String diffCommand) {
+ this.diffCommand = diffCommand;
+ }
+
+ public String getFromFile() {
+ return fromFile;
+ }
+
+ public void setFromFile(String fromFile) {
+ this.fromFile = fromFile;
+ }
+
+ public String getToFile() {
+ return toFile;
+ }
+
+ public void setToFile(String toFile) {
+ this.toFile = toFile;
+ }
+
+ public void setIndex(String index) {
+ this.index = index;
+ }
+
+ public String getIndex() {
+ return index;
+ }
+}
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
new file mode 100644
index 00000000..26194f12
--- /dev/null
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
@@ -0,0 +1,167 @@
+/*
+ * Copyright 2019 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 com.github.difflib.unifieddiff;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.function.BiConsumer;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.regex.MatchResult;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ *
+ * @author Tobias Warneke (t.warneke@gmx.net)
+ */
+public final class UnifiedDiffParser {
+
+ private final UnifiedDiffReader READER;
+ private final UnifiedDiff data = new UnifiedDiff();
+ private final UnifiedDiffLine[] PARSER_RULES = new UnifiedDiffLine[]{
+ new UnifiedDiffLine("^\\s+", (m, l) -> LOG.info("normal " + l)),
+ new UnifiedDiffLine(true, "^diff\\s", UnifiedDiffParser::processDiff),
+ new UnifiedDiffLine(true, "^index\\s[\\da-zA-Z]+\\.\\.[\\da-zA-Z]+(\\s(\\d+))?$", UnifiedDiffParser::processIndex)
+ };
+ private UnifiedDiffFile actualFile;
+
+ UnifiedDiffParser(Reader reader) {
+ this.READER = new UnifiedDiffReader(reader);
+ }
+
+ // schema = [[/^\s+/, normal], [/^diff\s/, start], [/^new file mode \d+$/, new_file],
+ // [/^deleted file mode \d+$/, deleted_file], [/^index\s[\da-zA-Z]+\.\.[\da-zA-Z]+(\s(\d+))?$/, index],
+ // [/^---\s/, from_file], [/^\+\+\+\s/, to_file], [/^@@\s+\-(\d+),?(\d+)?\s+\+(\d+),?(\d+)?\s@@/, chunk],
+ // [/^-/, del], [/^\+/, add], [/^\\ No newline at end of file$/, eof]];
+ private UnifiedDiff parse() throws IOException {
+ boolean header = true;
+ String headerTxt = "";
+ while (READER.ready()) {
+ String line = READER.readLine();
+ if (processLine(header, line) == false) {
+ if (header) {
+ headerTxt += line + "\n";
+ } else {
+ break;
+ }
+ } else {
+ header = false;
+ data.setHeader(headerTxt);
+ }
+ }
+ return data;
+ }
+
+ static String[] parseFileNames(String line) {
+ String[] split = line.split(" ");
+ return new String[]{
+ split[2].replaceAll("^a/", ""),
+ split[3].replaceAll("^b/", "")
+ };
+ }
+
+ private static final Logger LOG = Logger.getLogger(UnifiedDiffParser.class.getName());
+
+ public static UnifiedDiff parseUnifiedDiff(InputStream stream) throws IOException {
+ UnifiedDiffParser parser = new UnifiedDiffParser(new BufferedReader(new InputStreamReader(stream)));
+ return parser.parse();
+ }
+
+ private boolean processLine(boolean header, String line) {
+ for (UnifiedDiffLine rule : PARSER_RULES) {
+ if (header && rule.isStopsHeaderParsing() || !header) {
+ if (rule.processLine(line)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ private void initFileIfNecessary() {
+ if (actualFile == null) {
+ actualFile = new UnifiedDiffFile();
+ }
+ }
+
+ private void processDiff(MatchResult match, String line) {
+ initFileIfNecessary();
+ LOG.log(Level.INFO, "start {0}", line);
+ String[] fromTo = parseFileNames(READER.lastLine());
+ actualFile.setFromFile(fromTo[0]);
+ actualFile.setToFile(fromTo[1]);
+ }
+
+ private void processIndex(MatchResult match, String line) {
+ initFileIfNecessary();
+ LOG.log(Level.INFO, "index {0}", line);
+ actualFile.setIndex(line.substring(6));
+ }
+
+ class UnifiedDiffLine {
+
+ private final Pattern pattern;
+ private final BiConsumer command;
+ private final boolean stopsHeaderParsing;
+
+ public UnifiedDiffLine(String pattern, BiConsumer command) {
+ this(false, pattern, command);
+ }
+
+ public UnifiedDiffLine(boolean stopsHeaderParsing, String pattern, BiConsumer command) {
+ this.pattern = Pattern.compile(pattern);
+ this.command = command;
+ this.stopsHeaderParsing = stopsHeaderParsing;
+ }
+
+ public boolean processLine(String line) {
+ Matcher m = pattern.matcher(line);
+ if (m.find()) {
+ command.accept(m.toMatchResult(), line);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public boolean isStopsHeaderParsing() {
+ return stopsHeaderParsing;
+ }
+ }
+}
+
+class UnifiedDiffReader extends BufferedReader {
+
+ private String lastLine;
+
+ public UnifiedDiffReader(Reader reader) {
+ super(reader);
+ }
+
+ @Override
+ public String readLine() throws IOException {
+ lastLine = super.readLine();
+ return lastLine();
+ }
+
+ String lastLine() {
+ return lastLine;
+ }
+}
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java
similarity index 62%
rename from src/test/java/com/github/difflib/unifieddiff/UnifiedDiffTest.java
rename to src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java
index 380eb8e9..9eb6e052 100644
--- a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffTest.java
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java
@@ -16,6 +16,7 @@
package com.github.difflib.unifieddiff;
import java.io.IOException;
+import static org.assertj.core.api.Java6Assertions.assertThat;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
@@ -26,9 +27,9 @@
*
* @author Tobias Warneke (t.warneke@gmx.net)
*/
-public class UnifiedDiffTest {
+public class UnifiedDiffParserTest {
- public UnifiedDiffTest() {
+ public UnifiedDiffParserTest() {
}
@BeforeClass
@@ -49,9 +50,15 @@ public void tearDown() {
@Test
public void testSimpleParse() throws IOException {
- UnifiedDiff diff = UnifiedDiff.parse(UnifiedDiffTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));
+ UnifiedDiff diff = UnifiedDiffParser.parseUnifiedDiff(UnifiedDiffParserTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));
System.out.println(diff);
}
+ @Test
+ public void testParseDiffBlock() {
+ String[] files = UnifiedDiffParser.parseFileNames("diff --git a/src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java b/src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java");
+ assertThat(files).containsExactly("src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java", "src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java");
+ }
+
}
From c917f77bbf754b23e454f0ee4c7492f9183ef940 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Wed, 6 Mar 2019 01:45:39 +0100
Subject: [PATCH 016/266]
---
.../com/github/difflib/unifieddiff/UnifiedDiffParser.java | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
index 26194f12..77f218d3 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
@@ -37,8 +37,8 @@ public final class UnifiedDiffParser {
private final UnifiedDiff data = new UnifiedDiff();
private final UnifiedDiffLine[] PARSER_RULES = new UnifiedDiffLine[]{
new UnifiedDiffLine("^\\s+", (m, l) -> LOG.info("normal " + l)),
- new UnifiedDiffLine(true, "^diff\\s", UnifiedDiffParser::processDiff),
- new UnifiedDiffLine(true, "^index\\s[\\da-zA-Z]+\\.\\.[\\da-zA-Z]+(\\s(\\d+))?$", UnifiedDiffParser::processIndex)
+ new UnifiedDiffLine(true, "^diff\\s", this::processDiff),
+ new UnifiedDiffLine(true, "^index\\s[\\da-zA-Z]+\\.\\.[\\da-zA-Z]+(\\s(\\d+))?$", this::processIndex)
};
private UnifiedDiffFile actualFile;
@@ -101,7 +101,7 @@ private void initFileIfNecessary() {
}
}
- private void processDiff(MatchResult match, String line) {
+ public void processDiff(MatchResult match, String line) {
initFileIfNecessary();
LOG.log(Level.INFO, "start {0}", line);
String[] fromTo = parseFileNames(READER.lastLine());
@@ -109,7 +109,7 @@ private void processDiff(MatchResult match, String line) {
actualFile.setToFile(fromTo[1]);
}
- private void processIndex(MatchResult match, String line) {
+ public void processIndex(MatchResult match, String line) {
initFileIfNecessary();
LOG.log(Level.INFO, "index {0}", line);
actualFile.setIndex(line.substring(6));
From 361c27d5e7e472df372894b29e3ebb96ae18b019 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Wed, 6 Mar 2019 01:54:39 +0100
Subject: [PATCH 017/266]
---
.../java/com/github/difflib/unifieddiff/UnifiedDiffParser.java | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
index 77f218d3..84ddfc41 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
@@ -98,6 +98,7 @@ private boolean processLine(boolean header, String line) {
private void initFileIfNecessary() {
if (actualFile == null) {
actualFile = new UnifiedDiffFile();
+ data.addFile(actualFile);
}
}
From a523ba3a14747c1ca0d6316215052698414d7463 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Sun, 10 Mar 2019 19:24:03 +0100
Subject: [PATCH 018/266]
---
.../difflib/unifieddiff/UnifiedDiffFile.java | 7 ++
.../unifieddiff/UnifiedDiffParser.java | 66 ++++++++++++++++---
.../UnifiedDiffParserException.java | 43 ++++++++++++
3 files changed, 108 insertions(+), 8 deletions(-)
create mode 100644 src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParserException.java
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java
index c1faa5ab..cd49de57 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java
@@ -15,6 +15,8 @@
*/
package com.github.difflib.unifieddiff;
+import com.github.difflib.patch.Patch;
+
/**
*
* @author Tobias Warneke (t.warneke@gmx.net)
@@ -25,6 +27,7 @@ public class UnifiedDiffFile {
private String fromFile;
private String toFile;
private String index;
+ private Patch patch = new Patch<>();
public String getDiffCommand() {
return diffCommand;
@@ -57,4 +60,8 @@ public void setIndex(String index) {
public String getIndex() {
return index;
}
+
+ public Patch getPatch() {
+ return patch;
+ }
}
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
index 84ddfc41..7244169b 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
@@ -20,6 +20,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -33,13 +35,18 @@
*/
public final class UnifiedDiffParser {
+ private static final String UNIFIED_DIFF_CHUNK_REGEXP = "^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@$";
+
private final UnifiedDiffReader READER;
private final UnifiedDiff data = new UnifiedDiff();
- private final UnifiedDiffLine[] PARSER_RULES = new UnifiedDiffLine[]{
- new UnifiedDiffLine("^\\s+", (m, l) -> LOG.info("normal " + l)),
+ private final UnifiedDiffLine[] MAIN_PARSER_RULES = new UnifiedDiffLine[]{
new UnifiedDiffLine(true, "^diff\\s", this::processDiff),
- new UnifiedDiffLine(true, "^index\\s[\\da-zA-Z]+\\.\\.[\\da-zA-Z]+(\\s(\\d+))?$", this::processIndex)
+ new UnifiedDiffLine(true, "^index\\s[\\da-zA-Z]+\\.\\.[\\da-zA-Z]+(\\s(\\d+))?$", this::processIndex),
+ new UnifiedDiffLine(true, "^---\\s", this::processFromFile),
+ new UnifiedDiffLine(true, "^\\+\\+\\+\\s", this::processToFile),
+ new UnifiedDiffLine(true, UNIFIED_DIFF_CHUNK_REGEXP, this::processChunk)
};
+
private UnifiedDiffFile actualFile;
UnifiedDiffParser(Reader reader) {
@@ -50,7 +57,7 @@ public final class UnifiedDiffParser {
// [/^deleted file mode \d+$/, deleted_file], [/^index\s[\da-zA-Z]+\.\.[\da-zA-Z]+(\s(\d+))?$/, index],
// [/^---\s/, from_file], [/^\+\+\+\s/, to_file], [/^@@\s+\-(\d+),?(\d+)?\s+\+(\d+),?(\d+)?\s@@/, chunk],
// [/^-/, del], [/^\+/, add], [/^\\ No newline at end of file$/, eof]];
- private UnifiedDiff parse() throws IOException {
+ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
boolean header = true;
String headerTxt = "";
while (READER.ready()) {
@@ -79,13 +86,13 @@ static String[] parseFileNames(String line) {
private static final Logger LOG = Logger.getLogger(UnifiedDiffParser.class.getName());
- public static UnifiedDiff parseUnifiedDiff(InputStream stream) throws IOException {
+ public static UnifiedDiff parseUnifiedDiff(InputStream stream) throws IOException, UnifiedDiffParserException {
UnifiedDiffParser parser = new UnifiedDiffParser(new BufferedReader(new InputStreamReader(stream)));
return parser.parse();
}
- private boolean processLine(boolean header, String line) {
- for (UnifiedDiffLine rule : PARSER_RULES) {
+ private boolean processLine(boolean header, String line) throws UnifiedDiffParserException {
+ for (UnifiedDiffLine rule : MAIN_PARSER_RULES) {
if (header && rule.isStopsHeaderParsing() || !header) {
if (rule.processLine(line)) {
return true;
@@ -108,6 +115,35 @@ public void processDiff(MatchResult match, String line) {
String[] fromTo = parseFileNames(READER.lastLine());
actualFile.setFromFile(fromTo[0]);
actualFile.setToFile(fromTo[1]);
+ actualFile.setDiffCommand(line);
+ }
+
+ public void processChunk(MatchResult match, String chunkStart) {
+ try {
+ List originalTxt = new ArrayList<>();
+ List revisedTxt = new ArrayList<>();
+
+ int old_ln = match.group(1) == null ? 1 : Integer.parseInt(match.group(1));
+ int new_ln = match.group(3) == null ? 1 : Integer.parseInt(match.group(3));
+
+ while (this.READER.ready()) {
+ String line = READER.readLine();
+
+ if (line.startsWith(" ") || line.startsWith("+")) {
+ revisedTxt.add(line.substring(1));
+ }
+ if (line.startsWith(" ") || line.startsWith("-")) {
+ originalTxt.add(line.substring(1));
+ }
+ if (line.equals("")) {
+ break;
+ }
+ }
+
+ } catch (IOException ex) {
+ Logger.getLogger(UnifiedDiffParser.class.getName()).log(Level.SEVERE, null, ex);
+ throw new UnifiedDiffParserException(ex);
+ }
}
public void processIndex(MatchResult match, String line) {
@@ -116,6 +152,20 @@ public void processIndex(MatchResult match, String line) {
actualFile.setIndex(line.substring(6));
}
+ private void processFromFile(MatchResult match, String line) {
+ initFileIfNecessary();
+ actualFile.setFromFile(extractFileName(line));
+ }
+
+ private void processToFile(MatchResult match, String line) {
+ initFileIfNecessary();
+ actualFile.setToFile(extractFileName(line));
+ }
+
+ private String extractFileName(String line) {
+ return line.substring(4).replaceFirst("^(a|b)\\/", "");
+ }
+
class UnifiedDiffLine {
private final Pattern pattern;
@@ -132,7 +182,7 @@ public UnifiedDiffLine(boolean stopsHeaderParsing, String pattern, BiConsumer
Date: Sun, 10 Mar 2019 19:24:09 +0100
Subject: [PATCH 019/266]
From 55e4867e88fdc39e5865f0ef526fb397a7c96ca1 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Sun, 10 Mar 2019 23:36:16 +0100
Subject: [PATCH 020/266]
---
.../unifieddiff/UnifiedDiffParser.java | 64 +++++++++++++++----
.../unifieddiff/UnifiedDiffParserTest.java | 31 +++++++++
2 files changed, 82 insertions(+), 13 deletions(-)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
index 7244169b..d43aae0c 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
@@ -15,6 +15,8 @@
*/
package com.github.difflib.unifieddiff;
+import com.github.difflib.patch.ChangeDelta;
+import com.github.difflib.patch.Chunk;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@@ -35,7 +37,7 @@
*/
public final class UnifiedDiffParser {
- private static final String UNIFIED_DIFF_CHUNK_REGEXP = "^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@$";
+ static final Pattern UNIFIED_DIFF_CHUNK_REGEXP = Pattern.compile("^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@");
private final UnifiedDiffReader READER;
private final UnifiedDiff data = new UnifiedDiff();
@@ -62,6 +64,7 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
String headerTxt = "";
while (READER.ready()) {
String line = READER.readLine();
+ LOG.log(Level.INFO, "parsing line {0}", line);
if (processLine(header, line) == false) {
if (header) {
headerTxt += line + "\n";
@@ -118,25 +121,54 @@ public void processDiff(MatchResult match, String line) {
actualFile.setDiffCommand(line);
}
- public void processChunk(MatchResult match, String chunkStart) {
+ public void processChunk(MatchResult _match, String chunkStart) {
+ MatchResult match = _match;
try {
- List originalTxt = new ArrayList<>();
- List revisedTxt = new ArrayList<>();
- int old_ln = match.group(1) == null ? 1 : Integer.parseInt(match.group(1));
- int new_ln = match.group(3) == null ? 1 : Integer.parseInt(match.group(3));
+ while (true) {
- while (this.READER.ready()) {
- String line = READER.readLine();
+ List originalTxt = new ArrayList<>();
+ List revisedTxt = new ArrayList<>();
- if (line.startsWith(" ") || line.startsWith("+")) {
- revisedTxt.add(line.substring(1));
+ int old_ln = match.group(1) == null ? 1 : Integer.parseInt(match.group(1));
+ int new_ln = match.group(3) == null ? 1 : Integer.parseInt(match.group(3));
+ if (old_ln == 0) {
+ old_ln = 1;
}
- if (line.startsWith(" ") || line.startsWith("-")) {
- originalTxt.add(line.substring(1));
+ if (new_ln == 0) {
+ new_ln = 1;
}
- if (line.equals("")) {
+
+ while (this.READER.ready()) {
+ String line = READER.readLine();
+ LOG.log(Level.INFO, "processing chunk line {0}", line);
+
+ if (line.startsWith(" ") || line.startsWith("+")) {
+ revisedTxt.add(line.substring(1));
+ }
+ if (line.startsWith(" ") || line.startsWith("-")) {
+ originalTxt.add(line.substring(1));
+ }
+ if (line.equals("") || line.startsWith("@@") || line.startsWith("--")) {
+ break;
+ }
+ }
+
+ actualFile.getPatch().addDelta(new ChangeDelta<>(new Chunk<>(
+ old_ln - 1, originalTxt), new Chunk<>(
+ new_ln - 1, revisedTxt)));
+
+ if (READER.lastLine().equals("")
+ || READER.lastLine().startsWith("--")
+ || !READER.lastLine().startsWith("@@")) {
break;
+ } else {
+ Matcher m = UNIFIED_DIFF_CHUNK_REGEXP.matcher(READER.lastLine());
+ if (m.find()) {
+ match = m.toMatchResult();
+ } else {
+ break;
+ }
}
}
@@ -182,6 +214,12 @@ public UnifiedDiffLine(boolean stopsHeaderParsing, String pattern, BiConsumer command) {
+ this.pattern = pattern;
+ this.command = command;
+ this.stopsHeaderParsing = stopsHeaderParsing;
+ }
+
public boolean processLine(String line) throws UnifiedDiffParserException {
Matcher m = pattern.matcher(line);
if (m.find()) {
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java
index 9eb6e052..e13eb563 100644
--- a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java
@@ -16,9 +16,13 @@
package com.github.difflib.unifieddiff;
import java.io.IOException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import static org.assertj.core.api.Java6Assertions.assertThat;
import org.junit.After;
import org.junit.AfterClass;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -53,6 +57,12 @@ public void testSimpleParse() throws IOException {
UnifiedDiff diff = UnifiedDiffParser.parseUnifiedDiff(UnifiedDiffParserTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));
System.out.println(diff);
+
+ assertThat(diff.getFiles().size()).isEqualTo(2);
+
+ UnifiedDiffFile file1 = diff.getFiles().get(0);
+ assertThat(file1.getFromFile()).isEqualTo("src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt");
+ assertThat(file1.getPatch().getDeltas().size()).isEqualTo(3);
}
@Test
@@ -61,4 +71,25 @@ public void testParseDiffBlock() {
assertThat(files).containsExactly("src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java", "src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java");
}
+ @Test
+ public void testChunkHeaderParsing() {
+ Pattern pattern = UnifiedDiffParser.UNIFIED_DIFF_CHUNK_REGEXP;
+ Matcher matcher = pattern.matcher("@@ -189,6 +189,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */");
+
+ assertTrue(matcher.find());
+ assertEquals("189", matcher.group(1));
+ assertEquals("189", matcher.group(3));
+ }
+
+ @Test
+ public void testChunkHeaderParsing2() {
+ //"^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@.*$"
+ Pattern pattern = UnifiedDiffParser.UNIFIED_DIFF_CHUNK_REGEXP;
+ Matcher matcher = pattern.matcher("@@ -189,6 +189,7 @@");
+
+ assertTrue(matcher.find());
+ assertEquals("189", matcher.group(1));
+ assertEquals("189", matcher.group(3));
+ }
+
}
From d2355a8435052f8c508de28835105b822102672d Mon Sep 17 00:00:00 2001
From: wumpz
Date: Tue, 12 Mar 2019 00:15:51 +0100
Subject: [PATCH 021/266]
---
.../difflib/unifieddiff/UnifiedDiff.java | 8 ++
.../unifieddiff/UnifiedDiffParser.java | 126 ++++++++++--------
.../unifieddiff/UnifiedDiffParserTest.java | 2 +
3 files changed, 77 insertions(+), 59 deletions(-)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
index 76bd2740..42db0aee 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
@@ -26,6 +26,7 @@
public class UnifiedDiff {
private String header;
+ private String tail;
private final List files = new ArrayList<>();
public String getHeader() {
@@ -44,4 +45,11 @@ public List getFiles() {
return Collections.unmodifiableList(files);
}
+ void setTailTxt(String tailTxt) {
+ this.tail = tailTxt;
+ }
+
+ public String getTail() {
+ return tail;
+ }
}
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
index d43aae0c..77aa614f 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
@@ -46,7 +46,10 @@ public final class UnifiedDiffParser {
new UnifiedDiffLine(true, "^index\\s[\\da-zA-Z]+\\.\\.[\\da-zA-Z]+(\\s(\\d+))?$", this::processIndex),
new UnifiedDiffLine(true, "^---\\s", this::processFromFile),
new UnifiedDiffLine(true, "^\\+\\+\\+\\s", this::processToFile),
- new UnifiedDiffLine(true, UNIFIED_DIFF_CHUNK_REGEXP, this::processChunk)
+ new UnifiedDiffLine(false, UNIFIED_DIFF_CHUNK_REGEXP, this::processChunk),
+ new UnifiedDiffLine("^\\s+", this::processNormalLine),
+ new UnifiedDiffLine("^-", this::processDelLine),
+ new UnifiedDiffLine("^+", this::processAddLine)
};
private UnifiedDiffFile actualFile;
@@ -62,20 +65,33 @@ public final class UnifiedDiffParser {
private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
boolean header = true;
String headerTxt = "";
+ String tailTxt = "";
while (READER.ready()) {
String line = READER.readLine();
- LOG.log(Level.INFO, "parsing line {0}", line);
- if (processLine(header, line) == false) {
- if (header) {
- headerTxt += line + "\n";
+ if (line.matches("^\\-\\-\\s+")) {
+ break;
+ } else {
+ LOG.log(Level.INFO, "parsing line {0}", line);
+ if (processLine(header, line) == false) {
+ if (header) {
+ headerTxt += line + "\n";
+ } else {
+ break;
+ }
} else {
- break;
+ header = false;
+ data.setHeader(headerTxt);
}
- } else {
- header = false;
- data.setHeader(headerTxt);
}
}
+
+ finalizeChunk();
+
+ while (READER.ready()) {
+ tailTxt += READER.readLine() + "\n";
+ }
+ data.setTailTxt(tailTxt);
+
return data;
}
@@ -106,6 +122,10 @@ private boolean processLine(boolean header, String line) throws UnifiedDiffParse
}
private void initFileIfNecessary() {
+ if (!originalTxt.isEmpty() || !revisedTxt.isEmpty()) {
+ finalizeChunk();
+ actualFile = null;
+ }
if (actualFile == null) {
actualFile = new UnifiedDiffFile();
data.addFile(actualFile);
@@ -121,60 +141,48 @@ public void processDiff(MatchResult match, String line) {
actualFile.setDiffCommand(line);
}
- public void processChunk(MatchResult _match, String chunkStart) {
- MatchResult match = _match;
- try {
-
- while (true) {
-
- List originalTxt = new ArrayList<>();
- List revisedTxt = new ArrayList<>();
-
- int old_ln = match.group(1) == null ? 1 : Integer.parseInt(match.group(1));
- int new_ln = match.group(3) == null ? 1 : Integer.parseInt(match.group(3));
- if (old_ln == 0) {
- old_ln = 1;
- }
- if (new_ln == 0) {
- new_ln = 1;
- }
-
- while (this.READER.ready()) {
- String line = READER.readLine();
- LOG.log(Level.INFO, "processing chunk line {0}", line);
+ private List originalTxt = new ArrayList<>();
+ private List revisedTxt = new ArrayList<>();
+ private int old_ln;
+ private int new_ln;
+
+ private void finalizeChunk() {
+ if (!originalTxt.isEmpty() || !revisedTxt.isEmpty()) {
+ actualFile.getPatch().addDelta(new ChangeDelta<>(new Chunk<>(
+ old_ln - 1, originalTxt), new Chunk<>(
+ new_ln - 1, revisedTxt)));
+ old_ln = 0;
+ new_ln = 0;
+ originalTxt.clear();
+ revisedTxt.clear();
+ }
+ }
- if (line.startsWith(" ") || line.startsWith("+")) {
- revisedTxt.add(line.substring(1));
- }
- if (line.startsWith(" ") || line.startsWith("-")) {
- originalTxt.add(line.substring(1));
- }
- if (line.equals("") || line.startsWith("@@") || line.startsWith("--")) {
- break;
- }
- }
+ public void processNormalLine(MatchResult match, String line) {
+ String cline = line.substring(1);
+ originalTxt.add(cline);
+ revisedTxt.add(cline);
+ }
- actualFile.getPatch().addDelta(new ChangeDelta<>(new Chunk<>(
- old_ln - 1, originalTxt), new Chunk<>(
- new_ln - 1, revisedTxt)));
+ public void processAddLine(MatchResult match, String line) {
+ String cline = line.substring(1);
+ revisedTxt.add(cline);
+ }
- if (READER.lastLine().equals("")
- || READER.lastLine().startsWith("--")
- || !READER.lastLine().startsWith("@@")) {
- break;
- } else {
- Matcher m = UNIFIED_DIFF_CHUNK_REGEXP.matcher(READER.lastLine());
- if (m.find()) {
- match = m.toMatchResult();
- } else {
- break;
- }
- }
- }
+ public void processDelLine(MatchResult match, String line) {
+ String cline = line.substring(1);
+ originalTxt.add(cline);
+ }
- } catch (IOException ex) {
- Logger.getLogger(UnifiedDiffParser.class.getName()).log(Level.SEVERE, null, ex);
- throw new UnifiedDiffParserException(ex);
+ public void processChunk(MatchResult match, String chunkStart) {
+ finalizeChunk();
+ old_ln = match.group(1) == null ? 1 : Integer.parseInt(match.group(1));
+ new_ln = match.group(3) == null ? 1 : Integer.parseInt(match.group(3));
+ if (old_ln == 0) {
+ old_ln = 1;
+ }
+ if (new_ln == 0) {
+ new_ln = 1;
}
}
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java
index e13eb563..6fe916d8 100644
--- a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java
@@ -63,6 +63,8 @@ public void testSimpleParse() throws IOException {
UnifiedDiffFile file1 = diff.getFiles().get(0);
assertThat(file1.getFromFile()).isEqualTo("src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt");
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(3);
+
+ assertThat(diff.getTail()).isEqualTo("2.17.1.windows.2\n\n");
}
@Test
From 05b5d80d48b338ceaf95f8b2d32c7e77cbda5f23 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Mon, 18 Mar 2019 22:34:08 +0100
Subject: [PATCH 022/266]
---
.../com/github/difflib/unifieddiff/UnifiedDiff.java | 12 +++++++++++-
.../github/difflib/unifieddiff/UnifiedDiffFile.java | 10 +++++++++-
2 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
index 42db0aee..e1a52112 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
@@ -23,7 +23,7 @@
*
* @author Tobias Warneke (t.warneke@gmx.net)
*/
-public class UnifiedDiff {
+public final class UnifiedDiff {
private String header;
private String tail;
@@ -52,4 +52,14 @@ void setTailTxt(String tailTxt) {
public String getTail() {
return tail;
}
+
+ public static UnifiedDiff from(String header, String tail, UnifiedDiffFile... files) {
+ UnifiedDiff diff = new UnifiedDiff();
+ diff.setHeader(header);
+ diff.setTailTxt(tail);
+ for (UnifiedDiffFile file : files) {
+ diff.addFile(file);
+ }
+ return diff;
+ }
}
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java
index cd49de57..d57b9233 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java
@@ -21,7 +21,7 @@
*
* @author Tobias Warneke (t.warneke@gmx.net)
*/
-public class UnifiedDiffFile {
+public final class UnifiedDiffFile {
private String diffCommand;
private String fromFile;
@@ -64,4 +64,12 @@ public String getIndex() {
public Patch getPatch() {
return patch;
}
+
+ public static UnifiedDiffFile from(String fromFile, String toFile, Patch patch) {
+ UnifiedDiffFile file = new UnifiedDiffFile();
+ file.setFromFile(fromFile);
+ file.setToFile(toFile);
+ file.patch = patch;
+ return file;
+ }
}
From 983ec680f732dca4f96cf154c2eedddc7405e729 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Mon, 18 Mar 2019 22:36:49 +0100
Subject: [PATCH 023/266]
---
pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
index 82ccd481..b7ca5ea3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -249,7 +249,7 @@
com.puppycrawl.tools
checkstyle
- 6.19
+ 8.18
From 8c86e0f8d7f6d71fcb22f1588aebd73c308b9060 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Tue, 26 Mar 2019 07:58:25 +0100
Subject: [PATCH 024/266] upgraded checkstyle plugin (security alert from
github)
---
pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
index 200c4d14..4fec1a65 100644
--- a/pom.xml
+++ b/pom.xml
@@ -243,7 +243,7 @@
com.puppycrawl.tools
checkstyle
- 6.19
+ 8.12
From 4a83032d7e027beaecd106079f8a50427ba080a2 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Tue, 26 Mar 2019 21:14:54 +0100
Subject: [PATCH 025/266]
---
...DiffParser.java => UnifiedDiffReader.java} | 16 +++---
.../unifieddiff/UnifiedDiffWriter.java | 29 ++++++++++
...erTest.java => UnifiedDiffReaderTest.java} | 12 ++--
.../unifieddiff/UnifiedDiffWriterTest.java | 55 +++++++++++++++++++
4 files changed, 98 insertions(+), 14 deletions(-)
rename src/main/java/com/github/difflib/unifieddiff/{UnifiedDiffParser.java => UnifiedDiffReader.java} (95%)
create mode 100644 src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
rename src/test/java/com/github/difflib/unifieddiff/{UnifiedDiffParserTest.java => UnifiedDiffReaderTest.java} (87%)
create mode 100644 src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java
similarity index 95%
rename from src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
rename to src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java
index 77aa614f..9126ad78 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java
@@ -35,11 +35,11 @@
*
* @author Tobias Warneke (t.warneke@gmx.net)
*/
-public final class UnifiedDiffParser {
+public final class UnifiedDiffReader {
static final Pattern UNIFIED_DIFF_CHUNK_REGEXP = Pattern.compile("^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@");
- private final UnifiedDiffReader READER;
+ private final InternalUnifiedDiffReader READER;
private final UnifiedDiff data = new UnifiedDiff();
private final UnifiedDiffLine[] MAIN_PARSER_RULES = new UnifiedDiffLine[]{
new UnifiedDiffLine(true, "^diff\\s", this::processDiff),
@@ -54,8 +54,8 @@ public final class UnifiedDiffParser {
private UnifiedDiffFile actualFile;
- UnifiedDiffParser(Reader reader) {
- this.READER = new UnifiedDiffReader(reader);
+ UnifiedDiffReader(Reader reader) {
+ this.READER = new InternalUnifiedDiffReader(reader);
}
// schema = [[/^\s+/, normal], [/^diff\s/, start], [/^new file mode \d+$/, new_file],
@@ -103,10 +103,10 @@ static String[] parseFileNames(String line) {
};
}
- private static final Logger LOG = Logger.getLogger(UnifiedDiffParser.class.getName());
+ private static final Logger LOG = Logger.getLogger(UnifiedDiffReader.class.getName());
public static UnifiedDiff parseUnifiedDiff(InputStream stream) throws IOException, UnifiedDiffParserException {
- UnifiedDiffParser parser = new UnifiedDiffParser(new BufferedReader(new InputStreamReader(stream)));
+ UnifiedDiffReader parser = new UnifiedDiffReader(new BufferedReader(new InputStreamReader(stream)));
return parser.parse();
}
@@ -244,11 +244,11 @@ public boolean isStopsHeaderParsing() {
}
}
-class UnifiedDiffReader extends BufferedReader {
+class InternalUnifiedDiffReader extends BufferedReader {
private String lastLine;
- public UnifiedDiffReader(Reader reader) {
+ public InternalUnifiedDiffReader(Reader reader) {
super(reader);
}
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
new file mode 100644
index 00000000..4cbf67ff
--- /dev/null
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2019 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 com.github.difflib.unifieddiff;
+
+import java.io.Writer;
+
+/**
+ *
+ * @author Tobias Warneke (t.warneke@gmx.net)
+ */
+public class UnifiedDiffWriter {
+
+ public static void write(UnifiedDiff diff, Writer writer) {
+
+ }
+}
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java
similarity index 87%
rename from src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java
rename to src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java
index 6fe916d8..38526a8b 100644
--- a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java
@@ -31,9 +31,9 @@
*
* @author Tobias Warneke (t.warneke@gmx.net)
*/
-public class UnifiedDiffParserTest {
+public class UnifiedDiffReaderTest {
- public UnifiedDiffParserTest() {
+ public UnifiedDiffReaderTest() {
}
@BeforeClass
@@ -54,7 +54,7 @@ public void tearDown() {
@Test
public void testSimpleParse() throws IOException {
- UnifiedDiff diff = UnifiedDiffParser.parseUnifiedDiff(UnifiedDiffParserTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));
+ UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(UnifiedDiffReaderTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));
System.out.println(diff);
@@ -69,13 +69,13 @@ public void testSimpleParse() throws IOException {
@Test
public void testParseDiffBlock() {
- String[] files = UnifiedDiffParser.parseFileNames("diff --git a/src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java b/src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java");
+ String[] files = UnifiedDiffReader.parseFileNames("diff --git a/src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java b/src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java");
assertThat(files).containsExactly("src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java", "src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java");
}
@Test
public void testChunkHeaderParsing() {
- Pattern pattern = UnifiedDiffParser.UNIFIED_DIFF_CHUNK_REGEXP;
+ Pattern pattern = UnifiedDiffReader.UNIFIED_DIFF_CHUNK_REGEXP;
Matcher matcher = pattern.matcher("@@ -189,6 +189,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */");
assertTrue(matcher.find());
@@ -86,7 +86,7 @@ public void testChunkHeaderParsing() {
@Test
public void testChunkHeaderParsing2() {
//"^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@.*$"
- Pattern pattern = UnifiedDiffParser.UNIFIED_DIFF_CHUNK_REGEXP;
+ Pattern pattern = UnifiedDiffReader.UNIFIED_DIFF_CHUNK_REGEXP;
Matcher matcher = pattern.matcher("@@ -189,6 +189,7 @@");
assertTrue(matcher.find());
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
new file mode 100644
index 00000000..d73dcb7d
--- /dev/null
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2019 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 com.github.difflib.unifieddiff;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ *
+ * @author Tobias Warneke (t.warneke@gmx.net)
+ */
+public class UnifiedDiffWriterTest {
+
+ public UnifiedDiffWriterTest() {
+ }
+
+ @BeforeClass
+ public static void setUpClass() {
+ }
+
+ @AfterClass
+ public static void tearDownClass() {
+ }
+
+ @Before
+ public void setUp() {
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ @Test
+ public void testSomeMethod() {
+ // TODO review the generated test code and remove the default call to fail.
+ fail("The test case is a prototype.");
+ }
+
+}
From 61a21cbca91ddda3337d315d8768fc0d31482b26 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Tue, 26 Mar 2019 21:36:31 +0100
Subject: [PATCH 026/266]
---
.../github/difflib/unifieddiff/UnifiedDiffWriterTest.java | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
index d73dcb7d..7b8e1576 100644
--- a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
@@ -47,9 +47,8 @@ public void tearDown() {
}
@Test
- public void testSomeMethod() {
- // TODO review the generated test code and remove the default call to fail.
- fail("The test case is a prototype.");
+ public void testWrite() {
+
}
}
From 535ecf3e2a55811769a19fbe977bc89fc637168b Mon Sep 17 00:00:00 2001
From: Oliver Kopp
Date: Sun, 31 Mar 2019 14:18:24 +0200
Subject: [PATCH 027/266] Move CHANGELOG to separte file CHANGELOG.md
For more information on that format see http://keepachangelog.com/
---
CHANGELOG.md | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++
README.md | 33 ---------------------
2 files changed, 82 insertions(+), 33 deletions(-)
create mode 100644 CHANGELOG.md
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 00000000..dda5b33e
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,82 @@
+# Changelog
+
+All notable changes to this project will be documented in this file.
+
+The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
+This project uses a custom versioning scheme (and not [Semantic Versioning](https://semver.org/spec/v2.0.0.html)).
+
+## [Unreleased]
+
+### Changed
+
+* Exchange `0 += 1` for `0 = 1` in UnifiedDiffUtils
+
+## [4.0] – 2019-01-09
+
+### Changed
+
+* moved to organisation **java-diff-utils**
+* changed groupid to **io.github.java-diff-utils** and artifact id to **java-diff-utils**
+
+## [3.0] – 2018-10-18
+
+### Added
+
+* Introduced a process listener to diff algorithms. For long running
+ diffs one could implement some progress information.
+* automatic module name for JDK 9 and higher usage
+
+### Changed
+
+* changed generation of inline diffes, if there are different linefeeds within one diff, then these are excluded from the diff block.
+
+### Removed
+
+* Due to licensing issues Delta.java and DiffAlgorithm.java were removed.
+
+## [2.2] – 2017-11-09
+
+### Added
+
+* released at maven central
+* included checkstyle source code conventions
+* allow configurable splitting of lines to define the blocks to compare (words, characters, phrases).
+
+### Changed
+
+* groupid changed to **com.github.wumpz**, due to maven central releasing
+
+## [2.0] – 2017-08-14
+
+### Added
+
+* support for inline merge
+* integrated JGit (Eclipse Licensed) to provide HistogramDiff to gain speed for large datasets
+
+### Changed
+
+* switch to maven and removed other artifacts
+* changed groupid to **com.github.java-diff-utils** due to different forks at github
+* updated maven plugins
+* JDK 1.8 compatibility, sorry if you have to stick with older versions
+* restructured packages heavily
+* changed API
+* changed Algorithm to provide only cursor positions
+
+### Removed
+
+* removed all kinds of helper classes in favour of new JDK 8 function classes like Predicate
+
+## 1.2
+
+### Added
+
+* JDK 1.5 compatibility
+* Ant build script
+* Generate output in unified diff format (thanks for Bill James)
+
+[Unreleased]: https://github.com/java-diff-utils/java-diff-utils/compare/java-diff-utils-4.0...HEAD
+[4.0]: https://github.com/java-diff-utils/java-diff-utils/compare/diff-utils-3.0...java-diff-utils-4.0
+[3.0]: https://github.com/java-diff-utils/java-diff-utils/compare/diff-utils-2.2...diff-utils-3.0
+[2.2]: https://github.com/java-diff-utils/java-diff-utils/compare/diff-utils-2.0...diff-utils-2.2
+
diff --git a/README.md b/README.md
index c3c335ac..ebc61bd8 100644
--- a/README.md
+++ b/README.md
@@ -52,39 +52,6 @@ This is a test ~senctence~**for diffutils**.
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.
-### Changelog ###
- * Version 4.0-SNAPSHOT
- * moved to organisation **java-diff-utils**
- * changed groupid to **io.github.java-diff-utils** and artifact id to **java-diff-utils**
- * Version 3.0
- * changed generation of inline diffes, if there are different linefeeds within one diff, then these are excluded
- from the diff block.
- * Due to licensing issues Delta.java and DiffAlgorithm.java were removed.
- * Version 2.3-SNAPSHOT
- * Introduced a process listener to diff algorithms. For long running
- diffs one could implement some progress information.
- * automatic module name for JDK 9 and higher usage
- * Version 2.2
- * released at maven central
- * included checkstyle source code conventions
- * groupid changed to **com.github.wumpz**, due to maven central releasing
- * allow configurable splitting of lines to define the blocks to compare (words, characters, phrases).
- * Version 2.0
- * switch to maven and removed other artifacts
- * changed groupid to **com.github.java-diff-utils** due to different forks at github
- * updated maven plugins
- * JDK 1.8 compatibility, sorry if you have to stick with older versions
- * support for inline merge
- * restructured packages heavily
- * changed API
- * changed Algorithm to provide only cursor positions
- * integrated JGit (Eclipse Licensed) to provide HistogramDiff to gain speed for large datasets
- * removed all kinds of helper classes in favour of new JDK 8 function classes like Predicate
- * Version 1.2
- * JDK 1.5 compatibility
- * Ant build script
- * Generate output in unified diff format (thanks for Bill James)
-
## Source Code conventions
Recently a checkstyle process was integrated into the build process. java-diff-utils follows the sun java format convention. There are no TABs allowed. Use spaces.
From a4bb85bab1e9984d2fa996f5d5b9417795aa7d33 Mon Sep 17 00:00:00 2001
From: Oliver Kopp
Date: Sun, 31 Mar 2019 14:29:09 +0200
Subject: [PATCH 028/266] Fix linting issues in README.md
---
README.md | 58 ++++++++++++++++++++++++++++---------------------------
1 file changed, 30 insertions(+), 28 deletions(-)
diff --git a/README.md b/README.md
index c3c335ac..2522bf42 100644
--- a/README.md
+++ b/README.md
@@ -1,21 +1,22 @@
# java-diff-utils
-## Status ##
+## Status
+
[](https://travis-ci.org/java-diff-utils/java-diff-utils)
[](https://www.codacy.com/app/wumpz/java-diff-utils?utm_source=github.com&utm_medium=referral&utm_content=java-diff-utils/java-diff-utils&utm_campaign=Badge_Grade)
[](http://maven-badges.herokuapp.com/maven-central/io.github.java-diff-utils/java-diff-utils)
+## Intro
-## Intro ##
Diff Utils library is an OpenSource library for performing the comparison operations between texts: computing diffs, applying patches, generating unified diffs or parsing them, generating diff output for easy future displaying (like side-by-side view) and so on.
Main reason to build this library was the lack of easy-to-use libraries with all the usual stuff you need while working with diff files. Originally it was inspired by JRCS library and it's nice design of diff module.
**This is originally a fork of java-diff-utils from Google Code Archive.**
-## Examples ##
+## Examples
-Look [here](https://github.com/wumpz/java-diff-utils/wiki) to find more helpful informations and examples.
+Look [here](https://github.com/wumpz/java-diff-utils/wiki) to find more helpful informations and examples.
These two outputs are generated using this java-diff-utils. The source code can also be found at the *Examples* page:
@@ -23,7 +24,6 @@ These two outputs are generated using this java-diff-utils. The source code can
This is a test ~senctence~**for diffutils**.
-
**Producing a side by side view of computed differences.**
|original|new|
@@ -32,23 +32,22 @@ This is a test ~senctence~**for diffutils**.
|This is the second line.|This is the second line.|
|~And here is the finish.~||
+## Main Features
-## Main Features ##
-
- * computing the difference between two texts.
- * capable to hand more than plain ascci. Arrays or List of any type that implements hashCode() and equals() correctly can be subject to differencing using this library
- * patch and unpatch the text with the given patch
- * parsing the unified diff format
- * producing human-readable differences
- * inline difference construction
- * Algorithms:
- * Myer
- * HistogramDiff using JGit Library
+* computing the difference between two texts.
+* capable to hand more than plain ascci. Arrays or List of any type that implements hashCode() and equals() correctly can be subject to differencing using this library
+* patch and unpatch the text with the given patch
+* parsing the unified diff format
+* producing human-readable differences
+* inline difference construction
+* Algorithms:
+ * Myer
+ * HistogramDiff using JGit Library
-### Algoritms ###
+### Algoritms
* Myer's diff
-* HistogramDiff
+* HistogramDiff
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.
@@ -91,24 +90,26 @@ Recently a checkstyle process was integrated into the build process. java-diff-u
```java
public static Patch diff(List original, List revised,
- BiPredicate equalizer) throws DiffException {
- if (equalizer != null) {
- return DiffUtils.diff(original, revised,
- new MyersDiff<>(equalizer));
- }
- return DiffUtils.diff(original, revised, new MyersDiff<>());
+ BiPredicate equalizer) throws DiffException {
+ if (equalizer != null) {
+ return DiffUtils.diff(original, revised,
+ new MyersDiff<>(equalizer));
+ }
+ return DiffUtils.diff(original, revised, new MyersDiff<>());
}
```
This is a valid piece of source code:
+
* blocks without braces are not allowed
* after control statements (if, while, for) a whitespace is expected
* the opening brace should be in the same line as the control statement
-### To Install ###
+### To Install
Just add the code below to your maven dependencies:
-```
+
+```xml
io.github.java-diff-utils
java-diff-utils
@@ -118,7 +119,7 @@ Just add the code below to your maven dependencies:
Attention. We changed groupid and artifactid. Starting with version 4 you have to use:
-```
+```xml
io.github.java-diff-utils
java-diff-utils
@@ -127,7 +128,8 @@ Attention. We changed groupid and artifactid. Starting with version 4 you have t
```
or using gradle:
-```
+
+```groovy
// https://mvnrepository.com/artifact/com.github.wumpz/diffutils
compile group: 'com.github.wumpz', name: 'diffutils', version: '2.2'
```
From 6321d5327c50e8a4516bbd254798035f940e84af Mon Sep 17 00:00:00 2001
From: Oliver Kopp
Date: Sun, 31 Mar 2019 14:33:54 +0200
Subject: [PATCH 029/266] Add support for IntelliJ to .gitignore
---
.gitignore | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/.gitignore b/.gitignore
index 0643ce7d..54a9c8ca 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
-/target/
-/nbproject/
\ No newline at end of file
+.idea/
+build/
+nbproject/
+target/
From 13f2f5ede1aac32a3079836d0451cd03033dd507 Mon Sep 17 00:00:00 2001
From: 0xflotus <0xflotus@gmail.com>
Date: Tue, 2 Apr 2019 18:00:59 +0200
Subject: [PATCH 030/266] fixed Algorithms and Ascii
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 445729c4..03d4d100 100644
--- a/README.md
+++ b/README.md
@@ -35,7 +35,7 @@ This is a test ~senctence~**for diffutils**.
## Main Features
* computing the difference between two texts.
-* capable to hand more than plain ascci. Arrays or List of any type that implements hashCode() and equals() correctly can be subject to differencing using this library
+* capable to hand more than plain ascii. Arrays or List of any type that implements hashCode() and equals() correctly can be subject to differencing using this library
* patch and unpatch the text with the given patch
* parsing the unified diff format
* producing human-readable differences
@@ -44,7 +44,7 @@ This is a test ~senctence~**for diffutils**.
* Myer
* HistogramDiff using JGit Library
-### Algoritms
+### Algorithms
* Myer's diff
* HistogramDiff
From 4c1a0bc07288b2c5752f2f9ac1dc343cedb3985d Mon Sep 17 00:00:00 2001
From: wumpz
Date: Mon, 15 Apr 2019 00:17:53 +0200
Subject: [PATCH 031/266]
---
.../unifieddiff/UnifiedDiffWriter.java | 25 +++++++++++-
.../unifieddiff/UnifiedDiffReaderTest.java | 23 -----------
.../unifieddiff/UnifiedDiffWriterTest.java | 40 +++++++++----------
3 files changed, 43 insertions(+), 45 deletions(-)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
index 4cbf67ff..6c552bc6 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
@@ -15,6 +15,7 @@
*/
package com.github.difflib.unifieddiff;
+import java.io.IOException;
import java.io.Writer;
/**
@@ -23,7 +24,29 @@
*/
public class UnifiedDiffWriter {
- public static void write(UnifiedDiff diff, Writer writer) {
+ public static void write(UnifiedDiff diff, Writer writer) throws IOException {
+ writer.write(diff.getHeader());
+ for (UnifiedDiffFile file : diff.getFiles()) {
+ writeOrNothing(writer, file.getDiffCommand());
+ if (file.getIndex() != null) {
+ writer.write("index " + file.getIndex() + "\n");
+ }
+ if (file.getFromFile() != null) {
+ writer.write("--- " + file.getFromFile() + "\n");
+ }
+ if (file.getToFile() != null) {
+ writer.write("+++ " + file.getToFile() + "\n");
+ }
+
+ }
+
+ writer.write(diff.getTail());
+ }
+
+ private static void writeOrNothing(Writer writer, String str) throws IOException {
+ if (str != null) {
+ writer.append(str).append("\n");
+ }
}
}
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java
index 38526a8b..cd94f2ae 100644
--- a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java
@@ -19,12 +19,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.assertj.core.api.Java6Assertions.assertThat;
-import org.junit.After;
-import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-import org.junit.Before;
-import org.junit.BeforeClass;
import org.junit.Test;
/**
@@ -33,25 +29,6 @@
*/
public class UnifiedDiffReaderTest {
- public UnifiedDiffReaderTest() {
- }
-
- @BeforeClass
- public static void setUpClass() {
- }
-
- @AfterClass
- public static void tearDownClass() {
- }
-
- @Before
- public void setUp() {
- }
-
- @After
- public void tearDown() {
- }
-
@Test
public void testSimpleParse() throws IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(UnifiedDiffReaderTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
index 7b8e1576..782b35d7 100644
--- a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
@@ -15,10 +15,14 @@
*/
package com.github.difflib.unifieddiff;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import org.junit.Test;
/**
@@ -30,25 +34,19 @@ public class UnifiedDiffWriterTest {
public UnifiedDiffWriterTest() {
}
- @BeforeClass
- public static void setUpClass() {
- }
-
- @AfterClass
- public static void tearDownClass() {
- }
-
- @Before
- public void setUp() {
- }
-
- @After
- public void tearDown() {
- }
-
@Test
- public void testWrite() {
+ public void testWrite() throws URISyntaxException, IOException {
+ String str = readFile(UnifiedDiffReaderTest.class.getResource("jsqlparser_patch_1.diff").toURI(), Charset.defaultCharset());
+ UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(str.getBytes()));
+ StringWriter writer = new StringWriter();
+ UnifiedDiffWriter.write(diff, writer);
+ System.out.println(writer.toString());
}
+ static String readFile(URI path, Charset encoding)
+ throws IOException {
+ byte[] encoded = Files.readAllBytes(Paths.get(path));
+ return new String(encoded, encoding);
+ }
}
From 8c6679e2950ffa53ce988e38fa017fa04e2b3451 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Mon, 15 Apr 2019 00:51:14 +0200
Subject: [PATCH 032/266]
---
.../com/github/difflib/unifieddiff/UnifiedDiffWriter.java | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
index 6c552bc6..5645e191 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
@@ -40,8 +40,9 @@ public static void write(UnifiedDiff diff, Writer writer) throws IOException {
}
}
-
- writer.write(diff.getTail());
+ if (diff.getTail() != null) {
+ writer.write("--\n" + diff.getTail());
+ }
}
private static void writeOrNothing(Writer writer, String str) throws IOException {
From ecd834f3f53c00f4d55baa75fbada4aa338014db Mon Sep 17 00:00:00 2001
From: wumpz
Date: Tue, 30 Apr 2019 23:55:38 +0200
Subject: [PATCH 033/266]
---
.../java/com/github/difflib/patch/Chunk.java | 10 +-
.../unifieddiff/UnifiedDiffWriter.java | 162 +++++++++++++++++-
.../unifieddiff/UnifiedDiffReaderTest.java | 21 +++
3 files changed, 182 insertions(+), 11 deletions(-)
diff --git a/src/main/java/com/github/difflib/patch/Chunk.java b/src/main/java/com/github/difflib/patch/Chunk.java
index 6c1a9918..70b57223 100644
--- a/src/main/java/com/github/difflib/patch/Chunk.java
+++ b/src/main/java/com/github/difflib/patch/Chunk.java
@@ -15,6 +15,7 @@
*/
package com.github.difflib.patch;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@@ -23,9 +24,10 @@
* Holds the information about the part of text involved in the diff process
*
*
- * Text is represented as Object[] because the diff engine is capable of handling more than plain ascci. In
- * fact, arrays or lists of any type that implements {@link java.lang.Object#hashCode hashCode()} and
- * {@link java.lang.Object#equals equals()} correctly can be subject to differencing using this library.
+ * Text is represented as Object[] because the diff engine is capable of handling more
+ * than plain ascci. In fact, arrays or lists of any type that implements
+ * {@link java.lang.Object#hashCode hashCode()} and {@link java.lang.Object#equals equals()}
+ * correctly can be subject to differencing using this library.
*
*
* @author writer) throws IOException {
+ writer.accept(diff.getHeader());
for (UnifiedDiffFile file : diff.getFiles()) {
writeOrNothing(writer, file.getDiffCommand());
if (file.getIndex() != null) {
- writer.write("index " + file.getIndex() + "\n");
+ writer.accept("index " + file.getIndex());
}
if (file.getFromFile() != null) {
- writer.write("--- " + file.getFromFile() + "\n");
+ writer.accept("--- " + file.getFromFile());
}
if (file.getToFile() != null) {
- writer.write("+++ " + file.getToFile() + "\n");
+ writer.accept("+++ " + file.getToFile());
}
+ List> patchDeltas = new ArrayList<>(
+ file.getPatch().getDeltas());
+
+ List> deltas = new ArrayList<>();
+
+ int contextSize = 0;
+ List originalLines = new ArrayList<>();
+
+ AbstractDelta delta = patchDeltas.get(0);
+ deltas.add(delta); // add the first Delta to the current set
+ // if there's more than 1 Delta, we may need to output them together
+ if (patchDeltas.size() > 1) {
+ for (int i = 1; i < patchDeltas.size(); i++) {
+ int position = delta.getSource().getPosition();
+
+ // Check if the next Delta is too close to the current
+ // position.
+ // And if it is, add it to the current set
+ AbstractDelta nextDelta = patchDeltas.get(i);
+ if ((position + delta.getSource().size() + contextSize) >= (nextDelta
+ .getSource().getPosition() - contextSize)) {
+ deltas.add(nextDelta);
+ } else {
+ // if it isn't, output the current set,
+ // then create a new set and add the current Delta to
+ // it.
+ processDeltas(writer, originalLines, deltas, contextSize);
+ deltas.clear();
+ deltas.add(nextDelta);
+ }
+ delta = nextDelta;
+ }
+
+ }
+ // don't forget to process the last set of Deltas
+ processDeltas(writer, originalLines, deltas, contextSize);
+
}
if (diff.getTail() != null) {
- writer.write("--\n" + diff.getTail());
+ writer.accept("--");
+ writer.accept(diff.getTail());
+ }
+ }
+
+ private static void processDeltas(Consumer writer,
+ List origLines,
+ List> deltas, int contextSize) {
+ List buffer = new ArrayList<>();
+ int origTotal = 0; // counter for total lines output from Original
+ int revTotal = 0; // counter for total lines output from Original
+ int line;
+
+ AbstractDelta curDelta = deltas.get(0);
+
+ // NOTE: +1 to overcome the 0-offset Position
+ int origStart = curDelta.getSource().getPosition() + 1 - contextSize;
+ if (origStart < 1) {
+ origStart = 1;
+ }
+
+ int revStart = curDelta.getTarget().getPosition() + 1 - contextSize;
+ if (revStart < 1) {
+ revStart = 1;
+ }
+
+ // find the start of the wrapper context code
+ int contextStart = curDelta.getSource().getPosition() - contextSize;
+ if (contextStart < 0) {
+ contextStart = 0; // clamp to the start of the file
+ }
+
+ // output the context before the first Delta
+ for (line = contextStart; line < curDelta.getSource().getPosition(); line++) { //
+ buffer.add(" " + origLines.get(line));
+ origTotal++;
+ revTotal++;
+ }
+
+ // output the first Delta
+ getDeltaText(txt -> buffer.add(txt), curDelta);
+ origTotal += curDelta.getSource().getLines().size();
+ revTotal += curDelta.getTarget().getLines().size();
+
+ int deltaIndex = 1;
+ while (deltaIndex < deltas.size()) { // for each of the other Deltas
+ AbstractDelta nextDelta = deltas.get(deltaIndex);
+ int intermediateStart = curDelta.getSource().getPosition()
+ + curDelta.getSource().getLines().size();
+ for (line = intermediateStart; line < nextDelta.getSource()
+ .getPosition(); line++) {
+ // output the code between the last Delta and this one
+ buffer.add(" " + origLines.get(line));
+ origTotal++;
+ revTotal++;
+ }
+ getDeltaText(txt -> buffer.add(txt), nextDelta); // output the Delta
+ origTotal += nextDelta.getSource().getLines().size();
+ revTotal += nextDelta.getTarget().getLines().size();
+ curDelta = nextDelta;
+ deltaIndex++;
+ }
+
+ // Now output the post-Delta context code, clamping the end of the file
+ contextStart = curDelta.getSource().getPosition()
+ + curDelta.getSource().getLines().size();
+ for (line = contextStart; (line < (contextStart + contextSize))
+ && (line < origLines.size()); line++) {
+ buffer.add(" " + origLines.get(line));
+ origTotal++;
+ revTotal++;
+ }
+
+ // Create and insert the block header, conforming to the Unified Diff
+ // standard
+ writer.accept("@@ -" + origStart + "," + origTotal + " +" + revStart + "," + revTotal + " @@");
+ buffer.forEach(txt -> {
+ writer.accept(txt);
+ });
+ }
+
+ /**
+ * getDeltaText returns the lines to be added to the Unified Diff text from the Delta parameter
+ *
+ * @param delta - the Delta to output
+ * @return list of String lines of code.
+ * @author Bill James (tankerbay@gmail.com)
+ */
+ private static void getDeltaText(Consumer writer, AbstractDelta delta) {
+ for (String line : delta.getSource().getLines()) {
+ writer.accept("-" + line);
+ }
+ for (String line : delta.getTarget().getLines()) {
+ writer.accept("+" + line);
}
}
- private static void writeOrNothing(Writer writer, String str) throws IOException {
+ private static void writeOrNothing(Consumer writer, String str) throws IOException {
if (str != null) {
- writer.append(str).append("\n");
+ writer.accept(str);
}
}
}
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java
index cd94f2ae..ac372fa4 100644
--- a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java
@@ -15,6 +15,7 @@
*/
package com.github.difflib.unifieddiff;
+import com.github.difflib.patch.AbstractDelta;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -71,4 +72,24 @@ public void testChunkHeaderParsing2() {
assertEquals("189", matcher.group(3));
}
+ @Test
+ public void testSimpleParse2() throws IOException {
+ UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(UnifiedDiffReaderTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));
+
+ System.out.println(diff);
+
+ assertThat(diff.getFiles().size()).isEqualTo(2);
+
+ UnifiedDiffFile file1 = diff.getFiles().get(0);
+ assertThat(file1.getFromFile()).isEqualTo("src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt");
+ assertThat(file1.getPatch().getDeltas().size()).isEqualTo(3);
+
+ AbstractDelta first = file1.getPatch().getDeltas().get(0);
+
+ assertThat(first.getSource().size()).isGreaterThan(0);
+ assertThat(first.getTarget().size()).isGreaterThan(0);
+
+ assertThat(diff.getTail()).isEqualTo("2.17.1.windows.2\n\n");
+ }
+
}
From 9704b62970e232e654a0b1230e32aece51b799c8 Mon Sep 17 00:00:00 2001
From: Adrian Kunz
Date: Fri, 3 May 2019 10:27:38 +0200
Subject: [PATCH 034/266] Update Gradle Installation
Updated the Group, Name, Version and Link in the Gradle Installation snippet.
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 03d4d100..8287302a 100644
--- a/README.md
+++ b/README.md
@@ -97,6 +97,6 @@ Attention. We changed groupid and artifactid. Starting with version 4 you have t
or using gradle:
```groovy
-// https://mvnrepository.com/artifact/com.github.wumpz/diffutils
-compile group: 'com.github.wumpz', name: 'diffutils', version: '2.2'
+// https://mvnrepository.com/artifact/io.github.java-diff-utils/java-diff-utils
+compile group: 'io.github.java-diff-utils', name: 'java-diff-utils', version: '4.0'
```
From 89f2659dd70a3020b84e9a9c72113b3dd1ce7f19 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Tue, 7 May 2019 09:09:34 +0200
Subject: [PATCH 035/266] fixes #41
---
.../github/difflib/text/DiffRowGenerator.java | 30 +++++++++++++++----
.../com/github/difflib/text/StringUtils.java | 6 ----
.../difflib/text/DiffRowGeneratorTest.java | 30 +++++++++++++++++++
.../github/difflib/text/StringUtilsTest.java | 9 ------
4 files changed, 55 insertions(+), 20 deletions(-)
diff --git a/src/main/java/com/github/difflib/text/DiffRowGenerator.java b/src/main/java/com/github/difflib/text/DiffRowGenerator.java
index c0267345..c2734211 100644
--- a/src/main/java/com/github/difflib/text/DiffRowGenerator.java
+++ b/src/main/java/com/github/difflib/text/DiffRowGenerator.java
@@ -29,6 +29,7 @@
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import static java.util.stream.Collectors.toList;
/**
* This class for generating DiffRows for side-by-sidy view. You can customize the way of generating. For example, show
@@ -42,13 +43,16 @@
* ignoreWhiteSpaces(true).columnWidth(100).build();
*
*/
-public class DiffRowGenerator {
+public final class DiffRowGenerator {
public static final BiPredicate DEFAULT_EQUALIZER = Object::equals;
public static final BiPredicate IGNORE_WHITESPACE_EQUALIZER = (original, revised)
-> adjustWhitespace(original).equals(adjustWhitespace(revised));
+ public static final Function LINE_NORMALIZER_FOR_HTML = StringUtils::normalize;
+
+
/**
* Splitting lines by character to achieve char by char diff checking.
*/
@@ -60,6 +64,7 @@ public class DiffRowGenerator {
return list;
};
public static final Pattern SPLIT_BY_WORD_PATTERN = Pattern.compile("\\s+|[,.\\[\\](){}/\\\\*+\\-#]");
+
/**
* Splitting lines by word to achieve word by word diff checking.
*/
@@ -144,6 +149,7 @@ static void wrapInTag(List sequence, int startPosition,
private final Function newTag;
private final Function oldTag;
private final boolean reportLinesUnchanged;
+ private final Function lineNormalizer;
private final boolean showInlineDiffs;
@@ -157,8 +163,10 @@ private DiffRowGenerator(Builder builder) {
inlineDiffSplitter = builder.inlineDiffSplitter;
equalizer = ignoreWhiteSpaces ? IGNORE_WHITESPACE_EQUALIZER : DEFAULT_EQUALIZER;
reportLinesUnchanged = builder.reportLinesUnchanged;
+ lineNormalizer = builder.lineNormalizer;
Objects.requireNonNull(inlineDiffSplitter);
+ Objects.requireNonNull(lineNormalizer);
}
/**
@@ -258,14 +266,20 @@ private DiffRow buildDiffRowWithoutNormalizing(Tag type, String orgline, String
StringUtils.wrapText(newline, columnWidth));
}
+ List normalizeLines(List list) {
+ return list.stream()
+ .map(lineNormalizer::apply)
+ .collect(toList());
+ }
+
/**
* Add the inline diffs for given delta
*
* @param delta the given delta
*/
private List generateInlineDiffs(AbstractDelta delta) throws DiffException {
- List orig = StringUtils.normalize(delta.getSource().getLines());
- List rev = StringUtils.normalize(delta.getTarget().getLines());
+ List orig = normalizeLines(delta.getSource().getLines());
+ List rev = normalizeLines(delta.getTarget().getLines());
List origList;
List revList;
String joinedOrig = String.join("\n", orig);
@@ -334,9 +348,9 @@ private List generateInlineDiffs(AbstractDelta delta) throws Di
private String preprocessLine(String line) {
if (columnWidth == 0) {
- return StringUtils.normalize(line);
+ return lineNormalizer.apply(line);
} else {
- return StringUtils.wrapText(StringUtils.normalize(line), columnWidth);
+ return StringUtils.wrapText(lineNormalizer.apply(line), columnWidth);
}
}
@@ -358,6 +372,7 @@ public static class Builder {
private boolean mergeOriginalRevised = false;
private boolean reportLinesUnchanged = false;
private Function> inlineDiffSplitter = SPLITTER_BY_CHARACTER;
+ private Function lineNormalizer = LINE_NORMALIZER_FOR_HTML;
private Builder() {
}
@@ -463,5 +478,10 @@ public Builder inlineDiffBySplitter(Function> inlineDiffSpl
this.inlineDiffSplitter = inlineDiffSplitter;
return this;
}
+
+ public Builder lineNormalizer(Function lineNormalizer) {
+ this.lineNormalizer = lineNormalizer;
+ return this;
+ }
}
}
diff --git a/src/main/java/com/github/difflib/text/StringUtils.java b/src/main/java/com/github/difflib/text/StringUtils.java
index 3a2dab05..a1425488 100644
--- a/src/main/java/com/github/difflib/text/StringUtils.java
+++ b/src/main/java/com/github/difflib/text/StringUtils.java
@@ -34,12 +34,6 @@ public static String normalize(String str) {
return htmlEntites(str).replace("\t", " ");
}
- public static List normalize(List list) {
- return list.stream()
- .map(StringUtils::normalize)
- .collect(toList());
- }
-
public static List wrapText(List list, int columnWidth) {
return list.stream()
.map(line -> wrapText(line, columnWidth))
diff --git a/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java b/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java
index f9e5c710..752560b2 100644
--- a/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java
+++ b/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java
@@ -5,6 +5,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import static java.util.stream.Collectors.toList;
@@ -28,6 +29,16 @@ public void testGenerator_Default() throws DiffException {
assertEquals(3, rows.size());
}
+ /**
+ * Test of normalize method, of class StringUtils.
+ */
+ @Test
+ public void testNormalize_List() {
+ DiffRowGenerator generator = DiffRowGenerator.create()
+ .build();
+ assertEquals(Collections.singletonList(" test"), generator.normalizeLines(Collections.singletonList("\ttest")));
+ }
+
@Test
public void testGenerator_Default2() throws DiffException {
String first = "anything \n \nother";
@@ -375,4 +386,23 @@ public void testGeneratorIssue22_3() throws DiffException {
assertEquals("[[CHANGE,This is a test ~senctence~.,This is a test **for diffutils**.], [CHANGE,,**This is the second line.**], [CHANGE,,**And one more.**]]",
rows.toString());
}
+
+ @Test
+ public void testGeneratorIssue41DefaultNormalizer() throws DiffException {
+ DiffRowGenerator generator = DiffRowGenerator.create()
+ .build();
+ List rows = generator.generateDiffRows(Arrays.asList("<"), Arrays.asList("<"));
+ assertEquals("[[EQUAL,<,<]]", rows.toString());
+ }
+
+ @Test
+ public void testGeneratorIssue41UserNormalizer() throws DiffException {
+ DiffRowGenerator generator = DiffRowGenerator.create()
+ .lineNormalizer(str -> str.replace("\t", " "))
+ .build();
+ List rows = generator.generateDiffRows(Arrays.asList("<"), Arrays.asList("<"));
+ assertEquals("[[EQUAL,<,<]]", rows.toString());
+ rows = generator.generateDiffRows(Arrays.asList("\t<"), Arrays.asList("<"));
+ assertEquals("[[CHANGE, <,<]]", rows.toString());
+ }
}
diff --git a/src/test/java/com/github/difflib/text/StringUtilsTest.java b/src/test/java/com/github/difflib/text/StringUtilsTest.java
index 2b120d09..93076cc1 100644
--- a/src/test/java/com/github/difflib/text/StringUtilsTest.java
+++ b/src/test/java/com/github/difflib/text/StringUtilsTest.java
@@ -15,7 +15,6 @@
*/
package com.github.difflib.text;
-import java.util.Collections;
import static org.junit.Assert.*;
import org.junit.Test;
@@ -41,14 +40,6 @@ public void testNormalize_String() {
assertEquals(" test", StringUtils.normalize("\ttest"));
}
- /**
- * Test of normalize method, of class StringUtils.
- */
- @Test
- public void testNormalize_List() {
- assertEquals(Collections.singletonList(" test"), StringUtils.normalize(Collections.singletonList("\ttest")));
- }
-
/**
* Test of wrapText method, of class StringUtils.
*/
From a8d968cc1dc3a1363eed00e6b8bbd7d794d4af81 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Tue, 7 May 2019 09:57:52 +0200
Subject: [PATCH 036/266] added some documentation
---
.../github/difflib/text/DiffRowGenerator.java | 28 ++++++++++++++++---
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/src/main/java/com/github/difflib/text/DiffRowGenerator.java b/src/main/java/com/github/difflib/text/DiffRowGenerator.java
index c2734211..90babc71 100644
--- a/src/main/java/com/github/difflib/text/DiffRowGenerator.java
+++ b/src/main/java/com/github/difflib/text/DiffRowGenerator.java
@@ -400,7 +400,8 @@ public Builder ignoreWhiteSpaces(boolean val) {
}
/**
- * Give the originial old and new text lines to Diffrow without any additional processing.
+ * Give the originial old and new text lines to Diffrow without any additional processing and without any tags to
+ * highlight the change.
*
* @param val the value to set. Default: false.
* @return builder with configured reportLinesUnWrapped parameter
@@ -433,7 +434,7 @@ public Builder newTag(Function generator) {
}
/**
- * Set the column with of generated lines of original and revised texts.
+ * Set the column width of generated lines of original and revised texts.
*
* @param width the width to set. Making it < 0 doesn't have any sense. Default 80. @return builder with config
* ured ignoreBlankLines parameter
@@ -466,19 +467,38 @@ public Builder mergeOriginalRevised(boolean mergeOriginalRevised) {
}
/**
- * Per default each character is separatly processed. This variant introduces processing by word, which should
- * deliver no in word changes.
+ * Per default each character is separatly processed. This variant introduces processing by word, which does not
+ * deliver in word changes. Therefore the whole word will be tagged as changed:
+ *
+ *
+ * false: (aBa : aba) -- changed: a(B)a : a(b)a
+ * true: (aBa : aba) -- changed: (aBa) : (aba)
+ *
*/
public Builder inlineDiffByWord(boolean inlineDiffByWord) {
inlineDiffSplitter = inlineDiffByWord ? SPLITTER_BY_WORD : SPLITTER_BY_CHARACTER;
return this;
}
+ /**
+ * To provide some customized splitting a splitter can be provided. Here someone could think about sentence splitter,
+ * comma splitter or stuff like that.
+ *
+ * @param inlineDiffSplitter
+ * @return
+ */
public Builder inlineDiffBySplitter(Function> inlineDiffSplitter) {
this.inlineDiffSplitter = inlineDiffSplitter;
return this;
}
+ /**
+ * By default DiffRowGenerator preprocesses lines for HTML output. Tabs and special HTML characters like "<"
+ * are replaced with its encoded value. To change this you can provide a customized line normalizer here.
+ *
+ * @param lineNormalizer
+ * @return
+ */
public Builder lineNormalizer(Function lineNormalizer) {
this.lineNormalizer = lineNormalizer;
return this;
From a34c185a44a391626c40a9c6f8d3a331a2dfc598 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Wed, 8 May 2019 23:31:52 +0200
Subject: [PATCH 037/266]
---
.../unifieddiff/UnifiedDiffWriter.java | 52 +++++++++----------
1 file changed, 25 insertions(+), 27 deletions(-)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
index 46b84ac1..004d914d 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
@@ -83,7 +83,7 @@ public static void write(UnifiedDiff diff, Consumer writer) throws IOExc
// if it isn't, output the current set,
// then create a new set and add the current Delta to
// it.
- processDeltas(writer, originalLines, deltas, contextSize);
+ processDeltas(writer, deltas);
deltas.clear();
deltas.add(nextDelta);
}
@@ -92,7 +92,7 @@ public static void write(UnifiedDiff diff, Consumer writer) throws IOExc
}
// don't forget to process the last set of Deltas
- processDeltas(writer, originalLines, deltas, contextSize);
+ processDeltas(writer, deltas);
}
if (diff.getTail() != null) {
@@ -102,8 +102,7 @@ public static void write(UnifiedDiff diff, Consumer writer) throws IOExc
}
private static void processDeltas(Consumer writer,
- List origLines,
- List> deltas, int contextSize) {
+ List> deltas) {
List buffer = new ArrayList<>();
int origTotal = 0; // counter for total lines output from Original
int revTotal = 0; // counter for total lines output from Original
@@ -112,29 +111,28 @@ private static void processDeltas(Consumer writer,
AbstractDelta curDelta = deltas.get(0);
// NOTE: +1 to overcome the 0-offset Position
- int origStart = curDelta.getSource().getPosition() + 1 - contextSize;
+ int origStart = curDelta.getSource().getPosition() + 1;
if (origStart < 1) {
origStart = 1;
}
- int revStart = curDelta.getTarget().getPosition() + 1 - contextSize;
+ int revStart = curDelta.getTarget().getPosition() + 1;
if (revStart < 1) {
revStart = 1;
}
// find the start of the wrapper context code
- int contextStart = curDelta.getSource().getPosition() - contextSize;
+ int contextStart = curDelta.getSource().getPosition();
if (contextStart < 0) {
contextStart = 0; // clamp to the start of the file
}
- // output the context before the first Delta
- for (line = contextStart; line < curDelta.getSource().getPosition(); line++) { //
- buffer.add(" " + origLines.get(line));
- origTotal++;
- revTotal++;
- }
-
+// // output the context before the first Delta
+// for (line = contextStart; line < curDelta.getSource().getPosition(); line++) { //
+// buffer.add(" " + curDelta.getSource().getLines().get(line - contextStart));
+// origTotal++;
+// revTotal++;
+// }
// output the first Delta
getDeltaText(txt -> buffer.add(txt), curDelta);
origTotal += curDelta.getSource().getLines().size();
@@ -145,13 +143,13 @@ private static void processDeltas(Consumer writer,
AbstractDelta nextDelta = deltas.get(deltaIndex);
int intermediateStart = curDelta.getSource().getPosition()
+ curDelta.getSource().getLines().size();
- for (line = intermediateStart; line < nextDelta.getSource()
- .getPosition(); line++) {
- // output the code between the last Delta and this one
- buffer.add(" " + origLines.get(line));
- origTotal++;
- revTotal++;
- }
+// for (line = intermediateStart; line < nextDelta.getSource()
+// .getPosition(); line++) {
+// // output the code between the last Delta and this one
+// buffer.add(" " + origLines.get(line));
+// origTotal++;
+// revTotal++;
+// }
getDeltaText(txt -> buffer.add(txt), nextDelta); // output the Delta
origTotal += nextDelta.getSource().getLines().size();
revTotal += nextDelta.getTarget().getLines().size();
@@ -162,12 +160,12 @@ private static void processDeltas(Consumer writer,
// Now output the post-Delta context code, clamping the end of the file
contextStart = curDelta.getSource().getPosition()
+ curDelta.getSource().getLines().size();
- for (line = contextStart; (line < (contextStart + contextSize))
- && (line < origLines.size()); line++) {
- buffer.add(" " + origLines.get(line));
- origTotal++;
- revTotal++;
- }
+// for (line = contextStart; (line < (contextStart + contextSize))
+// && (line < origLines.size()); line++) {
+// buffer.add(" " + origLines.get(line));
+// origTotal++;
+// revTotal++;
+// }
// Create and insert the block header, conforming to the Unified Diff
// standard
From 16cd9c6484e4625f6e3f245e5785067dad586b59 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Wed, 5 Jun 2019 00:05:40 +0200
Subject: [PATCH 038/266]
---
.../unifieddiff/UnifiedDiffReader.java | 12 +-
.../unifieddiff/UnifiedDiffWriter.java | 49 +++----
.../difflib/GenerateUnifiedDiffTest.java | 2 +-
.../unifieddiff/UnifiedDiffRoundTripTest.java | 134 ++++++++++++++++++
.../unifieddiff/UnifiedDiffWriterTest.java | 4 +-
5 files changed, 168 insertions(+), 33 deletions(-)
create mode 100644 src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java
index 9126ad78..94841e1c 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java
@@ -132,7 +132,7 @@ private void initFileIfNecessary() {
}
}
- public void processDiff(MatchResult match, String line) {
+ private void processDiff(MatchResult match, String line) {
initFileIfNecessary();
LOG.log(Level.INFO, "start {0}", line);
String[] fromTo = parseFileNames(READER.lastLine());
@@ -158,23 +158,23 @@ private void finalizeChunk() {
}
}
- public void processNormalLine(MatchResult match, String line) {
+ private void processNormalLine(MatchResult match, String line) {
String cline = line.substring(1);
originalTxt.add(cline);
revisedTxt.add(cline);
}
- public void processAddLine(MatchResult match, String line) {
+ private void processAddLine(MatchResult match, String line) {
String cline = line.substring(1);
revisedTxt.add(cline);
}
- public void processDelLine(MatchResult match, String line) {
+ private void processDelLine(MatchResult match, String line) {
String cline = line.substring(1);
originalTxt.add(cline);
}
- public void processChunk(MatchResult match, String chunkStart) {
+ private void processChunk(MatchResult match, String chunkStart) {
finalizeChunk();
old_ln = match.group(1) == null ? 1 : Integer.parseInt(match.group(1));
new_ln = match.group(3) == null ? 1 : Integer.parseInt(match.group(3));
@@ -186,7 +186,7 @@ public void processChunk(MatchResult match, String chunkStart) {
}
}
- public void processIndex(MatchResult match, String line) {
+ private void processIndex(MatchResult match, String line) {
initFileIfNecessary();
LOG.log(Level.INFO, "index {0}", line);
actualFile.setIndex(line.substring(6));
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
index 004d914d..5ed92f66 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
@@ -21,28 +21,29 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
+import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
- *
+ * @todo use an instance to store contextSize and originalLinesProvider.
* @author Tobias Warneke (t.warneke@gmx.net)
*/
public class UnifiedDiffWriter {
private static final Logger LOG = Logger.getLogger(UnifiedDiffWriter.class.getName());
- public static void write(UnifiedDiff diff, Writer writer) throws IOException {
- write(diff, line -> {
+ public static void write(UnifiedDiff diff, Function> originalLinesProvider, Writer writer, int contextSize) throws IOException {
+ write(diff, originalLinesProvider, line -> {
try {
writer.append(line).append("\n");
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
}
- });
+ }, contextSize);
}
- public static void write(UnifiedDiff diff, Consumer writer) throws IOException {
+ public static void write(UnifiedDiff diff, Function> originalLinesProvider, Consumer writer, int contextSize) throws IOException {
writer.accept(diff.getHeader());
for (UnifiedDiffFile file : diff.getFiles()) {
@@ -57,14 +58,13 @@ public static void write(UnifiedDiff diff, Consumer writer) throws IOExc
writer.accept("+++ " + file.getToFile());
}
+ List originalLines = originalLinesProvider.apply(file.getFromFile());
+
List> patchDeltas = new ArrayList<>(
file.getPatch().getDeltas());
List> deltas = new ArrayList<>();
- int contextSize = 0;
- List originalLines = new ArrayList<>();
-
AbstractDelta delta = patchDeltas.get(0);
deltas.add(delta); // add the first Delta to the current set
// if there's more than 1 Delta, we may need to output them together
@@ -83,7 +83,7 @@ public static void write(UnifiedDiff diff, Consumer writer) throws IOExc
// if it isn't, output the current set,
// then create a new set and add the current Delta to
// it.
- processDeltas(writer, deltas);
+ processDeltas(writer, originalLines, deltas, contextSize);
deltas.clear();
deltas.add(nextDelta);
}
@@ -92,7 +92,7 @@ public static void write(UnifiedDiff diff, Consumer writer) throws IOExc
}
// don't forget to process the last set of Deltas
- processDeltas(writer, deltas);
+ processDeltas(writer, originalLines, deltas, contextSize);
}
if (diff.getTail() != null) {
@@ -102,7 +102,8 @@ public static void write(UnifiedDiff diff, Consumer writer) throws IOExc
}
private static void processDeltas(Consumer writer,
- List> deltas) {
+ List origLines, List> deltas,
+ int contextSize) {
List buffer = new ArrayList<>();
int origTotal = 0; // counter for total lines output from Original
int revTotal = 0; // counter for total lines output from Original
@@ -143,13 +144,13 @@ private static void processDeltas(Consumer writer,
AbstractDelta nextDelta = deltas.get(deltaIndex);
int intermediateStart = curDelta.getSource().getPosition()
+ curDelta.getSource().getLines().size();
-// for (line = intermediateStart; line < nextDelta.getSource()
-// .getPosition(); line++) {
-// // output the code between the last Delta and this one
-// buffer.add(" " + origLines.get(line));
-// origTotal++;
-// revTotal++;
-// }
+ for (line = intermediateStart; line < nextDelta.getSource()
+ .getPosition(); line++) {
+ // output the code between the last Delta and this one
+ buffer.add(" " + origLines.get(line));
+ origTotal++;
+ revTotal++;
+ }
getDeltaText(txt -> buffer.add(txt), nextDelta); // output the Delta
origTotal += nextDelta.getSource().getLines().size();
revTotal += nextDelta.getTarget().getLines().size();
@@ -160,12 +161,12 @@ private static void processDeltas(Consumer writer,
// Now output the post-Delta context code, clamping the end of the file
contextStart = curDelta.getSource().getPosition()
+ curDelta.getSource().getLines().size();
-// for (line = contextStart; (line < (contextStart + contextSize))
-// && (line < origLines.size()); line++) {
-// buffer.add(" " + origLines.get(line));
-// origTotal++;
-// revTotal++;
-// }
+ for (line = contextStart; (line < (contextStart + contextSize))
+ && (line < origLines.size()); line++) {
+ buffer.add(" " + origLines.get(line));
+ origTotal++;
+ revTotal++;
+ }
// Create and insert the block header, conforming to the Unified Diff
// standard
diff --git a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
index 49497e33..3fb2082a 100644
--- a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
+++ b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
@@ -16,7 +16,7 @@
public class GenerateUnifiedDiffTest {
- private static List fileToLines(String filename) throws FileNotFoundException, IOException {
+ public static List fileToLines(String filename) throws FileNotFoundException, IOException {
List lines = new ArrayList<>();
String line = "";
try (BufferedReader in = new BufferedReader(new FileReader(filename))) {
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java
new file mode 100644
index 00000000..f873795d
--- /dev/null
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java
@@ -0,0 +1,134 @@
+package com.github.difflib.unifieddiff;
+
+import com.github.difflib.DiffUtils;
+import com.github.difflib.algorithm.DiffException;
+import com.github.difflib.patch.Patch;
+import java.io.BufferedReader;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.junit.Test;
+
+public class UnifiedDiffRoundTripTest {
+
+ public static List fileToLines(String filename) throws FileNotFoundException, IOException {
+ List lines = new ArrayList<>();
+ String line = "";
+ try (BufferedReader in = new BufferedReader(new FileReader(filename))) {
+ while ((line = in.readLine()) != null) {
+ lines.add(line);
+ }
+ }
+ return lines;
+ }
+
+// @Test
+// public void testGenerateUnified() throws DiffException, IOException {
+// List origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt");
+// List revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt");
+//
+// verify(origLines, revLines, "original.txt", "revised.txt");
+// }
+//
+// @Test
+// public void testGenerateUnifiedWithOneDelta() throws DiffException, IOException {
+// List origLines = fileToLines(TestConstants.MOCK_FOLDER + "one_delta_test_original.txt");
+// List revLines = fileToLines(TestConstants.MOCK_FOLDER + "one_delta_test_revised.txt");
+//
+// verify(origLines, revLines, "one_delta_test_original.txt", "one_delta_test_revised.txt");
+// }
+ @Test
+ public void testGenerateUnifiedDiffWithoutAnyDeltas() throws DiffException, IOException {
+ List test = Arrays.asList("abc");
+ Patch patch = DiffUtils.diff(test, test);
+ StringWriter writer = new StringWriter();
+
+ UnifiedDiffWriter.write(
+ UnifiedDiff.from("header", "tail", UnifiedDiffFile.from("abc", "abc", patch)),
+ name -> test,
+ writer, 0);
+
+ System.out.println(writer);
+ }
+
+// @Test
+// public void testDiff_Issue10() throws IOException {
+// final List baseLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_base.txt");
+// final List patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");
+// final Patch p = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
+// try {
+// DiffUtils.patch(baseLines, p);
+// } catch (PatchFailedException e) {
+// fail(e.getMessage());
+// }
+// }
+//
+// /**
+// * Issue 12
+// */
+// @Test
+// public void testPatchWithNoDeltas() throws DiffException, IOException {
+// final List lines1 = fileToLines(TestConstants.MOCK_FOLDER + "issue11_1.txt");
+// final List lines2 = fileToLines(TestConstants.MOCK_FOLDER + "issue11_2.txt");
+// verify(lines1, lines2, "issue11_1.txt", "issue11_2.txt");
+// }
+//
+// @Test
+// public void testDiff5() throws DiffException, IOException {
+// final List lines1 = fileToLines(TestConstants.MOCK_FOLDER + "5A.txt");
+// final List lines2 = fileToLines(TestConstants.MOCK_FOLDER + "5B.txt");
+// verify(lines1, lines2, "5A.txt", "5B.txt");
+// }
+//
+// /**
+// * Issue 19
+// */
+// @Test
+// public void testDiffWithHeaderLineInText() throws DiffException {
+// List original = new ArrayList<>();
+// List revised = new ArrayList<>();
+//
+// original.add("test line1");
+// original.add("test line2");
+// original.add("test line 4");
+// original.add("test line 5");
+//
+// revised.add("test line1");
+// revised.add("test line2");
+// revised.add("@@ -2,6 +2,7 @@");
+// revised.add("test line 4");
+// revised.add("test line 5");
+//
+// Patch patch = DiffUtils.diff(original, revised);
+// List udiff = UnifiedDiffUtils.generateUnifiedDiff("original", "revised",
+// original, patch, 10);
+// UnifiedDiffUtils.parseUnifiedDiff(udiff);
+// }
+//
+// private void verify(List origLines, List revLines,
+// String originalFile, String revisedFile) throws DiffException {
+// Patch patch = DiffUtils.diff(origLines, revLines);
+// List unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile,
+// origLines, patch, 10);
+//
+// Patch fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
+// List patchedLines;
+// try {
+// patchedLines = fromUnifiedPatch.applyTo(origLines);
+// assertEquals(revLines.size(), patchedLines.size());
+// for (int i = 0; i < revLines.size(); i++) {
+// String l1 = revLines.get(i);
+// String l2 = patchedLines.get(i);
+// if (!l1.equals(l2)) {
+// fail("Line " + (i + 1) + " of the patched file did not match the revised original");
+// }
+// }
+// } catch (PatchFailedException e) {
+// fail(e.getMessage());
+// }
+// }
+}
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
index 782b35d7..1972ab39 100644
--- a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java
@@ -40,8 +40,8 @@ public void testWrite() throws URISyntaxException, IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(str.getBytes()));
StringWriter writer = new StringWriter();
- UnifiedDiffWriter.write(diff, writer);
- System.out.println(writer.toString());
+// UnifiedDiffWriter.write(diff, writer);
+// System.out.println(writer.toString());
}
static String readFile(URI path, Charset encoding)
From 7d620c825c57115541046252f8330524398791f1 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Wed, 5 Jun 2019 00:10:02 +0200
Subject: [PATCH 039/266]
---
.../unifieddiff/UnifiedDiffWriter.java | 56 ++++++++++---------
1 file changed, 29 insertions(+), 27 deletions(-)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
index 5ed92f66..8af5d9e8 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
@@ -63,36 +63,38 @@ public static void write(UnifiedDiff diff, Function> origin
List> patchDeltas = new ArrayList<>(
file.getPatch().getDeltas());
- List> deltas = new ArrayList<>();
-
- AbstractDelta delta = patchDeltas.get(0);
- deltas.add(delta); // add the first Delta to the current set
- // if there's more than 1 Delta, we may need to output them together
- if (patchDeltas.size() > 1) {
- for (int i = 1; i < patchDeltas.size(); i++) {
- int position = delta.getSource().getPosition();
-
- // Check if the next Delta is too close to the current
- // position.
- // And if it is, add it to the current set
- AbstractDelta nextDelta = patchDeltas.get(i);
- if ((position + delta.getSource().size() + contextSize) >= (nextDelta
- .getSource().getPosition() - contextSize)) {
- deltas.add(nextDelta);
- } else {
- // if it isn't, output the current set,
- // then create a new set and add the current Delta to
- // it.
- processDeltas(writer, originalLines, deltas, contextSize);
- deltas.clear();
- deltas.add(nextDelta);
+ if (!patchDeltas.isEmpty()) {
+ List> deltas = new ArrayList<>();
+
+ AbstractDelta delta = patchDeltas.get(0);
+ deltas.add(delta); // add the first Delta to the current set
+ // if there's more than 1 Delta, we may need to output them together
+ if (patchDeltas.size() > 1) {
+ for (int i = 1; i < patchDeltas.size(); i++) {
+ int position = delta.getSource().getPosition();
+
+ // Check if the next Delta is too close to the current
+ // position.
+ // And if it is, add it to the current set
+ AbstractDelta nextDelta = patchDeltas.get(i);
+ if ((position + delta.getSource().size() + contextSize) >= (nextDelta
+ .getSource().getPosition() - contextSize)) {
+ deltas.add(nextDelta);
+ } else {
+ // if it isn't, output the current set,
+ // then create a new set and add the current Delta to
+ // it.
+ processDeltas(writer, originalLines, deltas, contextSize);
+ deltas.clear();
+ deltas.add(nextDelta);
+ }
+ delta = nextDelta;
}
- delta = nextDelta;
- }
+ }
+ // don't forget to process the last set of Deltas
+ processDeltas(writer, originalLines, deltas, contextSize);
}
- // don't forget to process the last set of Deltas
- processDeltas(writer, originalLines, deltas, contextSize);
}
if (diff.getTail() != null) {
From d30d9ca23a17f60cdc703b90c405f1887b11e4cd Mon Sep 17 00:00:00 2001
From: wumpz
Date: Wed, 5 Jun 2019 23:28:13 +0200
Subject: [PATCH 040/266]
---
.../unifieddiff/UnifiedDiffWriter.java | 18 ++---
.../difflib/GenerateUnifiedDiffTest.java | 3 +
.../unifieddiff/UnifiedDiffRoundTripTest.java | 72 +++++++++++--------
3 files changed, 55 insertions(+), 38 deletions(-)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
index 8af5d9e8..c61f83bc 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
@@ -114,28 +114,28 @@ private static void processDeltas(Consumer writer,
AbstractDelta curDelta = deltas.get(0);
// NOTE: +1 to overcome the 0-offset Position
- int origStart = curDelta.getSource().getPosition() + 1;
+ int origStart = curDelta.getSource().getPosition() + 1 - contextSize;
if (origStart < 1) {
origStart = 1;
}
- int revStart = curDelta.getTarget().getPosition() + 1;
+ int revStart = curDelta.getTarget().getPosition() + 1 - contextSize;
if (revStart < 1) {
revStart = 1;
}
// find the start of the wrapper context code
- int contextStart = curDelta.getSource().getPosition();
+ int contextStart = curDelta.getSource().getPosition() - contextSize;
if (contextStart < 0) {
contextStart = 0; // clamp to the start of the file
}
-// // output the context before the first Delta
-// for (line = contextStart; line < curDelta.getSource().getPosition(); line++) { //
-// buffer.add(" " + curDelta.getSource().getLines().get(line - contextStart));
-// origTotal++;
-// revTotal++;
-// }
+ // output the context before the first Delta
+ for (line = contextStart; line < curDelta.getSource().getPosition(); line++) { //
+ buffer.add(" " + origLines.get(line));
+ origTotal++;
+ revTotal++;
+ }
// output the first Delta
getDeltaText(txt -> buffer.add(txt), curDelta);
origTotal += curDelta.getSource().getLines().size();
diff --git a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
index 3fb2082a..c6bfc4ec 100644
--- a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
+++ b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java
@@ -10,6 +10,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import static java.util.stream.Collectors.joining;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
@@ -110,6 +111,8 @@ private void verify(List origLines, List revLines,
List unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile,
origLines, patch, 10);
+ System.out.println(unifiedDiff.stream().collect(joining("\n")));
+
Patch fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
List patchedLines;
try {
diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java
index f873795d..0a982033 100644
--- a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java
+++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java
@@ -1,9 +1,12 @@
package com.github.difflib.unifieddiff;
import com.github.difflib.DiffUtils;
+import com.github.difflib.TestConstants;
import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.Patch;
+import com.github.difflib.patch.PatchFailedException;
import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
@@ -11,6 +14,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
import org.junit.Test;
public class UnifiedDiffRoundTripTest {
@@ -26,13 +31,13 @@ public static List fileToLines(String filename) throws FileNotFoundExcep
return lines;
}
-// @Test
-// public void testGenerateUnified() throws DiffException, IOException {
-// List origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt");
-// List revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt");
-//
-// verify(origLines, revLines, "original.txt", "revised.txt");
-// }
+ @Test
+ public void testGenerateUnified() throws DiffException, IOException {
+ List origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt");
+ List revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt");
+
+ verify(origLines, revLines, "original.txt", "revised.txt");
+ }
//
// @Test
// public void testGenerateUnifiedWithOneDelta() throws DiffException, IOException {
@@ -41,6 +46,7 @@ public static List fileToLines(String filename) throws FileNotFoundExcep
//
// verify(origLines, revLines, "one_delta_test_original.txt", "one_delta_test_revised.txt");
// }
+
@Test
public void testGenerateUnifiedDiffWithoutAnyDeltas() throws DiffException, IOException {
List test = Arrays.asList("abc");
@@ -109,26 +115,34 @@ public void testGenerateUnifiedDiffWithoutAnyDeltas() throws DiffException, IOEx
// UnifiedDiffUtils.parseUnifiedDiff(udiff);
// }
//
-// private void verify(List origLines, List revLines,
-// String originalFile, String revisedFile) throws DiffException {
-// Patch patch = DiffUtils.diff(origLines, revLines);
-// List unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile,
-// origLines, patch, 10);
-//
-// Patch fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
-// List patchedLines;
-// try {
-// patchedLines = fromUnifiedPatch.applyTo(origLines);
-// assertEquals(revLines.size(), patchedLines.size());
-// for (int i = 0; i < revLines.size(); i++) {
-// String l1 = revLines.get(i);
-// String l2 = patchedLines.get(i);
-// if (!l1.equals(l2)) {
-// fail("Line " + (i + 1) + " of the patched file did not match the revised original");
-// }
-// }
-// } catch (PatchFailedException e) {
-// fail(e.getMessage());
-// }
-// }
+ private void verify(List origLines, List revLines,
+ String originalFile, String revisedFile) throws DiffException, IOException {
+ Patch patch = DiffUtils.diff(origLines, revLines);
+
+ StringWriter writer = new StringWriter();
+ UnifiedDiffWriter.write(
+ UnifiedDiff.from("header", "tail", UnifiedDiffFile.from(originalFile, revisedFile, patch)),
+ name -> origLines,
+ writer, 10);
+
+ System.out.println(writer.toString());
+
+ UnifiedDiff unifiedDiff = UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(writer.toString().getBytes()));
+
+ Patch fromUnifiedPatch = unifiedDiff.getFiles().get(0).getPatch();
+ List patchedLines;
+ try {
+ patchedLines = fromUnifiedPatch.applyTo(origLines);
+ assertEquals(revLines.size(), patchedLines.size());
+ for (int i = 0; i < revLines.size(); i++) {
+ String l1 = revLines.get(i);
+ String l2 = patchedLines.get(i);
+ if (!l1.equals(l2)) {
+ fail("Line " + (i + 1) + " of the patched file did not match the revised original");
+ }
+ }
+ } catch (PatchFailedException e) {
+ fail(e.getMessage());
+ }
+ }
}
From a018e9c432056e4ee1e79474aa7af6591ae2f9d7 Mon Sep 17 00:00:00 2001
From: wumpz
Date: Thu, 6 Jun 2019 00:01:22 +0200
Subject: [PATCH 041/266]
---
.../unifieddiff/UnifiedDiffWriter.java | 27 +++++++++----------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
index c61f83bc..c9488f9c 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java
@@ -47,23 +47,22 @@ public static void write(UnifiedDiff diff, Function> origin
writer.accept(diff.getHeader());
for (UnifiedDiffFile file : diff.getFiles()) {
- writeOrNothing(writer, file.getDiffCommand());
- if (file.getIndex() != null) {
- writer.accept("index " + file.getIndex());
- }
- if (file.getFromFile() != null) {
- writer.accept("--- " + file.getFromFile());
- }
- if (file.getToFile() != null) {
- writer.accept("+++ " + file.getToFile());
- }
-
- List originalLines = originalLinesProvider.apply(file.getFromFile());
-
List> patchDeltas = new ArrayList<>(
file.getPatch().getDeltas());
-
if (!patchDeltas.isEmpty()) {
+ writeOrNothing(writer, file.getDiffCommand());
+ if (file.getIndex() != null) {
+ writer.accept("index " + file.getIndex());
+ }
+ if (file.getFromFile() != null) {
+ writer.accept("--- " + file.getFromFile());
+ }
+ if (file.getToFile() != null) {
+ writer.accept("+++ " + file.getToFile());
+ }
+
+ List originalLines = originalLinesProvider.apply(file.getFromFile());
+
List> deltas = new ArrayList<>();
AbstractDelta delta = patchDeltas.get(0);
From 630eed340b0abe4714d4944411dd6079bea4823e Mon Sep 17 00:00:00 2001
From: wumpz
Date: Fri, 7 Jun 2019 00:27:54 +0200
Subject: [PATCH 042/266]
---
.../difflib/unifieddiff/UnifiedDiff.java | 13 ++
.../unifieddiff/UnifiedDiffReader.java | 15 +-
.../unifieddiff/UnifiedDiffReaderTest.java | 19 +++
.../unifieddiff/UnifiedDiffRoundTripTest.java | 145 ++++++++++--------
4 files changed, 125 insertions(+), 67 deletions(-)
diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
index e1a52112..bdabc589 100644
--- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
+++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java
@@ -15,9 +15,11 @@
*/
package com.github.difflib.unifieddiff;
+import com.github.difflib.patch.PatchFailedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.function.Predicate;
/**
*
@@ -53,6 +55,17 @@ public String getTail() {
return tail;
}
+ public List spplyPatchTo(Predicate