Skip to content

Commit e39313e

Browse files
committed
feat: text-based diffing method
1 parent 757ccbf commit e39313e

2 files changed

Lines changed: 69 additions & 31 deletions

File tree

src/main/java/org/variantsync/diffdetective/variation/diff/VariationDiff.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,20 @@ public static VariationDiff<DiffLinesLabel> fromFile(final Path p, VariationDiff
105105
* @throws DiffParseException if {@code diff} couldn't be parsed
106106
*/
107107
public static VariationDiff<DiffLinesLabel> fromDiff(final String diff, final VariationDiffParseOptions parseOptions) throws DiffParseException {
108-
final VariationDiff<DiffLinesLabel> tree = VariationDiffParser.createVariationDiff(diff, parseOptions);
109-
tree.setSource(new PatchString(diff));
110-
return tree;
108+
final VariationDiff<DiffLinesLabel> d;
109+
try {
110+
d = VariationDiffParser.createVariationDiff(diff, parseOptions);
111+
} catch (DiffParseException e) {
112+
Logger.error("""
113+
Could not parse diff:
114+
115+
{}
116+
""",
117+
diff);
118+
throw e;
119+
}
120+
d.setSource(new PatchString(diff));
121+
return d;
111122
}
112123

113124
/**

src/main/java/org/variantsync/diffdetective/variation/diff/construction/JGitDiff.java

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package org.variantsync.diffdetective.variation.diff.construction;
22

3+
import org.apache.commons.io.IOUtils;
34
import org.eclipse.jgit.diff.*;
4-
import org.tinylog.Logger;
55
import org.variantsync.diffdetective.diff.git.GitDiffer;
66
import org.variantsync.diffdetective.diff.result.DiffParseException;
77
import org.variantsync.diffdetective.variation.DiffLinesLabel;
88
import org.variantsync.diffdetective.variation.diff.Time;
99
import org.variantsync.diffdetective.variation.diff.VariationDiff;
1010
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions;
11-
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParser;
12-
import org.variantsync.diffdetective.variation.diff.source.PatchString;
1311

1412
import java.io.BufferedReader;
1513
import java.io.ByteArrayOutputStream;
1614
import java.io.IOException;
1715
import java.io.StringReader;
1816
import java.nio.charset.StandardCharsets;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
1919
import java.util.regex.Pattern;
2020

2121
/**
@@ -26,25 +26,42 @@ public final class JGitDiff {
2626
private final static Pattern NO_NEWLINE_AT_END_OF_FILE = Pattern.compile("\n\\\\ No newline at end of file");
2727

2828
private JGitDiff() {}
29+
30+
/**
31+
* Creates a text-based diff from two line-based text inputs.
32+
* Uses JGit to diff the two files using the specified {@code options}.
33+
* @param beforeFile Path to text file before the change.
34+
* @param afterFile Path to text file after the change.
35+
* @param algorithm Specification of which algorithm to use for diffing with JGit.
36+
* @return A variation diff comprising the changes.
37+
* @throws IOException when JGit fails in differencing
38+
*/
39+
public static String textDiff(
40+
Path beforeFile,
41+
Path afterFile,
42+
DiffAlgorithm.SupportedAlgorithm algorithm
43+
) throws IOException {
44+
try (BufferedReader b = Files.newBufferedReader(beforeFile);
45+
BufferedReader a = Files.newBufferedReader(afterFile)
46+
) {
47+
return textDiff(IOUtils.toString(b), IOUtils.toString(a), algorithm);
48+
}
49+
}
2950

3051
/**
31-
* Creates a variation diff from to line-based text inputs.
32-
* Expects variability to be implemented via C preprocessor in those lines.
33-
* Uses JGit to diff the two files using the specified {@code options}, and afterwards, creates the variation diff.
52+
* Creates a text-based diff from two line-based text inputs.
53+
* Uses JGit to diff the two files using the specified {@code options}.
3454
* @param linesBefore State of annotated lines before the change.
3555
* @param linesAfter State of annotated lines after the change.
3656
* @param algorithm Specification of which algorithm to use for diffing with JGit.
37-
* @param options various options for parsing
3857
* @return A variation diff comprising the changes.
3958
* @throws IOException when JGit fails in differencing
40-
* @throws DiffParseException when DiffDetective fails in parsing the JGit diff to a variation diff
4159
*/
42-
public static VariationDiff<DiffLinesLabel> diff(
60+
public static String textDiff(
4361
String linesBefore,
4462
String linesAfter,
45-
DiffAlgorithm.SupportedAlgorithm algorithm,
46-
VariationDiffParseOptions options
47-
) throws IOException, DiffParseException {
63+
DiffAlgorithm.SupportedAlgorithm algorithm
64+
) throws IOException {
4865
final RawText[] text = new RawText[]{
4966
new RawText(linesBefore.getBytes()),
5067
new RawText(linesAfter.getBytes())
@@ -93,23 +110,33 @@ Using our own formatter without diff headers (paired with a maximum context (?))
93110
new BufferedReader(new StringReader(textDiff))
94111
);
95112

96-
97113
textDiff = NO_NEWLINE_AT_END_OF_FILE.matcher(textDiff).replaceAll("");
98114
//textDiff = HUNK_HEADER_REGEX.matcher(textDiff).replaceAll("");
99-
100-
final VariationDiff<DiffLinesLabel> d;
101-
try {
102-
d = VariationDiffParser.createVariationDiff(textDiff, options);
103-
} catch (DiffParseException e) {
104-
Logger.error("""
105-
Could not parse diff:
106-
107-
{}
108-
""",
109-
textDiff);
110-
throw e;
111-
}
112-
d.setSource(new PatchString(textDiff));
113-
return d;
115+
116+
return textDiff;
117+
}
118+
119+
/**
120+
* Creates a variation diff from to line-based text inputs.
121+
* Expects variability to be implemented via C preprocessor in those lines.
122+
* Uses JGit to diff the two files using the specified {@code options}, and afterwards, creates the variation diff.
123+
* Creates a variation diff from to line-based text inputs.
124+
* First creates a line-based diff with {@link #textDiff(String, String, DiffAlgorithm.SupportedAlgorithm)}
125+
* and then parses that diff with {@link VariationDiff#fromDiff(String, VariationDiffParseOptions)}.
126+
* @param linesBefore State of annotated lines before the change.
127+
* @param linesAfter State of annotated lines after the change.
128+
* @param algorithm Specification of which algorithm to use for diffing with JGit.
129+
* @param options various options for parsing
130+
* @return A variation diff comprising the changes.
131+
* @throws IOException when JGit fails in differencing
132+
* @throws DiffParseException when DiffDetective fails in parsing the JGit diff to a variation diff
133+
*/
134+
public static VariationDiff<DiffLinesLabel> diff(
135+
String linesBefore,
136+
String linesAfter,
137+
DiffAlgorithm.SupportedAlgorithm algorithm,
138+
VariationDiffParseOptions options
139+
) throws IOException, DiffParseException {
140+
return VariationDiff.fromDiff(textDiff(linesBefore, linesAfter, algorithm), options);
114141
}
115142
}

0 commit comments

Comments
 (0)