GeezerVison is showing github.com — Merge origin/master · fgsoap/java-diff-utils@fc7c714
File treeExpand file treeCollapse file tree Open diff view settings main/java/com/github/difflib
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | build/ | 3 | 3 | nbproject/ | 4 | 4 | target/ | | 5 | + | | 6 | +*.iml |
Collapse file Copy file name to clipboardExpand all lines: CHANGELOG.md +4Lines changed: 4 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,10 @@ This project uses a custom versioning scheme (and not [Semantic Versioning](http | 7 | 7 |
| 8 | 8 | ## [Unreleased] | 9 | 9 |
| | 10 | +### Changed | | 11 | + | | 12 | +* UnifiedDiffReader improved for **new file mode** and better timestamp recognition | | 13 | + | 10 | 14 | ## [4.7] | 11 | 15 |
| 12 | 16 | ### Changed | |
Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java +14-2Lines changed: 14 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -97,19 +97,31 @@ private static void processLinesInPrevChunk(List<String[]> rawChunk, Patch<Strin | 97 | 97 | List<String> oldChunkLines = new ArrayList<>(); | 98 | 98 | List<String> newChunkLines = new ArrayList<>(); | 99 | 99 |
| | 100 | + List<Integer> removePosition = new ArrayList<>(); | | 101 | + List<Integer> addPosition = new ArrayList<>(); | | 102 | + int removeNum = 0; | | 103 | + int addNum = 0; | 100 | 104 | for (String[] raw_line : rawChunk) { | 101 | 105 | tag = raw_line[0]; | 102 | 106 | rest = raw_line[1]; | 103 | 107 | if (" ".equals(tag) || "-".equals(tag)) { | | 108 | + removeNum++; | 104 | 109 | oldChunkLines.add(rest); | | 110 | + if ("-".equals(tag)) { | | 111 | + removePosition.add(old_ln - 1 + removeNum); | | 112 | + } | 105 | 113 | } | 106 | 114 | if (" ".equals(tag) || "+".equals(tag)) { | | 115 | + addNum++; | 107 | 116 | newChunkLines.add(rest); | | 117 | + if ("+".equals(tag)) { | | 118 | + addPosition.add(new_ln - 1 + addNum); | | 119 | + } | 108 | 120 | } | 109 | 121 | } | 110 | 122 | patch.addDelta(new ChangeDelta<>(new Chunk<>( | 111 | | - old_ln - 1, oldChunkLines), new Chunk<>( | 112 | | - new_ln - 1, newChunkLines))); | | 123 | + old_ln - 1, oldChunkLines, removePosition), new Chunk<>( | | 124 | + new_ln - 1, newChunkLines, addPosition))); | 113 | 125 | rawChunk.clear(); | 114 | 126 | } | 115 | 127 | } | |
Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/main/java/com/github/difflib/patch/Chunk.java +34-2Lines changed: 34 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,16 +37,19 @@ public final class Chunk<T> { | 37 | 37 |
| 38 | 38 | private final int position; | 39 | 39 | private List<T> lines; | | 40 | + private final List<Integer> changePosition; | 40 | 41 |
| 41 | 42 | /** | 42 | 43 | * Creates a chunk and saves a copy of affected lines | 43 | 44 | * | 44 | 45 | * @param position the start position | 45 | 46 | * @param lines the affected lines | | 47 | + * @param changePosition the positions of changed lines | 46 | 48 | */ | 47 | | - public Chunk(int position, List<T> lines) { | | 49 | + public Chunk(int position, List<T> lines, List<Integer> changePosition) { | 48 | 50 | this.position = position; | 49 | 51 | this.lines = new ArrayList<>(lines); | | 52 | + this.changePosition = changePosition; | 50 | 53 | } | 51 | 54 |
| 52 | 55 | /** | @@ -55,9 +58,31 @@ public Chunk(int position, List<T> lines) { | 55 | 58 | * @param position the start position | 56 | 59 | * @param lines the affected lines | 57 | 60 | */ | 58 | | - public Chunk(int position, T[] lines) { | | 61 | + public Chunk(int position, List<T> lines) { | | 62 | + this(position, lines, null); | | 63 | + } | | 64 | + | | 65 | + /** | | 66 | + * Creates a chunk and saves a copy of affected lines | | 67 | + * | | 68 | + * @param position the start position | | 69 | + * @param lines the affected lines | | 70 | + * @param changePosition the positions of changed lines | | 71 | + */ | | 72 | + public Chunk(int position, T[] lines, List<Integer> changePosition) { | 59 | 73 | this.position = position; | 60 | 74 | this.lines = Arrays.asList(lines); | | 75 | + this.changePosition = changePosition; | | 76 | + } | | 77 | + | | 78 | + /** | | 79 | + * Creates a chunk and saves a copy of affected lines | | 80 | + * | | 81 | + * @param position the start position | | 82 | + * @param lines the affected lines | | 83 | + */ | | 84 | + public Chunk(int position, T[] lines) { | | 85 | + this(position, lines, null); | 61 | 86 | } | 62 | 87 |
| 63 | 88 | /** | @@ -96,6 +121,13 @@ public List<T> getLines() { | 96 | 121 | return lines; | 97 | 122 | } | 98 | 123 |
| | 124 | + /** | | 125 | + * @return the positions of changed lines of chunk in the text | | 126 | + */ | | 127 | + public List<Integer> getChangePosition() { | | 128 | + return changePosition; | | 129 | + } | | 130 | + | 99 | 131 | public int size() { | 100 | 132 | return lines.size(); | 101 | 133 | } | |
Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package com.github.difflib; | 2 | 2 |
| | 3 | +import com.github.difflib.patch.Chunk; | 3 | 4 | import com.github.difflib.patch.Patch; | 4 | 5 | import com.github.difflib.patch.PatchFailedException; | 5 | 6 | import java.io.BufferedReader; | | 8 | 9 | import java.io.IOException; | 9 | 10 | import java.util.ArrayList; | 10 | 11 | import java.util.Arrays; | | 12 | +import java.util.Collections; | 11 | 13 | import java.util.List; | 12 | 14 | import static java.util.stream.Collectors.joining; | 13 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; | | 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; | 14 | 17 | import static org.junit.jupiter.api.Assertions.fail; | 15 | 18 | import org.junit.jupiter.api.Test; | 16 | 19 |
| @@ -126,6 +129,44 @@ public void testNewFileCreation() { | 126 | 129 | UnifiedDiffUtils.parseUnifiedDiff(udiff); | 127 | 130 | } | 128 | 131 |
| | 132 | + /** | | 133 | + * Issue 89 | | 134 | + */ | | 135 | + @Test | | 136 | + public void testChagngePosition() throws IOException { | | 137 | + final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue89_patch.txt"); | | 138 | + final Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patchLines); | | 139 | + List<Integer> realRemoveListOne = Collections.singletonList(3); | | 140 | + List<Integer> realAddListOne = Arrays.asList(3, 7, 8, 9, 10, 11, 12, 13, 14); | | 141 | + validateChangePosition(patch, 0, realRemoveListOne, realAddListOne); | | 142 | + List<Integer> realRemoveListTwo = new ArrayList<>(); | | 143 | + List<Integer> realAddListTwo = Arrays.asList(27, 28); | | 144 | + validateChangePosition(patch, 1, realRemoveListTwo, realAddListTwo); | | 145 | + | | 146 | + } | | 147 | + | | 148 | + private void validateChangePosition(Patch<String> patch, int index, List<Integer> realRemoveList, | | 149 | + List<Integer> realAddList ) { | | 150 | + final Chunk originChunk = patch.getDeltas().get(index).getSource(); | | 151 | + List<Integer> removeList = originChunk.getChangePosition(); | | 152 | + assertEquals(realRemoveList.size(), removeList.size()); | | 153 | + for (Integer ele: realRemoveList) { | | 154 | + assertTrue(realRemoveList.contains(ele)); | | 155 | + } | | 156 | + for (Integer ele: removeList) { | | 157 | + assertTrue(realAddList.contains(ele)); | | 158 | + } | | 159 | + final Chunk targetChunk = patch.getDeltas().get(index).getTarget(); | | 160 | + List<Integer> addList = targetChunk.getChangePosition(); | | 161 | + assertEquals(realAddList.size(), addList.size()); | | 162 | + for (Integer ele: realAddList) { | | 163 | + assertTrue(addList.contains(ele)); | | 164 | + } | | 165 | + for (Integer ele: addList) { | | 166 | + assertTrue(realAddList.contains(ele)); | | 167 | + } | | 168 | + } | | 169 | + | 129 | 170 | private void verify(List<String> origLines, List<String> revLines, | 130 | 171 | String originalFile, String revisedFile) { | 131 | 172 | Patch<String> patch = DiffUtils.diff(origLines, revLines); | |
Collapse file Copy file name to clipboard +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- Origin.java 2020-06-11 11:06:21.000000000 +0800 | | 2 | ++++ Update.java 2020-06-11 10:59:48.000000000 +0800 | | 3 | +@@ -1,9 +1,17 @@ | | 4 | + package checkstyle_demo.PatchSuppression.MultiChangeInOneFile; | | 5 | + | | 6 | +-public class Origin { | | 7 | ++public class Update { | | 8 | + public void test1() { | | 9 | + | | 10 | + } | | 11 | ++ | | 12 | ++ public void test2() { | | 13 | ++ | | 14 | ++ } | | 15 | ++ | | 16 | ++ public void test3() { | | 17 | ++ | | 18 | ++ } | | 19 | + } | | 20 | + | | 21 | + class BasicTest { | | 22 | +@@ -16,5 +24,7 @@ | | 23 | + class Test2 { | | 24 | + public void test1() { | | 25 | + System.out.println(); | | 26 | ++ System.out.println(); | | 27 | ++ System.out.println(); | | 28 | + } | | 29 | + } |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package checkstyle_demo.PatchSuppression.MultiChangeInOneFile; | | 2 | + | | 3 | +public class issue89_revised { | | 4 | + public void test1() { | | 5 | + | | 6 | + } | | 7 | + | | 8 | + public void test2() { | | 9 | + | | 10 | + } | | 11 | + | | 12 | + public void test3() { | | 13 | + | | 14 | + } | | 15 | +} | | 16 | + | | 17 | +class Test { | | 18 | + private int i; | | 19 | + void foo() { | | 20 | + i++; | | 21 | + } | | 22 | +} | | 23 | + | | 24 | +class Test2 { | | 25 | + public void test1() { | | 26 | + System.out.println(); | | 27 | + System.out.println(); | | 28 | + System.out.println(); | | 29 | + } | | 30 | +} |
|