Skip to content

Commit 614e861

Browse files
committed
Fix for issue dnaumenko#20 correcting empty-context add-only patches
Empty-context add-only patches were applied in the wrong place, typically one line early. This commit uses the code submitted in the issue but tidied up and corrected for current master snapshot.
1 parent 683d192 commit 614e861

File tree

5 files changed

+102
-24
lines changed

5 files changed

+102
-24
lines changed

src/main/java/difflib/DiffUtils.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -159,25 +159,8 @@ public static Patch<String> parseUnifiedDiff(List<String> diff) {
159159
Matcher m = unifiedDiffChunkRe.matcher(line);
160160
if (m.find()) {
161161
// Process the lines in the previous chunk
162-
if (rawChunk.size() != 0) {
163-
List<String> oldChunkLines = new ArrayList<String>();
164-
List<String> newChunkLines = new ArrayList<String>();
165-
166-
for (String[] raw_line : rawChunk) {
167-
tag = raw_line[0];
168-
rest = raw_line[1];
169-
if (tag.equals(" ") || tag.equals("-")) {
170-
oldChunkLines.add(rest);
171-
}
172-
if (tag.equals(" ") || tag.equals("+")) {
173-
newChunkLines.add(rest);
174-
}
175-
}
176-
patch.addDelta(new ChangeDelta<String>(new Chunk<String>(
177-
old_ln - 1, oldChunkLines), new Chunk<String>(
178-
new_ln - 1, newChunkLines)));
179-
rawChunk.clear();
180-
}
162+
processRawChunk(rawChunk, patch, old_ln, new_ln);
163+
181164
// Parse the @@ header
182165
old_ln = m.group(1) == null ? 1 : Integer.parseInt(m.group(1));
183166
new_ln = m.group(3) == null ? 1 : Integer.parseInt(m.group(3));
@@ -202,6 +185,15 @@ public static Patch<String> parseUnifiedDiff(List<String> diff) {
202185
}
203186

204187
// Process the lines in the last chunk
188+
processRawChunk(rawChunk, patch, old_ln, new_ln);
189+
190+
return patch;
191+
}
192+
193+
public static void processRawChunk(List<String[]> rawChunk, Patch patch, int old_ln, int new_ln) {
194+
String tag;
195+
String rest;
196+
205197
if (rawChunk.size() != 0) {
206198
List<String> oldChunkLines = new ArrayList<String>();
207199
List<String> newChunkLines = new ArrayList<String>();
@@ -217,13 +209,21 @@ public static Patch<String> parseUnifiedDiff(List<String> diff) {
217209
}
218210
}
219211

220-
patch.addDelta(new ChangeDelta<String>(new Chunk<String>(
221-
old_ln - 1, oldChunkLines), new Chunk<String>(new_ln - 1,
222-
newChunkLines)));
212+
if (oldChunkLines.isEmpty()) {
213+
patch.addDelta(new InsertDelta<String>(new Chunk<String>(
214+
old_ln, oldChunkLines), new Chunk<String>(new_ln - 1,
215+
newChunkLines)));
216+
} else if (newChunkLines.isEmpty()) {
217+
patch.addDelta(new InsertDelta<String>(new Chunk<String>(
218+
old_ln - 1, oldChunkLines), new Chunk<String>(new_ln,
219+
newChunkLines)));
220+
} else {
221+
patch.addDelta(new ChangeDelta<String>(new Chunk<String>(
222+
old_ln - 1, oldChunkLines), new Chunk<String>(
223+
new_ln - 1, newChunkLines)));
224+
}
223225
rawChunk.clear();
224226
}
225-
226-
return patch;
227227
}
228228

229229
/**
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package diffutils;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
10+
import difflib.DiffUtils;
11+
import difflib.Patch;
12+
import difflib.PatchFailedException;
13+
14+
import junit.framework.TestCase;
15+
16+
public class EmptyContextUnifiedDiffTest extends TestCase {
17+
18+
public List<String> fileToLines(String filename) {
19+
List<String> lines = new LinkedList<String>();
20+
String line = "";
21+
try {
22+
BufferedReader in = new BufferedReader(new FileReader(filename));
23+
while ((line = in.readLine()) != null) {
24+
lines.add(line);
25+
}
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
fail(e.getMessage());
29+
}
30+
return lines;
31+
}
32+
33+
public void testUnifiedContextInsert() {
34+
testUnifiedContextXXX(TestConstants.MOCK_FOLDER + "uc_insert_patch.txt", TestConstants.MOCK_FOLDER
35+
+ "uc_insert_revised.txt");
36+
}
37+
38+
public void testUnifiedContextXXX(String patch_file, String revised_file) {
39+
List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "uc_original.txt");
40+
List<String> revLines = fileToLines(revised_file);
41+
List<String> unifiedDiff = fileToLines(patch_file);
42+
List<String> patchedLines = null;
43+
Patch patch = DiffUtils.parseUnifiedDiff(unifiedDiff);
44+
45+
try {
46+
patchedLines = (List<String>) patch.applyTo(origLines);
47+
} catch (PatchFailedException e) {
48+
fail(e.getMessage());
49+
}
50+
51+
verifyLinesEqual(patchedLines, revLines);
52+
}
53+
54+
public void verifyLinesEqual(List<String> patchedLines, List<String> revLines) {
55+
assertTrue(revLines.size() == patchedLines.size());
56+
for (int i = 0; i < revLines.size(); i++) {
57+
String l1 = revLines.get(i);
58+
String l2 = patchedLines.get(i);
59+
if (!l1.equals(l2)) {
60+
fail("Line " + (i + 1) + " of the patched file did not match the revised original");
61+
}
62+
}
63+
}
64+
65+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- uc_original.txt 2011-06-14 16:21:56.578627000 +0300
2+
+++ uc_insert_revised.txt 2011-06-14 16:20:37.654820000 +0300
3+
@@ -2,0 +3 @@
4+
+not
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This
2+
is
3+
not
4+
a
5+
test
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This
2+
is
3+
a
4+
test

0 commit comments

Comments
 (0)