Example usage for org.eclipse.jgit.patch FileHeader FileHeader

List of usage examples for org.eclipse.jgit.patch FileHeader FileHeader

Introduction

In this page you can find the example usage for org.eclipse.jgit.patch FileHeader FileHeader.

Prototype

public FileHeader(byte[] headerLines, EditList edits, PatchType type) 

Source Link

Document

Constructs a new FileHeader

Usage

From source file:MyDiffFormatter.java

License:Eclipse Distribution License

public FormatResult getFormatResult(DiffEntry ent) throws IOException {
    final FormatResult res = new FormatResult();
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    final EditList editList;
    final FileHeader.PatchType type;

    formatHeader(buf, ent);/*from  ww  w . ja v a2s  . c o  m*/

    if (ent.getOldMode() == GITLINK || ent.getNewMode() == GITLINK) {
        formatOldNewPaths(buf, ent);
        writeGitLinkDiffText(buf, ent);
        editList = new EditList();
        type = PatchType.UNIFIED;

    } else if (ent.getOldId() == null || ent.getNewId() == null) {
        // Content not changed (e.g. only mode, pure rename)
        editList = new EditList();
        type = PatchType.UNIFIED;

    } else {
        assertHaveRepository();

        byte[] aRaw = open(OLD, ent);
        byte[] bRaw = open(NEW, ent);

        if (aRaw == BINARY || bRaw == BINARY //
                || RawText.isBinary(aRaw) || RawText.isBinary(bRaw)) {
            formatOldNewPaths(buf, ent);
            buf.write(encodeASCII("Binary files differ\n")); //$NON-NLS-1$
            editList = new EditList();
            type = PatchType.BINARY;

        } else {
            res.a = new RawText(aRaw);
            res.b = new RawText(bRaw);
            editList = diff(res.a, res.b);
            type = PatchType.UNIFIED;

            switch (ent.getChangeType()) {
            case RENAME:
            case COPY:
                if (!editList.isEmpty())
                    formatOldNewPaths(buf, ent);
                break;

            default:
                formatOldNewPaths(buf, ent);
                break;
            }
        }
    }

    res.header = new FileHeader(buf.toByteArray(), editList, type);
    return res;
}

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

License:Apache License

private PatchListEntry newCommitMessage(final RawTextComparator cmp, final ObjectReader reader,
        final RevCommit aCommit, final RevCommit bCommit) throws IOException {
    StringBuilder hdr = new StringBuilder();

    hdr.append("diff --git");
    if (aCommit != null) {
        hdr.append(" a/").append(Patch.COMMIT_MSG);
    } else {/*  w  ww .j  a va2 s  . c  o m*/
        hdr.append(" ").append(FileHeader.DEV_NULL);
    }
    hdr.append(" b/").append(Patch.COMMIT_MSG);
    hdr.append("\n");

    if (aCommit != null) {
        hdr.append("--- a/").append(Patch.COMMIT_MSG).append("\n");
    } else {
        hdr.append("--- ").append(FileHeader.DEV_NULL).append("\n");
    }
    hdr.append("+++ b/").append(Patch.COMMIT_MSG).append("\n");

    Text aText = aCommit != null ? Text.forCommit(reader, aCommit) : Text.EMPTY;
    Text bText = Text.forCommit(reader, bCommit);

    byte[] rawHdr = hdr.toString().getBytes("UTF-8");
    RawText aRawText = new RawText(aText.getContent());
    RawText bRawText = new RawText(bText.getContent());
    EditList edits = new HistogramDiff().diff(cmp, aRawText, bRawText);
    FileHeader fh = new FileHeader(rawHdr, edits, PatchType.UNIFIED);
    return new PatchListEntry(fh, edits);
}