11package org .variantsync .diffdetective .variation .diff .construction ;
22
3+ import org .apache .commons .io .IOUtils ;
34import org .eclipse .jgit .diff .*;
4- import org .tinylog .Logger ;
55import org .variantsync .diffdetective .diff .git .GitDiffer ;
66import org .variantsync .diffdetective .diff .result .DiffParseException ;
77import org .variantsync .diffdetective .variation .DiffLinesLabel ;
88import org .variantsync .diffdetective .variation .diff .Time ;
99import org .variantsync .diffdetective .variation .diff .VariationDiff ;
1010import 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
1412import java .io .BufferedReader ;
1513import java .io .ByteArrayOutputStream ;
1614import java .io .IOException ;
1715import java .io .StringReader ;
1816import java .nio .charset .StandardCharsets ;
17+ import java .nio .file .Files ;
18+ import java .nio .file .Path ;
1919import 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