Search in sources :

Example 1 with TextDiff

use of com.google.startupos.common.Protos.TextDiff in project startup-os by google.

the class TextDifferencer method getTextDiff.

public TextDiff getTextDiff(String leftText, String rightText, String diffString) {
    TextDiff.Builder result = TextDiff.newBuilder().setLeftFileContents(leftText).setRightFileContents(rightText);
    if (diffString.isEmpty()) {
        return result.build();
    }
    String[] diffLines = diffString.split("\n");
    List<DiffLine> leftLines = new ArrayList();
    List<DiffLine> rightLines = new ArrayList();
    int leftCodeLineNumber = getLeftStartLine(diffLines[0]);
    int leftDiffLineNumber = leftCodeLineNumber;
    int rightCodeLineNumber = getRightStartLine(diffLines[0]);
    int rightDiffLineNumber = rightCodeLineNumber;
    for (int diffIndex = 1; diffIndex < diffLines.length; diffIndex++) {
        if (diffLines[diffIndex].charAt(0) == '\\') {
            // This is not a real code line, probably "\ No newline at end of file".
            continue;
        }
        ChangeType type = CHANGE_TYPE_MAP.get(diffLines[diffIndex].charAt(0));
        if (type == null) {
            throw new IllegalStateException("Diff line " + diffIndex + " does not start with a diff character (+- ):\n" + diffLines[diffIndex] + "\nFor diffString:\n" + diffString);
        }
        if (type == ChangeType.NO_CHANGE) {
            // On NO_CHANGE lines, we know that both diff line indices should be the same. We add
            // placeholder DiffLines to fill in the gaps:
            fillPlaceholderGap(leftLines, rightLines, leftDiffLineNumber, rightDiffLineNumber);
            // Now gap is filled, so set both line numbers to the maximum:
            leftDiffLineNumber = Math.max(leftDiffLineNumber, rightDiffLineNumber);
            rightDiffLineNumber = leftDiffLineNumber;
            // On to the next line:
            leftCodeLineNumber++;
            rightCodeLineNumber++;
            leftDiffLineNumber++;
            rightDiffLineNumber++;
            continue;
        }
        String text = diffLines[diffIndex].substring(1);
        int codeLineNumber = type == ChangeType.DELETE ? leftCodeLineNumber : rightCodeLineNumber;
        int diffLineNumber = type == ChangeType.DELETE ? leftDiffLineNumber : rightDiffLineNumber;
        DiffLine diffLine = DiffLine.newBuilder().setText(text).setType(type).setCodeLineNumber(codeLineNumber).setDiffLineNumber(diffLineNumber).build();
        if (type == ChangeType.DELETE) {
            leftLines.add(diffLine);
            leftCodeLineNumber++;
            leftDiffLineNumber++;
        } else {
            rightLines.add(diffLine);
            rightCodeLineNumber++;
            rightDiffLineNumber++;
        }
    }
    // Fill any last section:
    fillPlaceholderGap(leftLines, rightLines, leftDiffLineNumber, rightDiffLineNumber);
    addWordChanges(leftLines, rightLines);
    result.addAllLeftDiffLine(leftLines);
    result.addAllRightDiffLine(rightLines);
    TextDiff textDiff = result.build();
    return textDiff;
}
Also used : TextDiff(com.google.startupos.common.Protos.TextDiff) ChangeType(com.google.startupos.common.Protos.ChangeType) DiffLine(com.google.startupos.common.Protos.DiffLine) ArrayList(java.util.ArrayList)

Example 2 with TextDiff

use of com.google.startupos.common.Protos.TextDiff in project startup-os by google.

the class TextDifferencerTest method testMixedChangesAtTheBeginning.

@Test
public void testMixedChangesAtTheBeginning() {
    String leftContents = "No Change.";
    String rightContents = "With Change.";
    String diff = "@@ -1 +1 @@\n-No Change.\n+With Change.";
    TextDiff expectedTextDiff = readTextDiff("MixedChangesAtTheBeginning_diff_prototxt.txt");
    assertEquals(expectedTextDiff, differencer.getTextDiff(leftContents, rightContents, diff));
}
Also used : TextDiff(com.google.startupos.common.Protos.TextDiff) Test(org.junit.Test)

Example 3 with TextDiff

use of com.google.startupos.common.Protos.TextDiff in project startup-os by google.

the class TextDifferencerTest method testOnlyAdditions.

@Test
public void testOnlyAdditions() {
    String leftContents = "";
    String rightContents = "Addition.";
    String diff = "@@ -0,0 +1 @@\n+Addition.";
    TextDiff expectedTextDiff = TextDiff.newBuilder().addRightDiffLine(DiffLine.newBuilder().setText(rightContents).setType(ChangeType.ADD).build()).addLeftDiffLine(DiffLine.newBuilder().setType(ChangeType.LINE_PLACEHOLDER).build()).setLeftFileContents(leftContents).setRightFileContents(rightContents).build();
    assertEquals(expectedTextDiff, differencer.getTextDiff(leftContents, rightContents, diff));
}
Also used : TextDiff(com.google.startupos.common.Protos.TextDiff) Test(org.junit.Test)

Example 4 with TextDiff

use of com.google.startupos.common.Protos.TextDiff in project startup-os by google.

the class TextDifferencerTest method testBuildFileChange.

@Test
public void testBuildFileChange() {
    String leftContents = readFile("BUILD_before.txt");
    String rightContents = readFile("BUILD_after.txt");
    // To regenerate BUILD_diff.txt, run:
    // git diff --no-index common/tests/resources/BUILD_before.txt
    // common/tests/resources/BUILD_after.txt | tail -n +5
    String diffString = readFile("BUILD_diff.txt");
    TextDiff expectedTextDiff = readTextDiff("BUILD_diff_prototxt.txt");
    assertEquals(expectedTextDiff, differencer.getTextDiff(leftContents, rightContents, diffString));
}
Also used : TextDiff(com.google.startupos.common.Protos.TextDiff) Test(org.junit.Test)

Example 5 with TextDiff

use of com.google.startupos.common.Protos.TextDiff in project startup-os by google.

the class TextDifferencerTest method testLicenseFileChange.

@Test
public void testLicenseFileChange() {
    String leftContents = readFile("License_before.txt");
    String rightContents = readFile("License_after.txt");
    String diffString = readFile("License_diff.txt");
    TextDiff expectedTextDiff = readTextDiff("License_diff_prototxt.txt");
    assertEquals(expectedTextDiff, differencer.getTextDiff(leftContents, rightContents, diffString));
}
Also used : TextDiff(com.google.startupos.common.Protos.TextDiff) Test(org.junit.Test)

Aggregations

TextDiff (com.google.startupos.common.Protos.TextDiff)11 Test (org.junit.Test)9 ChangeType (com.google.startupos.common.Protos.ChangeType)1 DiffLine (com.google.startupos.common.Protos.DiffLine)1 ArrayList (java.util.ArrayList)1