Example usage for org.eclipse.jgit.diff Edit Edit

List of usage examples for org.eclipse.jgit.diff Edit Edit

Introduction

In this page you can find the example usage for org.eclipse.jgit.diff Edit Edit.

Prototype

public Edit(int as, int bs) 

Source Link

Document

Create a new empty edit.

Usage

From source file:com.google.gerrit.httpd.rpc.patch.PatchScriptBuilder.java

License:Apache License

private PatchScript build(final PatchListEntry content, final CommentDetail comments, final List<Patch> history)
        throws IOException {
    boolean intralineDifferenceIsPossible = true;
    boolean intralineFailure = false;

    a.path = oldName(content);//ww w .  j  av  a 2  s  .  co m
    b.path = newName(content);

    a.resolve(null, aId);
    b.resolve(a, bId);

    edits = new ArrayList<Edit>(content.getEdits());

    if (!isModify(content)) {
        intralineDifferenceIsPossible = false;
    } else if (diffPrefs.isIntralineDifference()) {
        IntraLineDiff d = patchListCache.getIntraLineDiff(
                new IntraLineDiffKey(a.id, a.src, b.id, b.src, edits, projectKey, bId, b.path));
        if (d != null) {
            switch (d.getStatus()) {
            case EDIT_LIST:
                edits = new ArrayList<Edit>(d.getEdits());
                break;

            case DISABLED:
                intralineDifferenceIsPossible = false;
                break;

            case ERROR:
            case TIMEOUT:
                intralineDifferenceIsPossible = false;
                intralineFailure = true;
                break;
            }
        } else {
            intralineDifferenceIsPossible = false;
            intralineFailure = true;
        }
    }

    ensureCommentsVisible(comments);

    boolean hugeFile = false;
    if (a.mode == FileMode.GITLINK || b.mode == FileMode.GITLINK) {

    } else if (a.src == b.src && a.size() <= context && content.getEdits().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.
        //
        for (int i = 0; i < a.size(); i++) {
            a.addLine(i);
        }
        edits = new ArrayList<Edit>(1);
        edits.add(new Edit(a.size(), a.size()));

    } else {
        if (BIG_FILE < Math.max(a.size(), b.size())) {
            // IF the file is really large, we disable things to avoid choking
            // the browser client.
            //
            diffPrefs.setContext((short) Math.min(25, context));
            diffPrefs.setSyntaxHighlighting(false);
            context = diffPrefs.getContext();
            hugeFile = true;

        } else if (diffPrefs.isSyntaxHighlighting()) {
            // In order to syntax highlight the file properly we need to
            // give the client the complete file contents. So force our
            // context temporarily to the complete file size.
            //
            context = MAX_CONTEXT;
        }
        packContent(diffPrefs.getIgnoreWhitespace() != Whitespace.IGNORE_NONE);
    }

    return new PatchScript(change.getKey(), content.getChangeType(), content.getOldName(), content.getNewName(),
            a.fileMode, b.fileMode, content.getHeaderLines(), diffPrefs, a.dst, b.dst, edits, a.displayMethod,
            b.displayMethod, comments, history, hugeFile, intralineDifferenceIsPossible, intralineFailure);
}

From source file:com.google.gerrit.httpd.rpc.patch.PatchScriptBuilder.java

License:Apache License

private void ensureCommentsVisible(final CommentDetail comments) {
    if (comments.getCommentsA().isEmpty() && comments.getCommentsB().isEmpty()) {
        // No comments, no additional dummy edits are required.
        ////from  w w w  .java  2 s  . c o m
        return;
    }

    // Construct empty Edit blocks around each location where a comment is.
    // This will force the later packContent method to include the regions
    // containing comments, potentially combining those regions together if
    // they have overlapping contexts. UI renders will also be able to make
    // correct hunks from this, but because the Edit is empty they will not
    // style it specially.
    //
    final List<Edit> empty = new ArrayList<Edit>();
    int lastLine;

    lastLine = -1;
    for (PatchLineComment plc : comments.getCommentsA()) {
        final int a = plc.getLine();
        if (lastLine != a) {
            final int b = mapA2B(a - 1);
            if (0 <= b) {
                safeAdd(empty, new Edit(a - 1, b));
            }
            lastLine = a;
        }
    }

    lastLine = -1;
    for (PatchLineComment plc : comments.getCommentsB()) {
        final int b = plc.getLine();
        if (lastLine != b) {
            final int a = mapB2A(b - 1);
            if (0 <= a) {
                safeAdd(empty, new Edit(a, b - 1));
            }
            lastLine = b;
        }
    }

    // Sort the final list by the index in A, so packContent can combine
    // them correctly later.
    //
    edits.addAll(empty);
    Collections.sort(edits, EDIT_SORT);
}

From source file:com.google.gerrit.prettify.client.PrettyFormatter.java

License:Apache License

private SafeHtml colorLineEdits(SparseFileContent src) {
    // Make a copy of the edits with a sentinel that is after all lines
    // in the source. That simplifies our loop below because we'll never
    // run off the end of the edit list.
    ////from  w ww . j a  va2  s  . c om
    List<Edit> edits = new ArrayList<>(this.edits.size() + 1);
    edits.addAll(this.edits);
    edits.add(new Edit(src.size(), src.size()));

    SafeHtmlBuilder buf = new SafeHtmlBuilder();

    int curIdx = 0;
    Edit curEdit = edits.get(curIdx);

    ReplaceEdit lastReplace = null;
    List<Edit> charEdits = null;
    int lastPos = 0;
    int lastIdx = 0;

    for (int index = src.first(); index < src.size(); index = src.next(index)) {
        int cmp = compare(index, curEdit);
        while (0 < cmp) {
            // The index is after the edit. Skip to the next edit.
            //
            curEdit = edits.get(curIdx++);
            cmp = compare(index, curEdit);
        }

        if (cmp < 0) {
            // index occurs before the edit. This is a line of context.
            //
            appendShowBareCR(buf, src.get(index), true);
            buf.append('\n');
            continue;
        }

        // index occurs within the edit. The line is a modification.
        //
        if (curEdit instanceof ReplaceEdit) {
            if (lastReplace != curEdit) {
                lastReplace = (ReplaceEdit) curEdit;
                charEdits = lastReplace.getInternalEdits();
                lastPos = 0;
                lastIdx = 0;
            }

            String line = src.get(index) + "\n";
            for (int c = 0; c < line.length();) {
                if (charEdits == null || (charEdits.size() <= lastIdx)) {
                    appendShowBareCR(buf, line.substring(c), false);
                    break;
                }

                final Edit edit = charEdits.get(lastIdx);
                final int b = side.getBegin(edit) - lastPos;
                final int e = side.getEnd(edit) - lastPos;

                if (c < b) {
                    // There is text at the start of this line that is common
                    // with the other side. Copy it with no style around it.
                    //
                    final int cmnLen = Math.min(b, line.length());
                    buf.openSpan();
                    buf.setStyleName("wdc");
                    appendShowBareCR(buf, line.substring(c, cmnLen), //
                            cmnLen == line.length() - 1);
                    buf.closeSpan();
                    c = cmnLen;
                }

                final int modLen = Math.min(e, line.length());
                if (c < e && c < modLen) {
                    buf.openSpan();
                    buf.setStyleName(side.getStyleName());
                    appendShowBareCR(buf, line.substring(c, modLen), //
                            modLen == line.length() - 1);
                    buf.closeSpan();
                    if (modLen == line.length()) {
                        trailingEdits.add(index);
                    }
                    c = modLen;
                }

                if (e <= c) {
                    lastIdx++;
                }
            }
            lastPos += line.length();

        } else {
            appendShowBareCR(buf, src.get(index), true);
            buf.append('\n');
        }
    }
    return buf;
}

From source file:com.google.gerrit.prettify.common.PrettyFormatter.java

License:Apache License

private SafeHtml colorLineEdits(SparseFileContent src) {
    // Make a copy of the edits with a sentinel that is after all lines
    // in the source. That simplifies our loop below because we'll never
    // run off the end of the edit list.
    ////from   www .j a v  a2s .c om
    List<Edit> edits = new ArrayList<Edit>(this.edits.size() + 1);
    edits.addAll(this.edits);
    edits.add(new Edit(src.size(), src.size()));

    SafeHtmlBuilder buf = new SafeHtmlBuilder();

    int curIdx = 0;
    Edit curEdit = edits.get(curIdx);

    ReplaceEdit lastReplace = null;
    List<Edit> charEdits = null;
    int lastPos = 0;
    int lastIdx = 0;

    for (int index = src.first(); index < src.size(); index = src.next(index)) {
        int cmp = compare(index, curEdit);
        while (0 < cmp) {
            // The index is after the edit. Skip to the next edit.
            //
            curEdit = edits.get(curIdx++);
            cmp = compare(index, curEdit);
        }

        if (cmp < 0) {
            // index occurs before the edit. This is a line of context.
            //
            appendShowBareCR(buf, src.get(index), true);
            buf.append('\n');
            continue;
        }

        // index occurs within the edit. The line is a modification.
        //
        if (curEdit instanceof ReplaceEdit) {
            if (lastReplace != curEdit) {
                lastReplace = (ReplaceEdit) curEdit;
                charEdits = lastReplace.getInternalEdits();
                lastPos = 0;
                lastIdx = 0;
            }

            String line = src.get(index) + "\n";
            for (int c = 0; c < line.length();) {
                if (charEdits.size() <= lastIdx) {
                    appendShowBareCR(buf, line.substring(c), false);
                    break;
                }

                final Edit edit = charEdits.get(lastIdx);
                final int b = side.getBegin(edit) - lastPos;
                final int e = side.getEnd(edit) - lastPos;

                if (c < b) {
                    // There is text at the start of this line that is common
                    // with the other side. Copy it with no style around it.
                    //
                    final int cmnLen = Math.min(b, line.length());
                    buf.openSpan();
                    buf.setStyleName("wdc");
                    appendShowBareCR(buf, line.substring(c, cmnLen), //
                            cmnLen == line.length() - 1);
                    buf.closeSpan();
                    c = cmnLen;
                }

                final int modLen = Math.min(e, line.length());
                if (c < e && c < modLen) {
                    buf.openSpan();
                    buf.setStyleName(side.getStyleName());
                    appendShowBareCR(buf, line.substring(c, modLen), //
                            modLen == line.length() - 1);
                    buf.closeSpan();
                    if (modLen == line.length()) {
                        trailingEdits.add(index);
                    }
                    c = modLen;
                }

                if (e <= c) {
                    lastIdx++;
                }
            }
            lastPos += line.length();

        } else {
            appendShowBareCR(buf, src.get(index), true);
            buf.append('\n');
        }
    }
    return buf;
}

From source file:com.google.gerrit.server.patch.PatchScriptBuilder.java

License:Apache License

private PatchScript build(final PatchListEntry content, final CommentDetail comments, final List<Patch> history)
        throws IOException {
    boolean intralineDifferenceIsPossible = true;
    boolean intralineFailure = false;
    boolean intralineTimeout = false;

    a.path = oldName(content);/* w  w  w  .jav  a2  s .  co m*/
    b.path = newName(content);

    a.resolve(null, aId);
    b.resolve(a, bId);

    edits = new ArrayList<>(content.getEdits());

    if (!isModify(content)) {
        intralineDifferenceIsPossible = false;
    } else if (diffPrefs.isIntralineDifference()) {
        IntraLineDiff d = patchListCache.getIntraLineDiff(
                new IntraLineDiffKey(a.id, b.id, diffPrefs.getIgnoreWhitespace() != Whitespace.IGNORE_NONE),
                IntraLineDiffArgs.create(a.src, b.src, edits, projectKey, bId, b.path));
        if (d != null) {
            switch (d.getStatus()) {
            case EDIT_LIST:
                edits = new ArrayList<>(d.getEdits());
                break;

            case DISABLED:
                intralineDifferenceIsPossible = false;
                break;

            case ERROR:
                intralineDifferenceIsPossible = false;
                intralineFailure = true;
                break;

            case TIMEOUT:
                intralineDifferenceIsPossible = false;
                intralineTimeout = true;
                break;
            }
        } else {
            intralineDifferenceIsPossible = false;
            intralineFailure = true;
        }
    }

    if (comments != null) {
        ensureCommentsVisible(comments);
    }

    boolean hugeFile = false;
    if (a.mode == FileMode.GITLINK || b.mode == FileMode.GITLINK) {
        // Do nothing
    } else if (a.src == b.src && a.size() <= context && content.getEdits().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.
        //
        for (int i = 0; i < a.size(); i++) {
            a.addLine(i);
        }
        edits = new ArrayList<>(1);
        edits.add(new Edit(a.size(), a.size()));

    } else {
        if (BIG_FILE < Math.max(a.size(), b.size())) {
            // IF the file is really large, we disable things to avoid choking
            // the browser client.
            //
            hugeFile = true;

        }

        // In order to expand the skipped common lines or syntax highlight the
        // file properly we need to give the client the complete file contents.
        // So force our context temporarily to the complete file size.
        //
        context = MAX_CONTEXT;

        packContent(diffPrefs.getIgnoreWhitespace() != Whitespace.IGNORE_NONE);
    }

    return new PatchScript(change.getKey(), content.getChangeType(), content.getOldName(), content.getNewName(),
            a.fileMode, b.fileMode, content.getHeaderLines(), diffPrefs, a.dst, b.dst, edits, a.displayMethod,
            b.displayMethod, a.mimeType.toString(), b.mimeType.toString(), comments, history, hugeFile,
            intralineDifferenceIsPossible, intralineFailure, intralineTimeout,
            content.getPatchType() == Patch.PatchType.BINARY, aId == null ? null : aId.getName(),
            bId == null ? null : bId.getName());
}

From source file:com.google.gerrit.server.patch.PatchScriptBuilder.java

License:Apache License

private void ensureCommentsVisible(final CommentDetail comments) {
    if (comments.getCommentsA().isEmpty() && comments.getCommentsB().isEmpty()) {
        // No comments, no additional dummy edits are required.
        ////  w ww  . j  a v a2  s.  c  o m
        return;
    }

    // Construct empty Edit blocks around each location where a comment is.
    // This will force the later packContent method to include the regions
    // containing comments, potentially combining those regions together if
    // they have overlapping contexts. UI renders will also be able to make
    // correct hunks from this, but because the Edit is empty they will not
    // style it specially.
    //
    final List<Edit> empty = new ArrayList<>();
    int lastLine;

    lastLine = -1;
    for (PatchLineComment plc : comments.getCommentsA()) {
        final int a = plc.getLine();
        if (lastLine != a) {
            final int b = mapA2B(a - 1);
            if (0 <= b) {
                safeAdd(empty, new Edit(a - 1, b));
            }
            lastLine = a;
        }
    }

    lastLine = -1;
    for (PatchLineComment plc : comments.getCommentsB()) {
        final int b = plc.getLine();
        if (lastLine != b) {
            final int a = mapB2A(b - 1);
            if (0 <= a) {
                safeAdd(empty, new Edit(a, b - 1));
            }
            lastLine = b;
        }
    }

    // Sort the final list by the index in A, so packContent can combine
    // them correctly later.
    //
    edits.addAll(empty);
    Collections.sort(edits, EDIT_SORT);
}