Skip to content

Commit 16cd9c6

Browse files
committed
1 parent a34c185 commit 16cd9c6

File tree

5 files changed

+168
-33
lines changed

5 files changed

+168
-33
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private void initFileIfNecessary() {
132132
}
133133
}
134134

135-
public void processDiff(MatchResult match, String line) {
135+
private void processDiff(MatchResult match, String line) {
136136
initFileIfNecessary();
137137
LOG.log(Level.INFO, "start {0}", line);
138138
String[] fromTo = parseFileNames(READER.lastLine());
@@ -158,23 +158,23 @@ private void finalizeChunk() {
158158
}
159159
}
160160

161-
public void processNormalLine(MatchResult match, String line) {
161+
private void processNormalLine(MatchResult match, String line) {
162162
String cline = line.substring(1);
163163
originalTxt.add(cline);
164164
revisedTxt.add(cline);
165165
}
166166

167-
public void processAddLine(MatchResult match, String line) {
167+
private void processAddLine(MatchResult match, String line) {
168168
String cline = line.substring(1);
169169
revisedTxt.add(cline);
170170
}
171171

172-
public void processDelLine(MatchResult match, String line) {
172+
private void processDelLine(MatchResult match, String line) {
173173
String cline = line.substring(1);
174174
originalTxt.add(cline);
175175
}
176176

177-
public void processChunk(MatchResult match, String chunkStart) {
177+
private void processChunk(MatchResult match, String chunkStart) {
178178
finalizeChunk();
179179
old_ln = match.group(1) == null ? 1 : Integer.parseInt(match.group(1));
180180
new_ln = match.group(3) == null ? 1 : Integer.parseInt(match.group(3));
@@ -186,7 +186,7 @@ public void processChunk(MatchResult match, String chunkStart) {
186186
}
187187
}
188188

189-
public void processIndex(MatchResult match, String line) {
189+
private void processIndex(MatchResult match, String line) {
190190
initFileIfNecessary();
191191
LOG.log(Level.INFO, "index {0}", line);
192192
actualFile.setIndex(line.substring(6));

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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,29 @@
2121
import java.util.ArrayList;
2222
import java.util.List;
2323
import java.util.function.Consumer;
24+
import java.util.function.Function;
2425
import java.util.logging.Level;
2526
import java.util.logging.Logger;
2627

2728
/**
28-
*
29+
* @todo use an instance to store contextSize and originalLinesProvider.
2930
* @author Tobias Warneke (t.warneke@gmx.net)
3031
*/
3132
public class UnifiedDiffWriter {
3233

3334
private static final Logger LOG = Logger.getLogger(UnifiedDiffWriter.class.getName());
3435

35-
public static void write(UnifiedDiff diff, Writer writer) throws IOException {
36-
write(diff, line -> {
36+
public static void write(UnifiedDiff diff, Function<String, List<String>> originalLinesProvider, Writer writer, int contextSize) throws IOException {
37+
write(diff, originalLinesProvider, line -> {
3738
try {
3839
writer.append(line).append("\n");
3940
} catch (IOException ex) {
4041
LOG.log(Level.SEVERE, null, ex);
4142
}
42-
});
43+
}, contextSize);
4344
}
4445

45-
public static void write(UnifiedDiff diff, Consumer<String> writer) throws IOException {
46+
public static void write(UnifiedDiff diff, Function<String, List<String>> originalLinesProvider, Consumer<String> writer, int contextSize) throws IOException {
4647
writer.accept(diff.getHeader());
4748

4849
for (UnifiedDiffFile file : diff.getFiles()) {
@@ -57,14 +58,13 @@ public static void write(UnifiedDiff diff, Consumer<String> writer) throws IOExc
5758
writer.accept("+++ " + file.getToFile());
5859
}
5960

61+
List<String> originalLines = originalLinesProvider.apply(file.getFromFile());
62+
6063
List<AbstractDelta<String>> patchDeltas = new ArrayList<>(
6164
file.getPatch().getDeltas());
6265

6366
List<AbstractDelta<String>> deltas = new ArrayList<>();
6467

65-
int contextSize = 0;
66-
List<String> originalLines = new ArrayList<>();
67-
6868
AbstractDelta<String> delta = patchDeltas.get(0);
6969
deltas.add(delta); // add the first Delta to the current set
7070
// if there's more than 1 Delta, we may need to output them together
@@ -83,7 +83,7 @@ public static void write(UnifiedDiff diff, Consumer<String> writer) throws IOExc
8383
// if it isn't, output the current set,
8484
// then create a new set and add the current Delta to
8585
// it.
86-
processDeltas(writer, deltas);
86+
processDeltas(writer, originalLines, deltas, contextSize);
8787
deltas.clear();
8888
deltas.add(nextDelta);
8989
}
@@ -92,7 +92,7 @@ public static void write(UnifiedDiff diff, Consumer<String> writer) throws IOExc
9292

9393
}
9494
// don't forget to process the last set of Deltas
95-
processDeltas(writer, deltas);
95+
processDeltas(writer, originalLines, deltas, contextSize);
9696

9797
}
9898
if (diff.getTail() != null) {
@@ -102,7 +102,8 @@ public static void write(UnifiedDiff diff, Consumer<String> writer) throws IOExc
102102
}
103103

104104
private static void processDeltas(Consumer<String> writer,
105-
List<AbstractDelta<String>> deltas) {
105+
List<String> origLines, List<AbstractDelta<String>> deltas,
106+
int contextSize) {
106107
List<String> buffer = new ArrayList<>();
107108
int origTotal = 0; // counter for total lines output from Original
108109
int revTotal = 0; // counter for total lines output from Original
@@ -143,13 +144,13 @@ private static void processDeltas(Consumer<String> writer,
143144
AbstractDelta<String> nextDelta = deltas.get(deltaIndex);
144145
int intermediateStart = curDelta.getSource().getPosition()
145146
+ curDelta.getSource().getLines().size();
146-
// for (line = intermediateStart; line < nextDelta.getSource()
147-
// .getPosition(); line++) {
148-
// // output the code between the last Delta and this one
149-
// buffer.add(" " + origLines.get(line));
150-
// origTotal++;
151-
// revTotal++;
152-
// }
147+
for (line = intermediateStart; line < nextDelta.getSource()
148+
.getPosition(); line++) {
149+
// output the code between the last Delta and this one
150+
buffer.add(" " + origLines.get(line));
151+
origTotal++;
152+
revTotal++;
153+
}
153154
getDeltaText(txt -> buffer.add(txt), nextDelta); // output the Delta
154155
origTotal += nextDelta.getSource().getLines().size();
155156
revTotal += nextDelta.getTarget().getLines().size();
@@ -160,12 +161,12 @@ private static void processDeltas(Consumer<String> writer,
160161
// Now output the post-Delta context code, clamping the end of the file
161162
contextStart = curDelta.getSource().getPosition()
162163
+ curDelta.getSource().getLines().size();
163-
// for (line = contextStart; (line < (contextStart + contextSize))
164-
// && (line < origLines.size()); line++) {
165-
// buffer.add(" " + origLines.get(line));
166-
// origTotal++;
167-
// revTotal++;
168-
// }
164+
for (line = contextStart; (line < (contextStart + contextSize))
165+
&& (line < origLines.size()); line++) {
166+
buffer.add(" " + origLines.get(line));
167+
origTotal++;
168+
revTotal++;
169+
}
169170

170171
// Create and insert the block header, conforming to the Unified Diff
171172
// standard

src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
public class GenerateUnifiedDiffTest {
1818

19-
private static List<String> fileToLines(String filename) throws FileNotFoundException, IOException {
19+
public static List<String> fileToLines(String filename) throws FileNotFoundException, IOException {
2020
List<String> lines = new ArrayList<>();
2121
String line = "";
2222
try (BufferedReader in = new BufferedReader(new FileReader(filename))) {
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.github.difflib.unifieddiff;
2+
3+
import com.github.difflib.DiffUtils;
4+
import com.github.difflib.algorithm.DiffException;
5+
import com.github.difflib.patch.Patch;
6+
import java.io.BufferedReader;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileReader;
9+
import java.io.IOException;
10+
import java.io.StringWriter;
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.List;
14+
import org.junit.Test;
15+
16+
public class UnifiedDiffRoundTripTest {
17+
18+
public static List<String> fileToLines(String filename) throws FileNotFoundException, IOException {
19+
List<String> lines = new ArrayList<>();
20+
String line = "";
21+
try (BufferedReader in = new BufferedReader(new FileReader(filename))) {
22+
while ((line = in.readLine()) != null) {
23+
lines.add(line);
24+
}
25+
}
26+
return lines;
27+
}
28+
29+
// @Test
30+
// public void testGenerateUnified() throws DiffException, IOException {
31+
// List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt");
32+
// List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt");
33+
//
34+
// verify(origLines, revLines, "original.txt", "revised.txt");
35+
// }
36+
//
37+
// @Test
38+
// public void testGenerateUnifiedWithOneDelta() throws DiffException, IOException {
39+
// List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "one_delta_test_original.txt");
40+
// List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "one_delta_test_revised.txt");
41+
//
42+
// verify(origLines, revLines, "one_delta_test_original.txt", "one_delta_test_revised.txt");
43+
// }
44+
@Test
45+
public void testGenerateUnifiedDiffWithoutAnyDeltas() throws DiffException, IOException {
46+
List<String> test = Arrays.asList("abc");
47+
Patch<String> patch = DiffUtils.diff(test, test);
48+
StringWriter writer = new StringWriter();
49+
50+
UnifiedDiffWriter.write(
51+
UnifiedDiff.from("header", "tail", UnifiedDiffFile.from("abc", "abc", patch)),
52+
name -> test,
53+
writer, 0);
54+
55+
System.out.println(writer);
56+
}
57+
58+
// @Test
59+
// public void testDiff_Issue10() throws IOException {
60+
// final List<String> baseLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_base.txt");
61+
// final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");
62+
// final Patch<String> p = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
63+
// try {
64+
// DiffUtils.patch(baseLines, p);
65+
// } catch (PatchFailedException e) {
66+
// fail(e.getMessage());
67+
// }
68+
// }
69+
//
70+
// /**
71+
// * Issue 12
72+
// */
73+
// @Test
74+
// public void testPatchWithNoDeltas() throws DiffException, IOException {
75+
// final List<String> lines1 = fileToLines(TestConstants.MOCK_FOLDER + "issue11_1.txt");
76+
// final List<String> lines2 = fileToLines(TestConstants.MOCK_FOLDER + "issue11_2.txt");
77+
// verify(lines1, lines2, "issue11_1.txt", "issue11_2.txt");
78+
// }
79+
//
80+
// @Test
81+
// public void testDiff5() throws DiffException, IOException {
82+
// final List<String> lines1 = fileToLines(TestConstants.MOCK_FOLDER + "5A.txt");
83+
// final List<String> lines2 = fileToLines(TestConstants.MOCK_FOLDER + "5B.txt");
84+
// verify(lines1, lines2, "5A.txt", "5B.txt");
85+
// }
86+
//
87+
// /**
88+
// * Issue 19
89+
// */
90+
// @Test
91+
// public void testDiffWithHeaderLineInText() throws DiffException {
92+
// List<String> original = new ArrayList<>();
93+
// List<String> revised = new ArrayList<>();
94+
//
95+
// original.add("test line1");
96+
// original.add("test line2");
97+
// original.add("test line 4");
98+
// original.add("test line 5");
99+
//
100+
// revised.add("test line1");
101+
// revised.add("test line2");
102+
// revised.add("@@ -2,6 +2,7 @@");
103+
// revised.add("test line 4");
104+
// revised.add("test line 5");
105+
//
106+
// Patch<String> patch = DiffUtils.diff(original, revised);
107+
// List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff("original", "revised",
108+
// original, patch, 10);
109+
// UnifiedDiffUtils.parseUnifiedDiff(udiff);
110+
// }
111+
//
112+
// private void verify(List<String> origLines, List<String> revLines,
113+
// String originalFile, String revisedFile) throws DiffException {
114+
// Patch<String> patch = DiffUtils.diff(origLines, revLines);
115+
// List<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile,
116+
// origLines, patch, 10);
117+
//
118+
// Patch<String> fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
119+
// List<String> patchedLines;
120+
// try {
121+
// patchedLines = fromUnifiedPatch.applyTo(origLines);
122+
// assertEquals(revLines.size(), patchedLines.size());
123+
// for (int i = 0; i < revLines.size(); i++) {
124+
// String l1 = revLines.get(i);
125+
// String l2 = patchedLines.get(i);
126+
// if (!l1.equals(l2)) {
127+
// fail("Line " + (i + 1) + " of the patched file did not match the revised original");
128+
// }
129+
// }
130+
// } catch (PatchFailedException e) {
131+
// fail(e.getMessage());
132+
// }
133+
// }
134+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public void testWrite() throws URISyntaxException, IOException {
4040
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(str.getBytes()));
4141

4242
StringWriter writer = new StringWriter();
43-
UnifiedDiffWriter.write(diff, writer);
44-
System.out.println(writer.toString());
43+
// UnifiedDiffWriter.write(diff, writer);
44+
// System.out.println(writer.toString());
4545
}
4646

4747
static String readFile(URI path, Charset encoding)

0 commit comments

Comments
 (0)