|
1 | 1 | package org.variantsync.diffdetective.variation.diff; |
2 | 2 |
|
| 3 | +import org.apache.commons.io.IOUtils; |
| 4 | +import org.eclipse.jgit.diff.DiffAlgorithm; |
3 | 5 | import org.tinylog.Logger; |
4 | 6 | import org.variantsync.diffdetective.datasets.Repository; |
5 | 7 | import org.variantsync.diffdetective.diff.git.CommitDiff; |
|
12 | 14 | import org.variantsync.diffdetective.util.Assert; |
13 | 15 | import org.variantsync.diffdetective.variation.DiffLinesLabel; |
14 | 16 | import org.variantsync.diffdetective.variation.Label; |
| 17 | +import org.variantsync.diffdetective.variation.diff.construction.GumTreeDiff; |
| 18 | +import org.variantsync.diffdetective.variation.diff.construction.JGitDiff; |
15 | 19 | import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions; |
16 | 20 | import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParser; |
17 | 21 | import org.variantsync.diffdetective.variation.diff.source.PatchFile; |
@@ -145,6 +149,48 @@ public static Result<VariationDiff<DiffLinesLabel>, List<DiffError>> fromPatch(f |
145 | 149 | return Result.Failure(result.errors()); |
146 | 150 | } |
147 | 151 |
|
| 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 | + |
148 | 194 | /** |
149 | 195 | * Creates the projection of this variation diff at the given time. |
150 | 196 | * The returned value is a deep copy of the variation tree within this diff |
|
0 commit comments