Skip to content

Commit

Permalink
feat: text-based diffing method
Browse files Browse the repository at this point in the history
  • Loading branch information
pmbittner committed Jan 28, 2024
1 parent 757ccbf commit e39313e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,20 @@ public static VariationDiff<DiffLinesLabel> fromFile(final Path p, VariationDiff
* @throws DiffParseException if {@code diff} couldn't be parsed
*/
public static VariationDiff<DiffLinesLabel> fromDiff(final String diff, final VariationDiffParseOptions parseOptions) throws DiffParseException {
final VariationDiff<DiffLinesLabel> tree = VariationDiffParser.createVariationDiff(diff, parseOptions);
tree.setSource(new PatchString(diff));
return tree;
final VariationDiff<DiffLinesLabel> d;
try {
d = VariationDiffParser.createVariationDiff(diff, parseOptions);
} catch (DiffParseException e) {
Logger.error("""
Could not parse diff:
{}
""",
diff);
throw e;
}
d.setSource(new PatchString(diff));
return d;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package org.variantsync.diffdetective.variation.diff.construction;

import org.apache.commons.io.IOUtils;
import org.eclipse.jgit.diff.*;
import org.tinylog.Logger;
import org.variantsync.diffdetective.diff.git.GitDiffer;
import org.variantsync.diffdetective.diff.result.DiffParseException;
import org.variantsync.diffdetective.variation.DiffLinesLabel;
import org.variantsync.diffdetective.variation.diff.Time;
import org.variantsync.diffdetective.variation.diff.VariationDiff;
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions;
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParser;
import org.variantsync.diffdetective.variation.diff.source.PatchString;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Pattern;

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

private JGitDiff() {}

/**
* Creates a text-based diff from two line-based text inputs.
* Uses JGit to diff the two files using the specified {@code options}.
* @param beforeFile Path to text file before the change.
* @param afterFile Path to text file after the change.
* @param algorithm Specification of which algorithm to use for diffing with JGit.
* @return A variation diff comprising the changes.
* @throws IOException when JGit fails in differencing
*/
public static String textDiff(
Path beforeFile,
Path afterFile,
DiffAlgorithm.SupportedAlgorithm algorithm
) throws IOException {
try (BufferedReader b = Files.newBufferedReader(beforeFile);
BufferedReader a = Files.newBufferedReader(afterFile)
) {
return textDiff(IOUtils.toString(b), IOUtils.toString(a), algorithm);
}
}

/**
* Creates a variation diff from to line-based text inputs.
* Expects variability to be implemented via C preprocessor in those lines.
* Uses JGit to diff the two files using the specified {@code options}, and afterwards, creates the variation diff.
* Creates a text-based diff from two line-based text inputs.
* Uses JGit to diff the two files using the specified {@code options}.
* @param linesBefore State of annotated lines before the change.
* @param linesAfter State of annotated lines after the change.
* @param algorithm Specification of which algorithm to use for diffing with JGit.
* @param options various options for parsing
* @return A variation diff comprising the changes.
* @throws IOException when JGit fails in differencing
* @throws DiffParseException when DiffDetective fails in parsing the JGit diff to a variation diff
*/
public static VariationDiff<DiffLinesLabel> diff(
public static String textDiff(
String linesBefore,
String linesAfter,
DiffAlgorithm.SupportedAlgorithm algorithm,
VariationDiffParseOptions options
) throws IOException, DiffParseException {
DiffAlgorithm.SupportedAlgorithm algorithm
) throws IOException {
final RawText[] text = new RawText[]{
new RawText(linesBefore.getBytes()),
new RawText(linesAfter.getBytes())
Expand Down Expand Up @@ -93,23 +110,33 @@ Using our own formatter without diff headers (paired with a maximum context (?))
new BufferedReader(new StringReader(textDiff))
);


textDiff = NO_NEWLINE_AT_END_OF_FILE.matcher(textDiff).replaceAll("");
//textDiff = HUNK_HEADER_REGEX.matcher(textDiff).replaceAll("");

final VariationDiff<DiffLinesLabel> d;
try {
d = VariationDiffParser.createVariationDiff(textDiff, options);
} catch (DiffParseException e) {
Logger.error("""
Could not parse diff:
{}
""",
textDiff);
throw e;
}
d.setSource(new PatchString(textDiff));
return d;

return textDiff;
}

/**
* Creates a variation diff from to line-based text inputs.
* Expects variability to be implemented via C preprocessor in those lines.
* Uses JGit to diff the two files using the specified {@code options}, and afterwards, creates the variation diff.
* Creates a variation diff from to line-based text inputs.
* First creates a line-based diff with {@link #textDiff(String, String, DiffAlgorithm.SupportedAlgorithm)}
* and then parses that diff with {@link VariationDiff#fromDiff(String, VariationDiffParseOptions)}.
* @param linesBefore State of annotated lines before the change.
* @param linesAfter State of annotated lines after the change.
* @param algorithm Specification of which algorithm to use for diffing with JGit.
* @param options various options for parsing
* @return A variation diff comprising the changes.
* @throws IOException when JGit fails in differencing
* @throws DiffParseException when DiffDetective fails in parsing the JGit diff to a variation diff
*/
public static VariationDiff<DiffLinesLabel> diff(
String linesBefore,
String linesAfter,
DiffAlgorithm.SupportedAlgorithm algorithm,
VariationDiffParseOptions options
) throws IOException, DiffParseException {
return VariationDiff.fromDiff(textDiff(linesBefore, linesAfter, algorithm), options);
}
}

0 comments on commit e39313e

Please sign in to comment.