mirror of
https://github.com/java-diff-utils/java-diff-utils.git
synced 2026-03-13 10:11:17 +08:00
included new file diff capability to new unified diff parser
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user