Search in sources :

Example 86 with Edit

use of org.eclipse.jgit.diff.Edit in project gerrit by GerritCodeReview.

the class DiffContentCalculator method calculateDiffContent.

/**
 * Gather information necessary to display line-by-line difference between 2 texts.
 *
 * <p>The method returns instance of {@link DiffCalculatorResult} with the following data:
 *
 * <ul>
 *   <li>All changed lines
 *   <li>Additional lines to be displayed above and below the changed lines
 *   <li>All changed and unchanged lines with comments
 *   <li>Additional lines to be displayed above and below lines with commentsEdits with special
 *       "fake" edits for unchanged lines with comments
 * </ul>
 *
 * <p>More details can be found in {@link DiffCalculatorResult}.
 *
 * @param srcA Original text content
 * @param srcB New text content
 * @param edits List of edits which was applied to srcA to produce srcB
 * @return an instance of {@link DiffCalculatorResult}.
 */
DiffCalculatorResult calculateDiffContent(TextSource srcA, TextSource srcB, ImmutableList<Edit> edits) {
    if (srcA.src == srcB.src && edits.isEmpty()) {
        // Odd special case; the files are identical (100% rename or copy)
        // and the user has asked for context that is larger than the file.
        // Send them the entire file, with an empty edit after the last line.
        // 
        SparseFileContentBuilder diffA = new SparseFileContentBuilder(srcA.size());
        for (int i = 0; i < srcA.size(); i++) {
            srcA.copyLineTo(diffA, i);
        }
        DiffContent diffContent = new DiffContent(diffA.build(), SparseFileContent.create(ImmutableList.of(), srcB.size()));
        Edit emptyEdit = new Edit(srcA.size(), srcA.size());
        return new DiffCalculatorResult(diffContent, ImmutableList.of(emptyEdit));
    }
    ImmutableList<Edit> sortedEdits = correctForDifferencesInNewlineAtEnd(srcA, srcB, edits);
    DiffContent diffContent = packContent(srcA, srcB, diffPrefs.ignoreWhitespace != Whitespace.IGNORE_NONE, sortedEdits);
    return new DiffCalculatorResult(diffContent, sortedEdits);
}
Also used : SparseFileContentBuilder(com.google.gerrit.prettify.common.SparseFileContentBuilder) Edit(org.eclipse.jgit.diff.Edit) ReplaceEdit(com.google.gerrit.jgit.diff.ReplaceEdit)

Example 87 with Edit

use of org.eclipse.jgit.diff.Edit in project gerrit by GerritCodeReview.

the class IntraLineDiff method readObject.

private void readObject(ObjectInputStream in) throws IOException {
    status = readEnum(in, Status.values());
    int editCount = readVarInt32(in);
    Edit[] editArray = new Edit[editCount];
    for (int i = 0; i < editCount; i++) {
        editArray[i] = readEdit(in);
        int innerCount = readVarInt32(in);
        if (0 < innerCount) {
            Edit[] inner = new Edit[innerCount];
            for (int j = 0; j < innerCount; j++) {
                inner[j] = readEdit(in);
            }
            editArray[i] = new ReplaceEdit(editArray[i], asList(inner));
        }
    }
    edits = ImmutableList.copyOf(editArray);
}
Also used : ReplaceEdit(com.google.gerrit.jgit.diff.ReplaceEdit) Edit(org.eclipse.jgit.diff.Edit) ReplaceEdit(com.google.gerrit.jgit.diff.ReplaceEdit)

Example 88 with Edit

use of org.eclipse.jgit.diff.Edit in project gerrit by GerritCodeReview.

the class IntraLineLoader method applyEditsToString.

/**
 * Apply edits to the {@code target} string. Replace edits are applied to target and replaced with
 * a substring from {@code from}. Delete edits are applied to target. Insert edits are removed
 * from target.
 *
 * @return Optional containing the transformed string, or empty if the transformation fails (due
 *     to index out of bounds).
 */
private static Optional<String> applyEditsToString(StringBuilder target, String from, List<Edit> edits) {
    // Process edits right to left to avoid re-computation of indices when characters are removed.
    try {
        for (int i = edits.size() - 1; i >= 0; i--) {
            Edit edit = edits.get(i);
            if (edit.getType() == Edit.Type.REPLACE) {
                boundaryCheck(target, edit.getBeginA(), edit.getEndA() - 1);
                boundaryCheck(from, edit.getBeginB(), edit.getEndB() - 1);
                target.replace(edit.getBeginA(), edit.getEndA(), from.substring(edit.getBeginB(), edit.getEndB()));
            } else if (edit.getType() == Edit.Type.DELETE) {
                boundaryCheck(target, edit.getBeginA(), edit.getEndA() - 1);
                target.delete(edit.getBeginA(), edit.getEndA());
            } else if (edit.getType() == Edit.Type.INSERT) {
                boundaryCheck(target, edit.getBeginB(), edit.getEndB() - 1);
                target.delete(edit.getBeginB(), edit.getEndB());
            }
        }
        return Optional.of(target.toString());
    } catch (StringIndexOutOfBoundsException unused) {
        return Optional.empty();
    }
}
Also used : Edit(org.eclipse.jgit.diff.Edit) ReplaceEdit(com.google.gerrit.jgit.diff.ReplaceEdit)

Example 89 with Edit

use of org.eclipse.jgit.diff.Edit in project gerrit by GerritCodeReview.

the class IntraLineLoader method combineLineEdits.

private static void combineLineEdits(List<Edit> edits, ImmutableSet<Edit> editsDueToRebase, Text a, Text b) {
    for (int j = 0; j < edits.size() - 1; ) {
        Edit c = edits.get(j);
        Edit n = edits.get(j + 1);
        if (editsDueToRebase.contains(c) || editsDueToRebase.contains(n)) {
            // Don't combine any edits which were identified as being introduced by a rebase as we would
            // lose that information because of the combination.
            j++;
            continue;
        }
        // Combine edits that are really close together. Right now our rule
        // is, coalesce two line edits which are only one line apart if that
        // common context line is either a "pointless line", or is identical
        // on both sides and starts a new block of code. These are mostly
        // block reindents to add or remove control flow operators.
        // 
        final int ad = n.getBeginA() - c.getEndA();
        final int bd = n.getBeginB() - c.getEndB();
        if ((1 <= ad && isBlankLineGap(a, c.getEndA(), n.getBeginA())) || (1 <= bd && isBlankLineGap(b, c.getEndB(), n.getBeginB())) || (ad == 1 && bd == 1 && isControlBlockStart(a, c.getEndA()))) {
            int ab = c.getBeginA();
            int ae = n.getEndA();
            int bb = c.getBeginB();
            int be = n.getEndB();
            edits.set(j, new Edit(ab, ae, bb, be));
            edits.remove(j + 1);
            continue;
        }
        j++;
    }
}
Also used : Edit(org.eclipse.jgit.diff.Edit) ReplaceEdit(com.google.gerrit.jgit.diff.ReplaceEdit)

Example 90 with Edit

use of org.eclipse.jgit.diff.Edit in project gerrit by GerritCodeReview.

the class PatchListEntry method readEditArray.

private static Edit[] readEditArray(InputStream in) throws IOException {
    int numEdits = readVarInt32(in);
    Edit[] edits = new Edit[numEdits];
    for (int i = 0; i < numEdits; i++) {
        int beginA = readVarInt32(in);
        int endA = readVarInt32(in);
        int beginB = readVarInt32(in);
        int endB = readVarInt32(in);
        edits[i] = new Edit(beginA, endA, beginB, endB);
    }
    return edits;
}
Also used : Edit(org.eclipse.jgit.diff.Edit)

Aggregations

Edit (org.eclipse.jgit.diff.Edit)142 Test (org.junit.Test)117 FixResult (com.google.gerrit.server.fixes.FixCalculator.FixResult)112 ReplaceEdit (com.google.gerrit.jgit.diff.ReplaceEdit)9 ArrayList (java.util.ArrayList)5 RawText (org.eclipse.jgit.diff.RawText)5 Range (com.google.gerrit.entities.Comment.Range)4 FixReplacement (com.google.gerrit.entities.FixReplacement)4 Text (com.google.gerrit.server.patch.Text)4 EditList (org.eclipse.jgit.diff.EditList)4 PatchScript (com.google.gerrit.common.data.PatchScript)3 TaggedEdit (com.google.gerrit.server.patch.filediff.TaggedEdit)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)3 ReplaceEdit (org.eclipse.jgit.diff.ReplaceEdit)3 AbbreviatedObjectId (org.eclipse.jgit.lib.AbbreviatedObjectId)3 ObjectReader (org.eclipse.jgit.lib.ObjectReader)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 DiffPreferencesInfo (com.google.gerrit.extensions.client.DiffPreferencesInfo)2 SparseFileContent (com.google.gerrit.prettify.common.SparseFileContent)2