Skip to content

Commit 55e4867

Browse files
committed
1 parent 253a94b commit 55e4867

2 files changed

Lines changed: 82 additions & 13 deletions

File tree

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

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

18+
import com.github.difflib.patch.ChangeDelta;
19+
import com.github.difflib.patch.Chunk;
1820
import java.io.BufferedReader;
1921
import java.io.IOException;
2022
import java.io.InputStream;
@@ -35,7 +37,7 @@
3537
*/
3638
public final class UnifiedDiffParser {
3739

38-
private static final String UNIFIED_DIFF_CHUNK_REGEXP = "^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@$";
40+
static final Pattern UNIFIED_DIFF_CHUNK_REGEXP = Pattern.compile("^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@");
3941

4042
private final UnifiedDiffReader READER;
4143
private final UnifiedDiff data = new UnifiedDiff();
@@ -62,6 +64,7 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
6264
String headerTxt = "";
6365
while (READER.ready()) {
6466
String line = READER.readLine();
67+
LOG.log(Level.INFO, "parsing line {0}", line);
6568
if (processLine(header, line) == false) {
6669
if (header) {
6770
headerTxt += line + "\n";
@@ -118,25 +121,54 @@ public void processDiff(MatchResult match, String line) {
118121
actualFile.setDiffCommand(line);
119122
}
120123

121-
public void processChunk(MatchResult match, String chunkStart) {
124+
public void processChunk(MatchResult _match, String chunkStart) {
125+
MatchResult match = _match;
122126
try {
123-
List<String> originalTxt = new ArrayList<>();
124-
List<String> revisedTxt = new ArrayList<>();
125127

126-
int old_ln = match.group(1) == null ? 1 : Integer.parseInt(match.group(1));
127-
int new_ln = match.group(3) == null ? 1 : Integer.parseInt(match.group(3));
128+
while (true) {
128129

129-
while (this.READER.ready()) {
130-
String line = READER.readLine();
130+
List<String> originalTxt = new ArrayList<>();
131+
List<String> revisedTxt = new ArrayList<>();
131132

132-
if (line.startsWith(" ") || line.startsWith("+")) {
133-
revisedTxt.add(line.substring(1));
133+
int old_ln = match.group(1) == null ? 1 : Integer.parseInt(match.group(1));
134+
int new_ln = match.group(3) == null ? 1 : Integer.parseInt(match.group(3));
135+
if (old_ln == 0) {
136+
old_ln = 1;
134137
}
135-
if (line.startsWith(" ") || line.startsWith("-")) {
136-
originalTxt.add(line.substring(1));
138+
if (new_ln == 0) {
139+
new_ln = 1;
137140
}
138-
if (line.equals("")) {
141+
142+
while (this.READER.ready()) {
143+
String line = READER.readLine();
144+
LOG.log(Level.INFO, "processing chunk line {0}", line);
145+
146+
if (line.startsWith(" ") || line.startsWith("+")) {
147+
revisedTxt.add(line.substring(1));
148+
}
149+
if (line.startsWith(" ") || line.startsWith("-")) {
150+
originalTxt.add(line.substring(1));
151+
}
152+
if (line.equals("") || line.startsWith("@@") || line.startsWith("--")) {
153+
break;
154+
}
155+
}
156+
157+
actualFile.getPatch().addDelta(new ChangeDelta<>(new Chunk<>(
158+
old_ln - 1, originalTxt), new Chunk<>(
159+
new_ln - 1, revisedTxt)));
160+
161+
if (READER.lastLine().equals("")
162+
|| READER.lastLine().startsWith("--")
163+
|| !READER.lastLine().startsWith("@@")) {
139164
break;
165+
} else {
166+
Matcher m = UNIFIED_DIFF_CHUNK_REGEXP.matcher(READER.lastLine());
167+
if (m.find()) {
168+
match = m.toMatchResult();
169+
} else {
170+
break;
171+
}
140172
}
141173
}
142174

@@ -182,6 +214,12 @@ public UnifiedDiffLine(boolean stopsHeaderParsing, String pattern, BiConsumer<Ma
182214
this.stopsHeaderParsing = stopsHeaderParsing;
183215
}
184216

217+
public UnifiedDiffLine(boolean stopsHeaderParsing, Pattern pattern, BiConsumer<MatchResult, String> command) {
218+
this.pattern = pattern;
219+
this.command = command;
220+
this.stopsHeaderParsing = stopsHeaderParsing;
221+
}
222+
185223
public boolean processLine(String line) throws UnifiedDiffParserException {
186224
Matcher m = pattern.matcher(line);
187225
if (m.find()) {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
package com.github.difflib.unifieddiff;
1717

1818
import java.io.IOException;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
1921
import static org.assertj.core.api.Java6Assertions.assertThat;
2022
import org.junit.After;
2123
import org.junit.AfterClass;
24+
import static org.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertTrue;
2226
import org.junit.Before;
2327
import org.junit.BeforeClass;
2428
import org.junit.Test;
@@ -53,6 +57,12 @@ public void testSimpleParse() throws IOException {
5357
UnifiedDiff diff = UnifiedDiffParser.parseUnifiedDiff(UnifiedDiffParserTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));
5458

5559
System.out.println(diff);
60+
61+
assertThat(diff.getFiles().size()).isEqualTo(2);
62+
63+
UnifiedDiffFile file1 = diff.getFiles().get(0);
64+
assertThat(file1.getFromFile()).isEqualTo("src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt");
65+
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(3);
5666
}
5767

5868
@Test
@@ -61,4 +71,25 @@ public void testParseDiffBlock() {
6171
assertThat(files).containsExactly("src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java", "src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java");
6272
}
6373

74+
@Test
75+
public void testChunkHeaderParsing() {
76+
Pattern pattern = UnifiedDiffParser.UNIFIED_DIFF_CHUNK_REGEXP;
77+
Matcher matcher = pattern.matcher("@@ -189,6 +189,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */");
78+
79+
assertTrue(matcher.find());
80+
assertEquals("189", matcher.group(1));
81+
assertEquals("189", matcher.group(3));
82+
}
83+
84+
@Test
85+
public void testChunkHeaderParsing2() {
86+
//"^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@.*$"
87+
Pattern pattern = UnifiedDiffParser.UNIFIED_DIFF_CHUNK_REGEXP;
88+
Matcher matcher = pattern.matcher("@@ -189,6 +189,7 @@");
89+
90+
assertTrue(matcher.find());
91+
assertEquals("189", matcher.group(1));
92+
assertEquals("189", matcher.group(3));
93+
}
94+
6495
}

0 commit comments

Comments
 (0)