Example usage for org.eclipse.jgit.diff DiffFormatter format

List of usage examples for org.eclipse.jgit.diff DiffFormatter format

Introduction

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

Prototype

public void format(EditList edits, RawText a, RawText b) throws IOException 

Source Link

Document

Formats a list of edits in unified diff format

Usage

From source file:org.eclipse.egit.ui.internal.history.FileDiff.java

License:Open Source License

private void outputEclipseDiff(final StringBuilder d, final Repository db, final ObjectReader reader,
        final DiffFormatter diffFmt) throws IOException {
    if (!(getBlobs().length == 2))
        throw new UnsupportedOperationException(
                "Not supported yet if the number of parents is different from one"); //$NON-NLS-1$

    String projectRelativePath = getProjectRelativePath(db, getPath());
    d.append("diff --git ").append(projectRelativePath).append(" ") //$NON-NLS-1$ //$NON-NLS-2$
            .append(projectRelativePath).append("\n"); //$NON-NLS-1$
    final ObjectId id1 = getBlobs()[0];
    final ObjectId id2 = getBlobs()[1];
    final FileMode mode1 = getModes()[0];
    final FileMode mode2 = getModes()[1];

    if (id1.equals(ObjectId.zeroId())) {
        d.append("new file mode " + mode2).append("\n"); //$NON-NLS-1$//$NON-NLS-2$
    } else if (id2.equals(ObjectId.zeroId())) {
        d.append("deleted file mode " + mode1).append("\n"); //$NON-NLS-1$//$NON-NLS-2$
    } else if (!mode1.equals(mode2)) {
        d.append("old mode " + mode1); //$NON-NLS-1$
        d.append("new mode " + mode2).append("\n"); //$NON-NLS-1$//$NON-NLS-2$
    }/*from w w  w .ja va 2s . co  m*/
    d.append("index ").append(reader.abbreviate(id1).name()). //$NON-NLS-1$
            append("..").append(reader.abbreviate(id2).name()). //$NON-NLS-1$
            append(mode1.equals(mode2) ? " " + mode1 : "").append("\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    if (id1.equals(ObjectId.zeroId()))
        d.append("--- /dev/null\n"); //$NON-NLS-1$
    else {
        d.append("--- "); //$NON-NLS-1$
        d.append(getProjectRelativePath(db, getPath()));
        d.append("\n"); //$NON-NLS-1$
    }

    if (id2.equals(ObjectId.zeroId()))
        d.append("+++ /dev/null\n"); //$NON-NLS-1$
    else {
        d.append("+++ "); //$NON-NLS-1$
        d.append(getProjectRelativePath(db, getPath()));
        d.append("\n"); //$NON-NLS-1$
    }

    final RawText a = getRawText(id1, reader);
    final RawText b = getRawText(id2, reader);
    EditList editList = MyersDiff.INSTANCE.diff(RawTextComparator.DEFAULT, a, b);
    diffFmt.format(editList, a, b);
}