com.github.difflib.UnifiedDiffUtils Java Examples

The following examples show how to use com.github.difflib.UnifiedDiffUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Client.java    From batfish with Apache License 2.0 7 votes vote down vote up
/**
 * Computes a unified diff of the input strings, returning the empty string if the {@code
 * expected} and {@code actual} are equal.
 */
@Nonnull
@VisibleForTesting
static String getPatch(
    String expected, String actual, String expectedFileName, String actualFileName)
    throws DiffException {
  List<String> referenceLines = Arrays.asList(expected.split("\n"));
  List<String> testLines = Arrays.asList(actual.split("\n"));
  Patch<String> patch = DiffUtils.diff(referenceLines, testLines);
  if (patch.getDeltas().isEmpty()) {
    return "";
  } else {
    List<String> patchLines =
        UnifiedDiffUtils.generateUnifiedDiff(
            expectedFileName, actualFileName, referenceLines, patch, 3);
    return StringUtils.join(patchLines, "\n");
  }
}
 
Example #2
Source File: ApplyPatch.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws PatchFailedException, IOException {
    List<String> original = Files.readAllLines(new File(ORIGINAL).toPath());
    List<String> patched = Files.readAllLines(new File(PATCH).toPath());

    // At first, parse the unified diff file and get the patch
    Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patched);

    // Then apply the computed patch to the given text
    List<String> result = DiffUtils.patch(original, patch);
    System.out.println(result);
    // / Or we can call patch.applyTo(original). There is no difference.
}
 
Example #3
Source File: TextDiffSubject.java    From nomulus with Apache License 2.0 5 votes vote down vote up
static String generateUnifiedDiff(
    ImmutableList<String> expectedContent, ImmutableList<String> actualContent) {
  Patch<String> diff;
  try {
    diff = DiffUtils.diff(expectedContent, actualContent);
  } catch (DiffException e) {
    throw new RuntimeException(e);
  }
  List<String> unifiedDiff =
      UnifiedDiffUtils.generateUnifiedDiff("expected", "actual", expectedContent, diff, 0);

  return Joiner.on('\n').join(unifiedDiff);
}