GeezerVison is showing github.com — included new file diff capability to new unified diff parser · fgsoap/java-diff-utils@3b23656
File treeExpand file treeCollapse file tree Open diff view settings main/java/com/github/difflib/unifieddiff test/java/com/github/difflib
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java +19-11Lines changed: 19 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,7 +44,9 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin | 44 | 44 | } | 45 | 45 |
| 46 | 46 | 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 | + } | 48 | 50 |
| 49 | 51 | for (UnifiedDiffFile file : diff.getFiles()) { | 50 | 52 | List<AbstractDelta<String>> patchDeltas = new ArrayList<>( | @@ -54,9 +56,9 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin | 54 | 56 | if (file.getIndex() != null) { | 55 | 57 | writer.accept("index " + file.getIndex()); | 56 | 58 | } | 57 | | - if (file.getFromFile() != null) { | 58 | | - writer.accept("--- " + file.getFromFile()); | 59 | | - } | | 59 | + | | 60 | + writer.accept("--- " + file.getFromFile()); | | 61 | + | 60 | 62 | if (file.getToFile() != null) { | 61 | 63 | writer.accept("+++ " + file.getToFile()); | 62 | 64 | } | @@ -83,7 +85,7 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin | 83 | 85 | // if it isn't, output the current set, | 84 | 86 | // then create a new set and add the current Delta to | 85 | 87 | // it. | 86 | | - processDeltas(writer, originalLines, deltas, contextSize); | | 88 | + processDeltas(writer, originalLines, deltas, contextSize, false); | 87 | 89 | deltas.clear(); | 88 | 90 | deltas.add(nextDelta); | 89 | 91 | } | @@ -92,7 +94,8 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin | 92 | 94 |
| 93 | 95 | } | 94 | 96 | // 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); | 96 | 99 | } | 97 | 100 |
| 98 | 101 | } | @@ -104,18 +107,23 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin | 104 | 107 |
| 105 | 108 | private static void processDeltas(Consumer<String> writer, | 106 | 109 | List<String> origLines, List<AbstractDelta<String>> deltas, | 107 | | - int contextSize) { | | 110 | + int contextSize, boolean newFile) { | 108 | 111 | List<String> buffer = new ArrayList<>(); | 109 | 112 | int origTotal = 0; // counter for total lines output from Original | 110 | 113 | int revTotal = 0; // counter for total lines output from Original | 111 | 114 | int line; | 112 | 115 |
| 113 | 116 | AbstractDelta<String> curDelta = deltas.get(0); | 114 | 117 |
| 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 | + } | 119 | 127 | } | 120 | 128 |
| 121 | 129 | int revStart = curDelta.getTarget().getPosition() + 1 - contextSize; | |
Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -120,6 +120,8 @@ public void testNewFileCreation() throws DiffException { | 120 | 120 | List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff(null, "revised", | 121 | 121 | original, patch, 10); | 122 | 122 |
| | 123 | + assertEquals("--- null", udiff.get(0)); | | 124 | + assertEquals("+++ revised", udiff.get(1)); | 123 | 125 | assertEquals("@@ -0,0 +1,2 @@", udiff.get(2)); | 124 | 126 |
| 125 | 127 | UnifiedDiffUtils.parseUnifiedDiff(udiff); | |
Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | */ | 16 | 16 | package com.github.difflib.unifieddiff; | 17 | 17 |
| | 18 | +import com.github.difflib.DiffUtils; | | 19 | +import com.github.difflib.algorithm.DiffException; | | 20 | +import com.github.difflib.patch.Patch; | 18 | 21 | import java.io.ByteArrayInputStream; | 19 | 22 | import java.io.IOException; | 20 | 23 | import java.io.StringWriter; | | 23 | 26 | import java.nio.charset.Charset; | 24 | 27 | import java.nio.file.Files; | 25 | 28 | import java.nio.file.Paths; | | 29 | +import java.util.ArrayList; | | 30 | +import java.util.List; | | 31 | +import static org.junit.Assert.assertEquals; | 26 | 32 | import org.junit.Test; | 27 | 33 |
| 28 | 34 | /** | @@ -43,6 +49,33 @@ public void testWrite() throws URISyntaxException, IOException { | 43 | 49 | // UnifiedDiffWriter.write(diff, writer); | 44 | 50 | // System.out.println(writer.toString()); | 45 | 51 | } | | 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 | + } | 46 | 79 |
| 47 | 80 | static String readFile(URI path, Charset encoding) | 48 | 81 | throws IOException { | |
|