Example usage for org.eclipse.jgit.lib ObjectReader abbreviate

List of usage examples for org.eclipse.jgit.lib ObjectReader abbreviate

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ObjectReader abbreviate.

Prototype

public AbbreviatedObjectId abbreviate(AnyObjectId objectId, int len) throws IOException 

Source Link

Document

Obtain a unique abbreviation (prefix) of an object SHA-1.

Usage

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

License:Apache License

public static Text forCommit(ObjectReader reader, AnyObjectId commitId) throws IOException {
    try (RevWalk rw = new RevWalk(reader)) {
        RevCommit c;// w  ww.j a  v  a2s  .c  o  m
        if (commitId instanceof RevCommit) {
            c = (RevCommit) commitId;
        } else {
            c = rw.parseCommit(commitId);
        }

        StringBuilder b = new StringBuilder();
        switch (c.getParentCount()) {
        case 0:
            break;
        case 1: {
            RevCommit p = c.getParent(0);
            rw.parseBody(p);
            b.append("Parent:     ");
            b.append(reader.abbreviate(p, 8).name());
            b.append(" (");
            b.append(p.getShortMessage());
            b.append(")\n");
            break;
        }
        default:
            for (int i = 0; i < c.getParentCount(); i++) {
                RevCommit p = c.getParent(i);
                rw.parseBody(p);
                b.append(i == 0 ? "Merge Of:   " : "            ");
                b.append(reader.abbreviate(p, 8).name());
                b.append(" (");
                b.append(p.getShortMessage());
                b.append(")\n");
            }
        }
        appendPersonIdent(b, "Author", c.getAuthorIdent());
        appendPersonIdent(b, "Commit", c.getCommitterIdent());
        b.append("\n");
        b.append(c.getFullMessage());
        return new Text(b.toString().getBytes("UTF-8"));
    }
}

From source file:com.microsoft.gittf.core.util.ObjectIdUtil.java

License:Open Source License

/**
 * Abbreviates an object id to minimum of seven char representation that is
 * unique to the repository/*from  ww w  . j a  va 2  s . c o m*/
 * 
 * @param objectID
 *        the object id to abbreviate
 * 
 * @return String representation of the abbreviated object id
 */
public static final String abbreviate(final Repository repository, final ObjectId objectID) {
    Check.notNull(objectID, "objectID"); //$NON-NLS-1$

    if (repository != null) {
        ObjectReader objReader = repository.getObjectDatabase().newReader();

        try {
            return objReader.abbreviate(objectID, ABBREVIATED_LENGTH).name();
        } catch (IOException e) {
            log.warn("Could not read object from object database", e); //$NON-NLS-1$
        } finally {
            if (objReader != null) {
                objReader.release();
            }
        }
    }

    return objectID.getName().substring(0, ABBREVIATED_LENGTH);
}

From source file:org.eclipse.egit.ui.internal.synchronize.mapping.GitChangeSetLabelProvider.java

License:Open Source License

private String getAbbreviatedId(GitModelCommit commit) {
    RevCommit remoteCommit = commit.getBaseCommit();
    ObjectReader reader = commit.getRepository().newObjectReader();
    ObjectId commitId = remoteCommit.getId();
    AbbreviatedObjectId shortId;//from   www .  j a va2s. c om
    try {
        shortId = reader.abbreviate(commitId, 6);
    } catch (IOException e) {
        shortId = AbbreviatedObjectId.fromObjectId(ObjectId.zeroId());
        Activator.logError(e.getMessage(), e);
    } finally {
        reader.release();
    }
    return shortId.name();
}

From source file:pl.project13.jgit.DescribeResult.java

License:Open Source License

/**
 * JGit won't ever use 1 char as abbreviated ID, that's why only values of:
 * <ul>//from w  ww .ja  v a 2 s .co  m
 *   <li>0 (special meaning - don't show commit id at all),</li>
 *   <li>the range from 2 to 40 (inclusive) are valid</li>
 * </ul>
 *
 * @return the abbreviated commit id, possibly longer than the requested len (if it wouldn't be unique)
 */
private static Optional<AbbreviatedObjectId> createAbbreviatedCommitId(@NotNull ObjectReader objectReader,
        ObjectId commitId, int requestedLenght) {
    if (requestedLenght < 2) {
        // 0 means we don't want to print commit id's at all
        return Optional.absent();
    }

    try {
        AbbreviatedObjectId abbreviatedObjectId = objectReader.abbreviate(commitId, requestedLenght);
        return Optional.of(abbreviatedObjectId);
    } catch (IOException e) {
        return Optional.absent();
    }
}

From source file:pl.project13.maven.git.GitCommitIdMojo.java

License:Open Source License

private void putAbbrevCommitId(ObjectReader objectReader, Properties properties, RevCommit headCommit,
        int abbrevLength) throws MojoExecutionException {
    if (abbrevLength < 2 || abbrevLength > 40) {
        throw new MojoExecutionException(
                "Abbreviated commit id lenght must be between 2 and 40, inclusive! Was [%s]. ".codePointBefore(
                        abbrevLength) + "Please fix your configuration (the <abbrevLength/> element).");
    }/*from   ww  w  .j a  v  a  2 s.co m*/

    try {
        AbbreviatedObjectId abbreviatedObjectId = objectReader.abbreviate(headCommit, abbrevLength);
        put(properties, COMMIT_ID_ABBREV, abbreviatedObjectId.name());
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to abbreviate commit id! "
                + "You may want to investigate the <abbrevLength/> element in your configuration.", e);
    }
}