Skip to content
This repository was archived by the owner on Nov 12, 2019. It is now read-only.

Commit aeed6b8

Browse files
author
shotwave
committed
Issues 10, 12 fixed
git-svn-id: http://java-diff-utils.googlecode.com/svn/trunk@26 d8d7d024-a22d-11de-b755-fd640f38fa9d
1 parent 74f025e commit aeed6b8

File tree

6 files changed

+136
-56
lines changed

6 files changed

+136
-56
lines changed

src/difflib/DiffUtils.java

+47-41
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
*/
1616
package difflib;
1717

18-
import java.util.*;
18+
import difflib.myers.MyersDiff;
19+
20+
import java.util.ArrayList;
21+
import java.util.Collections;
22+
import java.util.Comparator;
23+
import java.util.List;
1924
import java.util.regex.Matcher;
2025
import java.util.regex.Pattern;
2126

22-
import difflib.myers.*;
23-
2427
/**
2528
* Implements the difference and patching engine
2629
*
@@ -140,6 +143,8 @@ public static Patch parseUnifiedDiff(List<String> diff) {
140143
if (tag.equals(" ") || tag.equals("+") || tag.equals("-")) {
141144
rawChunk.add(new Object[] { tag, rest });
142145
}
146+
} else {
147+
rawChunk.add(new Object[] {" ", ""});
143148
}
144149
}
145150
}
@@ -184,47 +189,48 @@ public static List<String> generateUnifiedDiff(String original, String revised,
184189
List<String> ret = new ArrayList<String>();
185190
ret.add("--- " + original);
186191
ret.add("+++ " + revised);
187-
188-
// Hmm, I thought the Deltas were sorted already... turns out they're not.
189-
List<Delta> patchDeltas = new ArrayList<Delta>( patch.getDeltas() );
190-
Collections.sort( patchDeltas, new Comparator<Delta>() {
191-
public int compare( Delta a, Delta b ) {
192-
return new Integer(a.getOriginal().getPosition()).compareTo( b.getOriginal().getPosition() );
193-
}
194-
});
195-
196-
// code outside the if block also works for single-delta issues.
197-
List<Delta> deltas = new ArrayList<Delta>(); // current list of Delta's to process
198-
Delta delta = patchDeltas.get(0);
199-
deltas.add(delta); // add the first Delta to the current set
200-
// if there's more than 1 Delta, we may need to output them together
201-
if (patchDeltas.size() > 1) {
202-
for (int i = 1; i < patchDeltas.size(); i++) {
203-
int position = delta.getOriginal().getPosition(); // store the current position of
204-
// the first Delta
205-
206-
// Check if the next Delta is too close to the current position.
207-
// And if it is, add it to the current set
208-
Delta nextDelta = patchDeltas.get(i);
209-
if ((position + delta.getOriginal().getSize() + contextSize) >=
210-
(nextDelta.getOriginal().getPosition() - contextSize)) {
211-
deltas.add(nextDelta);
212-
} else {
213-
// if it isn't, output the current set,
214-
// then create a new set and add the current Delta to it.
215-
List<String> curBlock = processDeltas(originalLines, deltas, contextSize);
216-
ret.addAll(curBlock);
217-
deltas.clear();
218-
deltas.add(nextDelta);
192+
193+
if (!patch.getDeltas().isEmpty()) {
194+
// Hmm, I thought the Deltas were sorted already... turns out they're not.
195+
List<Delta> patchDeltas = new ArrayList<Delta>( patch.getDeltas() );
196+
Collections.sort( patchDeltas, new Comparator<Delta>() {
197+
public int compare( Delta a, Delta b ) {
198+
return new Integer(a.getOriginal().getPosition()).compareTo( b.getOriginal().getPosition() );
219199
}
220-
delta = nextDelta;
200+
});
201+
202+
// code outside the if block also works for single-delta issues.
203+
List<Delta> deltas = new ArrayList<Delta>(); // current list of Delta's to process
204+
Delta delta = patchDeltas.get(0);
205+
deltas.add(delta); // add the first Delta to the current set
206+
// if there's more than 1 Delta, we may need to output them together
207+
if (patchDeltas.size() > 1) {
208+
for (int i = 1; i < patchDeltas.size(); i++) {
209+
int position = delta.getOriginal().getPosition(); // store the current position of
210+
// the first Delta
211+
212+
// Check if the next Delta is too close to the current position.
213+
// And if it is, add it to the current set
214+
Delta nextDelta = patchDeltas.get(i);
215+
if ((position + delta.getOriginal().getSize() + contextSize) >=
216+
(nextDelta.getOriginal().getPosition() - contextSize)) {
217+
deltas.add(nextDelta);
218+
} else {
219+
// if it isn't, output the current set,
220+
// then create a new set and add the current Delta to it.
221+
List<String> curBlock = processDeltas(originalLines, deltas, contextSize);
222+
ret.addAll(curBlock);
223+
deltas.clear();
224+
deltas.add(nextDelta);
225+
}
226+
delta = nextDelta;
227+
}
228+
221229
}
222-
230+
// don't forget to process the last set of Deltas
231+
List<String> curBlock = processDeltas(originalLines, deltas, contextSize);
232+
ret.addAll(curBlock);
223233
}
224-
// don't forget to process the last set of Deltas
225-
List<String> curBlock = processDeltas(originalLines, deltas, contextSize);
226-
ret.addAll(curBlock);
227-
228234
return ret;
229235
}
230236

test/mocks/issue10_base.txt

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="Python" name="Python">
5+
<configuration sdkName="Python 2.7 (C:/Python27/python.exe)" />
6+
</facet>
7+
</component>
8+
<component name="NewModuleRootManager" inherit-compiler-output="true">
9+
<exclude-output />
10+
<content url="file://$MODULE_DIR$">
11+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
12+
<sourceFolder url="file://$MODULE_DIR$/resources" isTestSource="false" />
13+
</content>
14+
<orderEntry type="inheritedJdk" />
15+
<orderEntry type="sourceFolder" forTests="false" />
16+
<orderEntry type="library" name="Python 2.7 (C:/Python27/python.exe) interpreter library" level="application" />
17+
<orderEntry type="module-library">
18+
<library>
19+
<CLASSES>
20+
<root url="jar://$MODULE_DIR$/lib/commons-io-1.4.jar!/" />
21+
</CLASSES>
22+
<JAVADOC />
23+
<SOURCES />
24+
</library>
25+
</orderEntry>
26+
<orderEntry type="module-library">
27+
<library>
28+
<CLASSES>
29+
<root url="jar://$MODULE_DIR$/lib/java-diff-utils-1.2.jar!/" />
30+
</CLASSES>
31+
<JAVADOC />
32+
<SOURCES />
33+
</library>
34+
</orderEntry>
35+
<orderEntry type="module-library">
36+
<library>
37+
<CLASSES>
38+
<root url="jar://$MODULE_DIR$/lib/jython.jar!/" />
39+
</CLASSES>
40+
<JAVADOC />
41+
<SOURCES />
42+
</library>
43+
</orderEntry>
44+
</component>
45+
</module>
46+

test/mocks/issue10_patch.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--- /bonobo/bonobo.iml
2+
+++ /bonobo/bonobo.iml
3+
@@ -41,6 +40,7 @@
4+
<SOURCES />
5+
</library>
6+
</orderEntry>
7+
+ <orderEntry type="library" name="Python 2.6.6 (/usr/bin/python2.6) interpreter library" level="application" />
8+
</component>
9+
</module>
10+

test/mocks/issue11_1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
svn: '77954' path not found

test/mocks/issue11_2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
svn: '77954' path not found
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package diffutills;
22

3+
import difflib.DiffUtils;
4+
import difflib.Patch;
5+
import difflib.PatchFailedException;
6+
import junit.framework.TestCase;
7+
38
import java.io.BufferedReader;
49
import java.io.File;
510
import java.io.FileReader;
@@ -8,19 +13,13 @@
813
import java.util.LinkedList;
914
import java.util.List;
1015

11-
import difflib.DiffUtils;
12-
import difflib.Patch;
13-
import difflib.PatchFailedException;
14-
15-
import junit.framework.TestCase;
16-
1716
public class GenerateUnifiedDiffTest extends TestCase {
1817
static final String FS = File.separator;
1918
static final String originalFilename = "test" + FS + "mocks" + FS + "original.txt";
2019
static final String revisedFilename = "test" + FS + "mocks" + FS + "revised.txt";
2120
static final String originalFilenameOneDelta = "test" + FS + "mocks" + FS + "one_delta_test_original.txt";
2221
static final String revisedFilenameOneDelta = "test" + FS + "mocks" + FS + "one_delta_test_revised.txt";
23-
22+
2423
public List<String> fileToLines(String filename) {
2524
List<String> lines = new LinkedList<String>();
2625
String line = "";
@@ -34,37 +33,37 @@ public List<String> fileToLines(String filename) {
3433
}
3534
return lines;
3635
}
37-
36+
3837
/**
3938
* Tests the Unified Diff generation by creating a Patch, then
4039
* creating the Unified Diff representation, then parsing that
4140
* Unified Diff, and applying the patch to the original unrevised
4241
* text, then comparing that to the original revised text.
43-
*
42+
*
4443
* @author Bill James ([email protected])
4544
*/
4645
public void testGenerateUnified() {
4746
List<String> origLines = fileToLines(originalFilename);
4847
List<String> revLines = fileToLines(revisedFilename);
49-
48+
5049
testGenerateUnified(origLines, revLines);
5150
}
52-
51+
5352
/**
5453
* Tests the Unified Diff generation for diff with one delta.
5554
*/
5655
public void testGenerateUnifiedWithOneDelta() {
5756
List<String> origLines = fileToLines(originalFilenameOneDelta);
5857
List<String> revLines = fileToLines(revisedFilenameOneDelta);
59-
58+
6059
testGenerateUnified(origLines, revLines);
6160
}
62-
61+
6362
private void testGenerateUnified(List<String> origLines, List<String> revLines) {
6463
Patch p = DiffUtils.diff(origLines, revLines);
6564
List<String> unifiedDiff = DiffUtils.generateUnifiedDiff(
6665
originalFilename, revisedFilename, origLines, p, 10);
67-
66+
6867
Patch fromUnifiedPatch = DiffUtils.parseUnifiedDiff(unifiedDiff);
6968
List<String> patchedLines = new ArrayList<String>();
7069
try {
@@ -73,7 +72,7 @@ private void testGenerateUnified(List<String> origLines, List<String> revLines)
7372
// TODO Auto-generated catch block
7473
fail(e.getMessage());
7574
}
76-
75+
7776
assertTrue(revLines.size() == patchedLines.size());
7877
for (int i = 0; i < revLines.size(); i++) {
7978
String l1 = revLines.get(i);
@@ -84,5 +83,22 @@ private void testGenerateUnified(List<String> origLines, List<String> revLines)
8483
}
8584
}
8685

86+
public void testDiff_Issue10() throws Exception {
87+
final List<String> baseLines = fileToLines("test" + FS + "mocks" + FS + "issue10_base.txt");
88+
final List<String> patchLines = fileToLines("test" + FS + "mocks" + FS + "issue10_patch.txt");
89+
final Patch p = DiffUtils.parseUnifiedDiff(patchLines);
90+
DiffUtils.patch(baseLines, p);
91+
}
92+
93+
public void testDiff_Issue11() throws Exception {
94+
final List<String> lines1 = fileToLines("test" + FS + "mocks" + FS + "issue11_1.txt");
95+
final List<String> lines2 = fileToLines("test" + FS + "mocks" + FS + "issue11_2.txt");
8796

97+
98+
final Patch patch = DiffUtils.diff(lines1, lines2);
99+
final String fileName = "xxx";
100+
final List<String> stringList = DiffUtils.generateUnifiedDiff(fileName, fileName, lines1, patch, 3);
101+
final Patch x = DiffUtils.parseUnifiedDiff(stringList);
102+
DiffUtils.patch(lines1, x);
103+
}
88104
}

0 commit comments

Comments
 (0)