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

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

Introduction

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

Prototype

public final int getBeginB() 

Source Link

Document

Get start point in sequence B

Usage

From source file:com.GitAnalytics.CommitInfo.java

private CommitInfo(DiffFormatter df, RevWalk rw, RevCommit commit, List<String> tags) throws Exception {
    System.out.print("Processing Commit: " + I + "\r");
    I++;//from   w w  w  . j  a  va 2  s.co  m
    mCommit = commit;
    mTags = tags;
    mAuthor = mCommit.getAuthorIdent();
    mLinesChanged = new int[3];

    RevTree parent = null;
    try {
        parent = rw.parseCommit(commit.getParent(0).getId()).getTree();
    } catch (Exception e) {
    }
    //int filesChanged = diffs.size();
    mLinesChanged[0] = mLinesChanged[1] = mLinesChanged[2] = 0;
    for (DiffEntry diff : df.scan(parent, commit.getTree())) {
        for (Edit edit : df.toFileHeader(diff).toEditList()) {
            int del = edit.getEndA() - edit.getBeginA();
            int add = edit.getEndB() - edit.getBeginB();

            if (edit.getType() == Type.REPLACE) {
                mLinesChanged[1] += Math.abs(add - del);
            }

            mLinesChanged[2] += add;
            mLinesChanged[0] += del;
        }
    }
}

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

License:Apache License

private void safeAdd(final List<Edit> empty, final Edit toAdd) {
    final int a = toAdd.getBeginA();
    final int b = toAdd.getBeginB();
    for (final Edit e : edits) {
        if (e.getBeginA() <= a && a <= e.getEndA()) {
            return;
        }/*from  w w  w  .ja va 2  s. co m*/
        if (e.getBeginB() <= b && b <= e.getEndB()) {
            return;
        }
    }
    empty.add(toAdd);
}

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

License:Apache License

private int mapA2B(final int a) {
    if (edits.isEmpty()) {
        // Magic special case of an unmodified file.
        ////from  w w  w  .  j a v  a 2  s.c o m
        return a;
    }

    for (int i = 0; i < edits.size(); i++) {
        final Edit e = edits.get(i);
        if (a < e.getBeginA()) {
            if (i == 0) {
                // Special case of context at start of file.
                //
                return a;
            }
            return e.getBeginB() - (e.getBeginA() - a);
        }
        if (e.getBeginA() <= a && a <= e.getEndA()) {
            return -1;
        }
    }

    final Edit last = edits.get(edits.size() - 1);
    return last.getBeginB() + (a - last.getEndA());
}

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

License:Apache License

private int mapB2A(final int b) {
    if (edits.isEmpty()) {
        // Magic special case of an unmodified file.
        ////from  w  ww.j a  va2s  . c o m
        return b;
    }

    for (int i = 0; i < edits.size(); i++) {
        final Edit e = edits.get(i);
        if (b < e.getBeginB()) {
            if (i == 0) {
                // Special case of context at start of file.
                //
                return b;
            }
            return e.getBeginA() - (e.getBeginB() - b);
        }
        if (e.getBeginB() <= b && b <= e.getEndB()) {
            return -1;
        }
    }

    final Edit last = edits.get(edits.size() - 1);
    return last.getBeginA() + (b - last.getEndB());
}

From source file:com.google.gerrit.server.change.GetDiff.java

License:Apache License

@Override
public Response<DiffInfo> apply(FileResource resource) throws ResourceConflictException,
        ResourceNotFoundException, OrmException, AuthException, InvalidChangeOperationException, IOException {
    PatchSet basePatchSet = null;/* w w  w . j ava 2  s .  c om*/
    if (base != null) {
        RevisionResource baseResource = revisions.parse(resource.getRevision().getChangeResource(),
                IdString.fromDecoded(base));
        basePatchSet = baseResource.getPatchSet();
    }
    AccountDiffPreference prefs = new AccountDiffPreference(new Account.Id(0));
    prefs.setIgnoreWhitespace(ignoreWhitespace.whitespace);
    prefs.setContext(context);
    prefs.setIntralineDifference(intraline);

    try {
        PatchScriptFactory psf = patchScriptFactoryFactory.create(resource.getRevision().getControl(),
                resource.getPatchKey().getFileName(), basePatchSet != null ? basePatchSet.getId() : null,
                resource.getPatchKey().getParentKey(), prefs);
        psf.setLoadHistory(false);
        psf.setLoadComments(context != AccountDiffPreference.WHOLE_FILE_CONTEXT);
        PatchScript ps = psf.call();
        Content content = new Content(ps);
        for (Edit edit : ps.getEdits()) {
            if (edit.getType() == Edit.Type.EMPTY) {
                continue;
            }
            content.addCommon(edit.getBeginA());

            checkState(content.nextA == edit.getBeginA(), "nextA = %s; want %s", content.nextA,
                    edit.getBeginA());
            checkState(content.nextB == edit.getBeginB(), "nextB = %s; want %s", content.nextB,
                    edit.getBeginB());
            switch (edit.getType()) {
            case DELETE:
            case INSERT:
            case REPLACE:
                List<Edit> internalEdit = edit instanceof ReplaceEdit ? ((ReplaceEdit) edit).getInternalEdits()
                        : null;
                content.addDiff(edit.getEndA(), edit.getEndB(), internalEdit);
                break;
            case EMPTY:
            default:
                throw new IllegalStateException();
            }
        }
        content.addCommon(ps.getA().size());

        ProjectState state = projectCache.get(resource.getRevision().getChange().getProject());

        DiffInfo result = new DiffInfo();
        // TODO referring to the parent commit by refs/changes/12/60012/1^1
        // will likely not work for inline edits
        String revA = basePatchSet != null ? basePatchSet.getRefName()
                : resource.getRevision().getPatchSet().getRefName() + "^1";
        String revB = resource.getRevision().getEdit().isPresent()
                ? resource.getRevision().getEdit().get().getRefName()
                : resource.getRevision().getPatchSet().getRefName();

        FluentIterable<DiffWebLinkInfo> links = webLinks.getDiffLinks(state.getProject().getName(),
                resource.getPatchKey().getParentKey().getParentKey().get(),
                basePatchSet != null ? basePatchSet.getId().get() : null, revA,
                MoreObjects.firstNonNull(ps.getOldName(), ps.getNewName()),
                resource.getPatchKey().getParentKey().get(), revB, ps.getNewName());
        result.webLinks = links.isEmpty() ? null : links.toList();

        if (!webLinksOnly) {
            if (ps.isBinary()) {
                result.binary = true;
            }
            if (ps.getDisplayMethodA() != DisplayMethod.NONE) {
                result.metaA = new FileMeta();
                result.metaA.name = MoreObjects.firstNonNull(ps.getOldName(), ps.getNewName());
                result.metaA.contentType = FileContentUtil.resolveContentType(state, result.metaA.name,
                        ps.getFileModeA(), ps.getMimeTypeA());
                result.metaA.lines = ps.getA().size();
                result.metaA.webLinks = getFileWebLinks(state.getProject(), revA, result.metaA.name);
                result.metaA.commitId = content.commitIdA;
            }

            if (ps.getDisplayMethodB() != DisplayMethod.NONE) {
                result.metaB = new FileMeta();
                result.metaB.name = ps.getNewName();
                result.metaB.contentType = FileContentUtil.resolveContentType(state, result.metaB.name,
                        ps.getFileModeB(), ps.getMimeTypeB());
                result.metaB.lines = ps.getB().size();
                result.metaB.webLinks = getFileWebLinks(state.getProject(), revB, result.metaB.name);
                result.metaB.commitId = content.commitIdB;
            }

            if (intraline) {
                if (ps.hasIntralineTimeout()) {
                    result.intralineStatus = IntraLineStatus.TIMEOUT;
                } else if (ps.hasIntralineFailure()) {
                    result.intralineStatus = IntraLineStatus.FAILURE;
                } else {
                    result.intralineStatus = IntraLineStatus.OK;
                }
            }

            result.changeType = CHANGE_TYPE.get(ps.getChangeType());
            if (result.changeType == null) {
                throw new IllegalStateException("unknown change type: " + ps.getChangeType());
            }

            if (ps.getPatchHeader().size() > 0) {
                result.diffHeader = ps.getPatchHeader();
            }
            result.content = content.lines;
        }

        Response<DiffInfo> r = Response.ok(result);
        if (resource.isCacheable()) {
            r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
        }
        return r;
    } catch (NoSuchChangeException e) {
        throw new ResourceNotFoundException(e.getMessage());
    } catch (LargeObjectException e) {
        throw new ResourceConflictException(e.getMessage());
    }
}

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

License:Apache License

private static void writeEdit(OutputStream out, Edit e) throws IOException {
    writeVarInt32(out, e.getBeginA());//from  w  w  w  .ja  va 2s .c  o  m
    writeVarInt32(out, e.getEndA());
    writeVarInt32(out, e.getBeginB());
    writeVarInt32(out, e.getEndB());
}

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

License:Apache License

static IntraLineDiff compute(Text aText, Text bText, List<Edit> edits) throws Exception {
    combineLineEdits(edits, aText, bText);

    for (int i = 0; i < edits.size(); i++) {
        Edit e = edits.get(i);

        if (e.getType() == Edit.Type.REPLACE) {
            CharText a = new CharText(aText, e.getBeginA(), e.getEndA());
            CharText b = new CharText(bText, e.getBeginB(), e.getEndB());
            CharTextComparator cmp = new CharTextComparator();

            List<Edit> wordEdits = MyersDiff.INSTANCE.diff(cmp, a, b);

            // Combine edits that are really close together. If they are
            // just a few characters apart we tend to get better results
            // by joining them together and taking the whole span.
            ////from   w ww.j a  va 2  s .  com
            for (int j = 0; j < wordEdits.size() - 1;) {
                Edit c = wordEdits.get(j);
                Edit n = wordEdits.get(j + 1);

                if (n.getBeginA() - c.getEndA() <= 5 || n.getBeginB() - c.getEndB() <= 5) {
                    int ab = c.getBeginA();
                    int ae = n.getEndA();

                    int bb = c.getBeginB();
                    int be = n.getEndB();

                    if (canCoalesce(a, c.getEndA(), n.getBeginA())
                            && canCoalesce(b, c.getEndB(), n.getBeginB())) {
                        wordEdits.set(j, new Edit(ab, ae, bb, be));
                        wordEdits.remove(j + 1);
                        continue;
                    }
                }

                j++;
            }

            // Apply some simple rules to fix up some of the edits. Our
            // logic above, along with our per-character difference tends
            // to produce some crazy stuff.
            //
            for (int j = 0; j < wordEdits.size(); j++) {
                Edit c = wordEdits.get(j);
                int ab = c.getBeginA();
                int ae = c.getEndA();

                int bb = c.getBeginB();
                int be = c.getEndB();

                // Sometimes the diff generator produces an INSERT or DELETE
                // right up against a REPLACE, but we only find this after
                // we've also played some shifting games on the prior edit.
                // If that happened to us, coalesce them together so we can
                // correct this mess for the user. If we don't we wind up
                // with silly stuff like "es" -> "es = Addresses".
                //
                if (1 < j) {
                    Edit p = wordEdits.get(j - 1);
                    if (p.getEndA() == ab || p.getEndB() == bb) {
                        if (p.getEndA() == ab && p.getBeginA() < p.getEndA()) {
                            ab = p.getBeginA();
                        }
                        if (p.getEndB() == bb && p.getBeginB() < p.getEndB()) {
                            bb = p.getBeginB();
                        }
                        wordEdits.remove(--j);
                    }
                }

                // We sometimes collapsed an edit together in a strange way,
                // such that the edges of each text is identical. Fix by
                // by dropping out that incorrectly replaced region.
                //
                while (ab < ae && bb < be && cmp.equals(a, ab, b, bb)) {
                    ab++;
                    bb++;
                }
                while (ab < ae && bb < be && cmp.equals(a, ae - 1, b, be - 1)) {
                    ae--;
                    be--;
                }

                // The leading part of an edit and its trailing part in the same
                // text might be identical. Slide down that edit and use the tail
                // rather than the leading bit. If however the edit is only on a
                // whitespace block try to shift it to the left margin, assuming
                // that it is an indentation change.
                //
                boolean aShift = true;
                if (ab < ae && isOnlyWhitespace(a, ab, ae)) {
                    int lf = findLF(wordEdits, j, a, ab);
                    if (lf < ab && a.charAt(lf) == '\n') {
                        int nb = lf + 1;
                        int p = 0;
                        while (p < ae - ab) {
                            if (cmp.equals(a, ab + p, a, ab + p)) {
                                p++;
                            } else {
                                break;
                            }
                        }
                        if (p == ae - ab) {
                            ab = nb;
                            ae = nb + p;
                            aShift = false;
                        }
                    }
                }
                if (aShift) {
                    while (0 < ab && ab < ae && a.charAt(ab - 1) != '\n' && cmp.equals(a, ab - 1, a, ae - 1)) {
                        ab--;
                        ae--;
                    }
                    if (!a.isLineStart(ab) || !a.contains(ab, ae, '\n')) {
                        while (ab < ae && ae < a.size() && cmp.equals(a, ab, a, ae)) {
                            ab++;
                            ae++;
                            if (a.charAt(ae - 1) == '\n') {
                                break;
                            }
                        }
                    }
                }

                boolean bShift = true;
                if (bb < be && isOnlyWhitespace(b, bb, be)) {
                    int lf = findLF(wordEdits, j, b, bb);
                    if (lf < bb && b.charAt(lf) == '\n') {
                        int nb = lf + 1;
                        int p = 0;
                        while (p < be - bb) {
                            if (cmp.equals(b, bb + p, b, bb + p)) {
                                p++;
                            } else {
                                break;
                            }
                        }
                        if (p == be - bb) {
                            bb = nb;
                            be = nb + p;
                            bShift = false;
                        }
                    }
                }
                if (bShift) {
                    while (0 < bb && bb < be && b.charAt(bb - 1) != '\n' && cmp.equals(b, bb - 1, b, be - 1)) {
                        bb--;
                        be--;
                    }
                    if (!b.isLineStart(bb) || !b.contains(bb, be, '\n')) {
                        while (bb < be && be < b.size() && cmp.equals(b, bb, b, be)) {
                            bb++;
                            be++;
                            if (b.charAt(be - 1) == '\n') {
                                break;
                            }
                        }
                    }
                }

                // If most of a line was modified except the LF was common, make
                // the LF part of the modification region. This is easier to read.
                //
                if (ab < ae //
                        && (ab == 0 || a.charAt(ab - 1) == '\n') //
                        && ae < a.size() && a.charAt(ae - 1) != '\n' && a.charAt(ae) == '\n') {
                    ae++;
                }
                if (bb < be //
                        && (bb == 0 || b.charAt(bb - 1) == '\n') //
                        && be < b.size() && b.charAt(be - 1) != '\n' && b.charAt(be) == '\n') {
                    be++;
                }

                wordEdits.set(j, new Edit(ab, ae, bb, be));
            }

            edits.set(i, new ReplaceEdit(e, wordEdits));
        }
    }

    return new IntraLineDiff(edits);
}

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

License:Apache License

private static void combineLineEdits(List<Edit> edits, Text a, Text b) {
    for (int j = 0; j < edits.size() - 1;) {
        Edit c = edits.get(j);/*  w ww .j a  va  2 s.co m*/
        Edit n = edits.get(j + 1);

        // 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++;
    }
}

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

License:Apache License

private static List<Edit> intraline(String a, String b, Edit lines) throws Exception {
    Text aText = new Text(a.getBytes(UTF_8));
    Text bText = new Text(b.getBytes(UTF_8));

    IntraLineDiff diff;//from w  w w  . j  a  v  a 2s  . c  o m
    diff = IntraLineLoader.compute(aText, bText, EditList.singleton(lines));

    assertThat(diff.getStatus()).isEqualTo(IntraLineDiff.Status.EDIT_LIST);
    List<Edit> actualEdits = diff.getEdits();
    assertThat(actualEdits).hasSize(1);
    Edit actualEdit = actualEdits.get(0);
    assertThat(actualEdit.getBeginA()).isEqualTo(lines.getBeginA());
    assertThat(actualEdit.getEndA()).isEqualTo(lines.getEndA());
    assertThat(actualEdit.getBeginB()).isEqualTo(lines.getBeginB());
    assertThat(actualEdit.getEndB()).isEqualTo(lines.getEndB());
    assertThat(actualEdit).isInstanceOf(ReplaceEdit.class);

    return ((ReplaceEdit) actualEdit).getInternalEdits();
}

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

License:Apache License

PatchListEntry(final FileHeader hdr, List<Edit> editList) {
    changeType = toChangeType(hdr);// ww w  . jav  a2 s.co m
    patchType = toPatchType(hdr);

    switch (changeType) {
    case DELETED:
        oldName = null;
        newName = hdr.getOldPath();
        break;

    case ADDED:
    case MODIFIED:
        oldName = null;
        newName = hdr.getNewPath();
        break;

    case COPIED:
    case RENAMED:
        oldName = hdr.getOldPath();
        newName = hdr.getNewPath();
        break;

    default:
        throw new IllegalArgumentException("Unsupported type " + changeType);
    }

    header = compact(hdr);

    if (hdr instanceof CombinedFileHeader || hdr.getHunks().isEmpty() //
            || hdr.getOldMode() == FileMode.GITLINK || hdr.getNewMode() == FileMode.GITLINK) {
        edits = Collections.emptyList();
    } else {
        edits = Collections.unmodifiableList(editList);
    }

    int ins = 0;
    int del = 0;
    for (Edit e : editList) {
        del += e.getEndA() - e.getBeginA();
        ins += e.getEndB() - e.getBeginB();
    }
    insertions = ins;
    deletions = del;
}