Skip to content

Commit f1cfa06

Browse files
committed
factory methods in VariationDiff for GumTree and JGit
1 parent c4e6e28 commit f1cfa06

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.variantsync.diffdetective.variation.diff;
22

3+
import org.apache.commons.io.IOUtils;
4+
import org.eclipse.jgit.diff.DiffAlgorithm;
35
import org.tinylog.Logger;
46
import org.variantsync.diffdetective.datasets.Repository;
57
import org.variantsync.diffdetective.diff.git.CommitDiff;
@@ -12,6 +14,8 @@
1214
import org.variantsync.diffdetective.util.Assert;
1315
import org.variantsync.diffdetective.variation.DiffLinesLabel;
1416
import org.variantsync.diffdetective.variation.Label;
17+
import org.variantsync.diffdetective.variation.diff.construction.GumTreeDiff;
18+
import org.variantsync.diffdetective.variation.diff.construction.JGitDiff;
1519
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions;
1620
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParser;
1721
import org.variantsync.diffdetective.variation.diff.source.PatchFile;
@@ -145,6 +149,48 @@ public static Result<VariationDiff<DiffLinesLabel>, List<DiffError>> fromPatch(f
145149
return Result.Failure(result.errors());
146150
}
147151

152+
/**
153+
* Create a VariationDiff from two given text files.
154+
* @see #fromLines(String, String, DiffAlgorithm.SupportedAlgorithm, VariationDiffParseOptions)
155+
*/
156+
public static VariationDiff<DiffLinesLabel> fromFiles(
157+
final Path beforeFile,
158+
final Path afterFile,
159+
DiffAlgorithm.SupportedAlgorithm algorithm,
160+
VariationDiffParseOptions options)
161+
throws IOException, DiffParseException
162+
{
163+
try (BufferedReader b = Files.newBufferedReader(beforeFile);
164+
BufferedReader a = Files.newBufferedReader(afterFile)
165+
) {
166+
return fromLines(IOUtils.toString(b), IOUtils.toString(a), algorithm, options);
167+
}
168+
}
169+
170+
/**
171+
* Creates a variation diff from to line-based text inputs.
172+
* This method just forwards to:
173+
* @see JGitDiff#diff(String, String, DiffAlgorithm.SupportedAlgorithm, VariationDiffParseOptions)
174+
*/
175+
public static VariationDiff<DiffLinesLabel> fromLines(
176+
String before,
177+
String after,
178+
DiffAlgorithm.SupportedAlgorithm algorithm,
179+
VariationDiffParseOptions options) throws IOException, DiffParseException
180+
{
181+
return JGitDiff.diff(before, after, algorithm, options);
182+
}
183+
184+
/**
185+
* Create a {@link VariationDiff} by matching nodes between {@code before} and {@code after} with the
186+
* default GumTree matcher.
187+
*
188+
* @see GumTreeDiff#diffUsingMatching(VariationTree, VariationTree)
189+
*/
190+
public static <L extends Label> VariationDiff<L> fromTrees(VariationTree<L> before, VariationTree<L> after) {
191+
return GumTreeDiff.diffUsingMatching(before, after);
192+
}
193+
148194
/**
149195
* Creates the projection of this variation diff at the given time.
150196
* The returned value is a deep copy of the variation tree within this diff

0 commit comments

Comments
 (0)