Example usage for org.eclipse.jgit.util RawParseUtils endOfParagraph

List of usage examples for org.eclipse.jgit.util RawParseUtils endOfParagraph

Introduction

In this page you can find the example usage for org.eclipse.jgit.util RawParseUtils endOfParagraph.

Prototype

public static final int endOfParagraph(byte[] b, int start) 

Source Link

Document

Locate the end of a paragraph.

Usage

From source file:com.google.gerrit.server.notedb.ChangeNotesParser.java

License:Apache License

private void parseChangeMessage(PatchSet.Id psId, Account.Id accountId, RevCommit commit) {
    byte[] raw = commit.getRawBuffer();
    int size = raw.length;
    Charset enc = RawParseUtils.parseEncoding(raw);

    int subjectStart = RawParseUtils.commitMessage(raw, 0);
    if (subjectStart < 0 || subjectStart >= size) {
        return;/*from  w w  w .ja  va  2s .  co  m*/
    }

    int subjectEnd = RawParseUtils.endOfParagraph(raw, subjectStart);
    if (subjectEnd == size) {
        return;
    }

    int changeMessageStart;

    if (raw[subjectEnd] == '\n') {
        changeMessageStart = subjectEnd + 2; //\n\n ends paragraph
    } else if (raw[subjectEnd] == '\r') {
        changeMessageStart = subjectEnd + 4; //\r\n\r\n ends paragraph
    } else {
        return;
    }

    int ptr = size - 1;
    int changeMessageEnd = -1;
    while (ptr > changeMessageStart) {
        ptr = RawParseUtils.prevLF(raw, ptr, '\r');
        if (ptr == -1) {
            break;
        }
        if (raw[ptr] == '\n') {
            changeMessageEnd = ptr - 1;
            break;
        } else if (raw[ptr] == '\r') {
            changeMessageEnd = ptr - 3;
            break;
        }
    }

    if (ptr <= changeMessageStart) {
        return;
    }

    String changeMsgString = RawParseUtils.decode(enc, raw, changeMessageStart, changeMessageEnd + 1);
    ChangeMessage changeMessage = new ChangeMessage(new ChangeMessage.Key(psId.getParentKey(), commit.name()),
            accountId, new Timestamp(commit.getCommitterIdent().getWhen().getTime()), psId);
    changeMessage.setMessage(changeMsgString);
    changeMessages.put(psId, changeMessage);
}

From source file:playRepository.GitCommit.java

License:Apache License

/**
 * Parse the commit message and return the first "line" of it.
 * <p>/*w w  w. j a v a  2 s .c o  m*/
 * The first line is everything up to the first pair of LFs. This is the
 * "oneline" format, suitable for output in a single line display.
 * <p>
 * This method parses and returns the message portion of the commit buffer,
 * after taking the commit's character set into account and decoding the
 * buffer using that character set. This method is a fairly expensive
 * operation and produces a new string on each invocation.
 *
 * @return decoded commit message as a string. Never null. The returned
 *         string does not contain any LFs, even if the first paragraph
 *         spanned multiple lines. Embedded LFs are converted to spaces.
 */
@Override
public String getShortMessage() {
    if (shortMessage == null) {
        final byte[] raw = revCommit.getRawBuffer();
        final int msgB = RawParseUtils.commitMessage(raw, 0);
        if (msgB < 0)
            return ""; //$NON-NLS-1$

        final Charset enc = parseEncoding(raw, Charset.defaultCharset());
        final int msgE = RawParseUtils.endOfParagraph(raw, msgB);
        String str = RawParseUtils.decode(enc, raw, msgB, msgE);
        if (hasLF(raw, msgB, msgE))
            str = StringUtils.replaceLineBreaksWithSpace(str);
        shortMessage = str;
    }

    return shortMessage;
}