Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b23656

Browse files
committed
included new file diff capability to new unified diff parser
1 parent 39efdd5 commit 3b23656

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

java-diff-utils/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
4444
}
4545

4646
public static void write(UnifiedDiff diff, Function<String, List<String>> originalLinesProvider, Consumer<String> writer, int contextSize) throws IOException {
47-
writer.accept(diff.getHeader());
47+
if (diff.getHeader() != null) {
48+
writer.accept(diff.getHeader());
49+
}
4850

4951
for (UnifiedDiffFile file : diff.getFiles()) {
5052
List<AbstractDelta<String>> patchDeltas = new ArrayList<>(
@@ -54,9 +56,9 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
5456
if (file.getIndex() != null) {
5557
writer.accept("index " + file.getIndex());
5658
}
57-
if (file.getFromFile() != null) {
58-
writer.accept("--- " + file.getFromFile());
59-
}
59+
60+
writer.accept("--- " + file.getFromFile());
61+
6062
if (file.getToFile() != null) {
6163
writer.accept("+++ " + file.getToFile());
6264
}
@@ -83,7 +85,7 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
8385
// if it isn't, output the current set,
8486
// then create a new set and add the current Delta to
8587
// it.
86-
processDeltas(writer, originalLines, deltas, contextSize);
88+
processDeltas(writer, originalLines, deltas, contextSize, false);
8789
deltas.clear();
8890
deltas.add(nextDelta);
8991
}
@@ -92,7 +94,8 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
9294

9395
}
9496
// don't forget to process the last set of Deltas
95-
processDeltas(writer, originalLines, deltas, contextSize);
97+
processDeltas(writer, originalLines, deltas, contextSize,
98+
patchDeltas.size() == 1 && file.getFromFile() == null);
9699
}
97100

98101
}
@@ -104,18 +107,23 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
104107

105108
private static void processDeltas(Consumer<String> writer,
106109
List<String> origLines, List<AbstractDelta<String>> deltas,
107-
int contextSize) {
110+
int contextSize, boolean newFile) {
108111
List<String> buffer = new ArrayList<>();
109112
int origTotal = 0; // counter for total lines output from Original
110113
int revTotal = 0; // counter for total lines output from Original
111114
int line;
112115

113116
AbstractDelta<String> curDelta = deltas.get(0);
114117

115-
// NOTE: +1 to overcome the 0-offset Position
116-
int origStart = curDelta.getSource().getPosition() + 1 - contextSize;
117-
if (origStart < 1) {
118-
origStart = 1;
118+
int origStart;
119+
if (newFile) {
120+
origStart = 0;
121+
} else {
122+
// NOTE: +1 to overcome the 0-offset Position
123+
origStart = curDelta.getSource().getPosition() + 1 - contextSize;
124+
if (origStart < 1) {
125+
origStart = 1;
126+
}
119127
}
120128

121129
int revStart = curDelta.getTarget().getPosition() + 1 - contextSize;

java-diff-utils/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public void testNewFileCreation() throws DiffException {
120120
List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff(null, "revised",
121121
original, patch, 10);
122122

123+
assertEquals("--- null", udiff.get(0));
124+
assertEquals("+++ revised", udiff.get(1));
123125
assertEquals("@@ -0,0 +1,2 @@", udiff.get(2));
124126

125127
UnifiedDiffUtils.parseUnifiedDiff(udiff);

java-diff-utils/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.github.difflib.unifieddiff;
1717

18+
import com.github.difflib.DiffUtils;
19+
import com.github.difflib.algorithm.DiffException;
20+
import com.github.difflib.patch.Patch;
1821
import java.io.ByteArrayInputStream;
1922
import java.io.IOException;
2023
import java.io.StringWriter;
@@ -23,6 +26,9 @@
2326
import java.nio.charset.Charset;
2427
import java.nio.file.Files;
2528
import java.nio.file.Paths;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
import static org.junit.Assert.assertEquals;
2632
import org.junit.Test;
2733

2834
/**
@@ -43,6 +49,33 @@ public void testWrite() throws URISyntaxException, IOException {
4349
// UnifiedDiffWriter.write(diff, writer);
4450
// System.out.println(writer.toString());
4551
}
52+
53+
/**
54+
* Issue 47
55+
*/
56+
@Test
57+
public void testWriteWithNewFile() throws URISyntaxException, IOException, DiffException {
58+
59+
List<String> original = new ArrayList<>();
60+
List<String> revised = new ArrayList<>();
61+
62+
revised.add("line1");
63+
revised.add("line2");
64+
65+
Patch<String> patch = DiffUtils.diff(original, revised);
66+
UnifiedDiff diff = new UnifiedDiff();
67+
diff.addFile( UnifiedDiffFile.from(null, "revised", patch) );
68+
69+
StringWriter writer = new StringWriter();
70+
UnifiedDiffWriter.write(diff, f -> original, writer, 5);
71+
System.out.println(writer.toString());
72+
73+
String[] lines = writer.toString().split("\\n");
74+
75+
assertEquals("--- null", lines[0]);
76+
assertEquals("+++ revised", lines[1]);
77+
assertEquals("@@ -0,0 +1,2 @@", lines[2]);
78+
}
4679

4780
static String readFile(URI path, Charset encoding)
4881
throws IOException {

0 commit comments

Comments
 (0)