included new file diff capability to new unified diff parser

This commit is contained in:
Tobias Warneke
2020-01-04 01:18:22 +01:00
parent 39efdd5143
commit 3b23656260
3 changed files with 54 additions and 11 deletions

View File

@@ -44,7 +44,9 @@ public class UnifiedDiffWriter {
}
public static void write(UnifiedDiff diff, Function<String, List<String>> originalLinesProvider, Consumer<String> writer, int contextSize) throws IOException {
writer.accept(diff.getHeader());
if (diff.getHeader() != null) {
writer.accept(diff.getHeader());
}
for (UnifiedDiffFile file : diff.getFiles()) {
List<AbstractDelta<String>> patchDeltas = new ArrayList<>(
@@ -54,9 +56,9 @@ public class UnifiedDiffWriter {
if (file.getIndex() != null) {
writer.accept("index " + file.getIndex());
}
if (file.getFromFile() != null) {
writer.accept("--- " + file.getFromFile());
}
writer.accept("--- " + file.getFromFile());
if (file.getToFile() != null) {
writer.accept("+++ " + file.getToFile());
}
@@ -83,7 +85,7 @@ public class UnifiedDiffWriter {
// 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, originalLines, deltas, contextSize, false);
deltas.clear();
deltas.add(nextDelta);
}
@@ -92,7 +94,8 @@ public class UnifiedDiffWriter {
}
// don't forget to process the last set of Deltas
processDeltas(writer, originalLines, deltas, contextSize);
processDeltas(writer, originalLines, deltas, contextSize,
patchDeltas.size() == 1 && file.getFromFile() == null);
}
}
@@ -104,7 +107,7 @@ public class UnifiedDiffWriter {
private static void processDeltas(Consumer<String> writer,
List<String> origLines, List<AbstractDelta<String>> deltas,
int contextSize) {
int contextSize, boolean newFile) {
List<String> buffer = new ArrayList<>();
int origTotal = 0; // counter for total lines output from Original
int revTotal = 0; // counter for total lines output from Original
@@ -112,10 +115,15 @@ public class UnifiedDiffWriter {
AbstractDelta<String> 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 origStart;
if (newFile) {
origStart = 0;
} else {
// NOTE: +1 to overcome the 0-offset Position
origStart = curDelta.getSource().getPosition() + 1 - contextSize;
if (origStart < 1) {
origStart = 1;
}
}
int revStart = curDelta.getTarget().getPosition() + 1 - contextSize;

View File

@@ -120,6 +120,8 @@ public class GenerateUnifiedDiffTest {
List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff(null, "revised",
original, patch, 10);
assertEquals("--- null", udiff.get(0));
assertEquals("+++ revised", udiff.get(1));
assertEquals("@@ -0,0 +1,2 @@", udiff.get(2));
UnifiedDiffUtils.parseUnifiedDiff(udiff);

View File

@@ -15,6 +15,9 @@
*/
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.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
@@ -23,6 +26,9 @@ import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
@@ -43,6 +49,33 @@ public class UnifiedDiffWriterTest {
// UnifiedDiffWriter.write(diff, writer);
// System.out.println(writer.toString());
}
/**
* Issue 47
*/
@Test
public void testWriteWithNewFile() throws URISyntaxException, IOException, DiffException {
List<String> original = new ArrayList<>();
List<String> revised = new ArrayList<>();
revised.add("line1");
revised.add("line2");
Patch<String> patch = DiffUtils.diff(original, revised);
UnifiedDiff diff = new UnifiedDiff();
diff.addFile( UnifiedDiffFile.from(null, "revised", patch) );
StringWriter writer = new StringWriter();
UnifiedDiffWriter.write(diff, f -> original, writer, 5);
System.out.println(writer.toString());
String[] lines = writer.toString().split("\\n");
assertEquals("--- null", lines[0]);
assertEquals("+++ revised", lines[1]);
assertEquals("@@ -0,0 +1,2 @@", lines[2]);
}
static String readFile(URI path, Charset encoding)
throws IOException {