Skip to content

Commit 630eed3

Browse files
committed
1 parent a018e9c commit 630eed3

File tree

4 files changed

+125
-67
lines changed

4 files changed

+125
-67
lines changed

src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java

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

18+
import com.github.difflib.patch.PatchFailedException;
1819
import java.util.ArrayList;
1920
import java.util.Collections;
2021
import java.util.List;
22+
import java.util.function.Predicate;
2123

2224
/**
2325
*
@@ -53,6 +55,17 @@ public String getTail() {
5355
return tail;
5456
}
5557

58+
public List<String> spplyPatchTo(Predicate<String> findFile, List<String> originalLines) throws PatchFailedException {
59+
UnifiedDiffFile file = files.stream()
60+
.filter(diff -> findFile.test(diff.getFromFile()))
61+
.findFirst().orElse(null);
62+
if (file != null) {
63+
return file.getPatch().applyTo(originalLines);
64+
} else {
65+
return originalLines;
66+
}
67+
}
68+
5669
public static UnifiedDiff from(String header, String tail, UnifiedDiffFile... files) {
5770
UnifiedDiff diff = new UnifiedDiff();
5871
diff.setHeader(header);

src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
6868
String tailTxt = "";
6969
while (READER.ready()) {
7070
String line = READER.readLine();
71-
if (line.matches("^\\-\\-\\s+")) {
71+
if (line.equals("--")) {
7272
break;
7373
} else {
7474
LOG.log(Level.INFO, "parsing line {0}", line);
@@ -79,8 +79,10 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
7979
break;
8080
}
8181
} else {
82-
header = false;
83-
data.setHeader(headerTxt);
82+
if (header) {
83+
header = false;
84+
data.setHeader(headerTxt);
85+
}
8486
}
8587
}
8688
}
@@ -114,10 +116,12 @@ private boolean processLine(boolean header, String line) throws UnifiedDiffParse
114116
for (UnifiedDiffLine rule : MAIN_PARSER_RULES) {
115117
if (header && rule.isStopsHeaderParsing() || !header) {
116118
if (rule.processLine(line)) {
119+
LOG.info(" >>> processed rule " + rule.toString());
117120
return true;
118121
}
119122
}
120123
}
124+
LOG.info(" >>> no rule matched " + line);
121125
return false;
122126
}
123127

@@ -241,6 +245,11 @@ public boolean processLine(String line) throws UnifiedDiffParserException {
241245
public boolean isStopsHeaderParsing() {
242246
return stopsHeaderParsing;
243247
}
248+
249+
@Override
250+
public String toString() {
251+
return "UnifiedDiffLine{" + "pattern=" + pattern + ", stopsHeaderParsing=" + stopsHeaderParsing + '}';
252+
}
244253
}
245254
}
246255

src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ public void testChunkHeaderParsing2() {
7272
assertEquals("189", matcher.group(3));
7373
}
7474

75+
@Test
76+
public void testChunkHeaderParsing3() {
77+
//"^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@.*$"
78+
Pattern pattern = UnifiedDiffReader.UNIFIED_DIFF_CHUNK_REGEXP;
79+
Matcher matcher = pattern.matcher("@@ -1,27 +1,27 @@");
80+
81+
assertTrue(matcher.find());
82+
assertEquals("1", matcher.group(1));
83+
assertEquals("1", matcher.group(3));
84+
}
85+
7586
@Test
7687
public void testSimpleParse2() throws IOException {
7788
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(UnifiedDiffReaderTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));
@@ -92,4 +103,12 @@ public void testSimpleParse2() throws IOException {
92103
assertThat(diff.getTail()).isEqualTo("2.17.1.windows.2\n\n");
93104
}
94105

106+
@Test
107+
public void testSimplePattern() {
108+
Pattern pattern = Pattern.compile("^\\+\\+\\+\\s");
109+
110+
Matcher m = pattern.matcher("+++ revised.txt");
111+
assertTrue(m.find());
112+
}
113+
95114
}

src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.ArrayList;
1515
import java.util.Arrays;
1616
import java.util.List;
17+
import static java.util.stream.Collectors.joining;
1718
import static org.junit.Assert.assertEquals;
1819
import static org.junit.Assert.fail;
1920
import org.junit.Test;
@@ -38,14 +39,14 @@ public void testGenerateUnified() throws DiffException, IOException {
3839

3940
verify(origLines, revLines, "original.txt", "revised.txt");
4041
}
41-
//
42-
// @Test
43-
// public void testGenerateUnifiedWithOneDelta() throws DiffException, IOException {
44-
// List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "one_delta_test_original.txt");
45-
// List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "one_delta_test_revised.txt");
46-
//
47-
// verify(origLines, revLines, "one_delta_test_original.txt", "one_delta_test_revised.txt");
48-
// }
42+
43+
@Test
44+
public void testGenerateUnifiedWithOneDelta() throws DiffException, IOException {
45+
List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "one_delta_test_original.txt");
46+
List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "one_delta_test_revised.txt");
47+
48+
verify(origLines, revLines, "one_delta_test_original.txt", "one_delta_test_revised.txt");
49+
}
4950

5051
@Test
5152
public void testGenerateUnifiedDiffWithoutAnyDeltas() throws DiffException, IOException {
@@ -61,60 +62,71 @@ public void testGenerateUnifiedDiffWithoutAnyDeltas() throws DiffException, IOEx
6162
System.out.println(writer);
6263
}
6364

64-
// @Test
65-
// public void testDiff_Issue10() throws IOException {
66-
// final List<String> baseLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_base.txt");
67-
// final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");
68-
// final Patch<String> p = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
69-
// try {
70-
// DiffUtils.patch(baseLines, p);
71-
// } catch (PatchFailedException e) {
72-
// fail(e.getMessage());
73-
// }
74-
// }
75-
//
76-
// /**
77-
// * Issue 12
78-
// */
79-
// @Test
80-
// public void testPatchWithNoDeltas() throws DiffException, IOException {
81-
// final List<String> lines1 = fileToLines(TestConstants.MOCK_FOLDER + "issue11_1.txt");
82-
// final List<String> lines2 = fileToLines(TestConstants.MOCK_FOLDER + "issue11_2.txt");
83-
// verify(lines1, lines2, "issue11_1.txt", "issue11_2.txt");
84-
// }
85-
//
86-
// @Test
87-
// public void testDiff5() throws DiffException, IOException {
88-
// final List<String> lines1 = fileToLines(TestConstants.MOCK_FOLDER + "5A.txt");
89-
// final List<String> lines2 = fileToLines(TestConstants.MOCK_FOLDER + "5B.txt");
90-
// verify(lines1, lines2, "5A.txt", "5B.txt");
91-
// }
92-
//
93-
// /**
94-
// * Issue 19
95-
// */
96-
// @Test
97-
// public void testDiffWithHeaderLineInText() throws DiffException {
98-
// List<String> original = new ArrayList<>();
99-
// List<String> revised = new ArrayList<>();
100-
//
101-
// original.add("test line1");
102-
// original.add("test line2");
103-
// original.add("test line 4");
104-
// original.add("test line 5");
105-
//
106-
// revised.add("test line1");
107-
// revised.add("test line2");
108-
// revised.add("@@ -2,6 +2,7 @@");
109-
// revised.add("test line 4");
110-
// revised.add("test line 5");
111-
//
112-
// Patch<String> patch = DiffUtils.diff(original, revised);
113-
// List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff("original", "revised",
114-
// original, patch, 10);
115-
// UnifiedDiffUtils.parseUnifiedDiff(udiff);
116-
// }
117-
//
65+
@Test
66+
public void testDiff_Issue10() throws IOException {
67+
final List<String> baseLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_base.txt");
68+
final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");
69+
70+
UnifiedDiff unifiedDiff = UnifiedDiffReader.parseUnifiedDiff(
71+
new ByteArrayInputStream(patchLines.stream().collect(joining("\n")).getBytes())
72+
);
73+
74+
final Patch<String> p = unifiedDiff.getFiles().get(0).getPatch();
75+
try {
76+
DiffUtils.patch(baseLines, p);
77+
} catch (PatchFailedException e) {
78+
fail(e.getMessage());
79+
}
80+
}
81+
82+
/**
83+
* Issue 12
84+
*/
85+
@Test
86+
public void testPatchWithNoDeltas() throws DiffException, IOException {
87+
final List<String> lines1 = fileToLines(TestConstants.MOCK_FOLDER + "issue11_1.txt");
88+
final List<String> lines2 = fileToLines(TestConstants.MOCK_FOLDER + "issue11_2.txt");
89+
verify(lines1, lines2, "issue11_1.txt", "issue11_2.txt");
90+
}
91+
92+
@Test
93+
public void testDiff5() throws DiffException, IOException {
94+
final List<String> lines1 = fileToLines(TestConstants.MOCK_FOLDER + "5A.txt");
95+
final List<String> lines2 = fileToLines(TestConstants.MOCK_FOLDER + "5B.txt");
96+
verify(lines1, lines2, "5A.txt", "5B.txt");
97+
}
98+
99+
/**
100+
* Issue 19
101+
*/
102+
@Test
103+
public void testDiffWithHeaderLineInText() throws DiffException, IOException {
104+
List<String> original = new ArrayList<>();
105+
List<String> revised = new ArrayList<>();
106+
107+
original.add("test line1");
108+
original.add("test line2");
109+
original.add("test line 4");
110+
original.add("test line 5");
111+
112+
revised.add("test line1");
113+
revised.add("test line2");
114+
revised.add("@@ -2,6 +2,7 @@");
115+
revised.add("test line 4");
116+
revised.add("test line 5");
117+
118+
Patch<String> patch = DiffUtils.diff(original, revised);
119+
StringWriter writer = new StringWriter();
120+
UnifiedDiffWriter.write(
121+
UnifiedDiff.from("header", "tail", UnifiedDiffFile.from("original", "revised", patch)),
122+
name -> original,
123+
writer, 10);
124+
125+
System.out.println(writer.toString());
126+
127+
UnifiedDiff unifiedDiff = UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(writer.toString().getBytes()));
128+
}
129+
118130
private void verify(List<String> origLines, List<String> revLines,
119131
String originalFile, String revisedFile) throws DiffException, IOException {
120132
Patch<String> patch = DiffUtils.diff(origLines, revLines);
@@ -129,10 +141,15 @@ private void verify(List<String> origLines, List<String> revLines,
129141

130142
UnifiedDiff unifiedDiff = UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(writer.toString().getBytes()));
131143

132-
Patch<String> fromUnifiedPatch = unifiedDiff.getFiles().get(0).getPatch();
133144
List<String> patchedLines;
134145
try {
135-
patchedLines = fromUnifiedPatch.applyTo(origLines);
146+
// if (unifiedDiff.getFiles().isEmpty()) {
147+
// patchedLines = new ArrayList<>(origLines);
148+
// } else {
149+
// Patch<String> fromUnifiedPatch = unifiedDiff.getFiles().get(0).getPatch();
150+
// patchedLines = fromUnifiedPatch.applyTo(origLines);
151+
// }
152+
patchedLines = unifiedDiff.spplyPatchTo(file -> originalFile.equals(file), origLines);
136153
assertEquals(revLines.size(), patchedLines.size());
137154
for (int i = 0; i < revLines.size(); i++) {
138155
String l1 = revLines.get(i);

0 commit comments

Comments
 (0)