Java Code Examples for com.github.difflib.UnifiedDiffUtils

The following examples show how to use com.github.difflib.UnifiedDiffUtils. These examples are extracted from open source projects. 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 Project: batfish   Source File: Client.java    License: Apache License 2.0 6 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 Project: java-diff-utils   Source File: ApplyPatch.java    License: 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 Project: nomulus   Source File: TextDiffSubject.java    License: 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);
}