Example usage for org.eclipse.jgit.lib ObjectId getName

List of usage examples for org.eclipse.jgit.lib ObjectId getName

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ObjectId getName.

Prototype

public final String getName() 

Source Link

Document

Get string form of the SHA-1, in lower case hexadecimal.

Usage

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * Returns a path model of the current file in the treewalk.
 *
 * @param tw//  w w w  .  ja  v a2 s .  com
 * @param basePath
 * @param commit
 * @return a path model of the current file in the treewalk
 */
private static PathModel getPathModel(TreeWalk tw, String basePath, RevCommit commit) {
    String name;
    long size = 0;

    if (StringUtils.isEmpty(basePath)) {
        name = tw.getPathString();
    } else {
        name = tw.getPathString().substring(basePath.length() + 1);
    }
    ObjectId objectId = tw.getObjectId(0);
    FilestoreModel filestoreItem = null;

    try {
        if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) {

            size = tw.getObjectReader().getObjectSize(objectId, Constants.OBJ_BLOB);

            if (isPossibleFilestoreItem(size)) {
                filestoreItem = getFilestoreItem(tw.getObjectReader().open(objectId));
            }
        }
    } catch (Throwable t) {
        error(t, null, "failed to retrieve blob size for " + tw.getPathString());
    }
    return new PathModel(name, tw.getPathString(), filestoreItem, size, tw.getFileMode(0).getBits(),
            objectId.getName(), commit.getName());
}

From source file:com.gitblit.utils.MetricUtils.java

License:Apache License

/**
 * Returns the list of metrics for the specified commit reference, branch,
 * or tag within the repository. If includeTotal is true, the total of all
 * the metrics will be included as the first element in the returned list.
 *
 * If the dateformat is unspecified an attempt is made to determine an
 * appropriate date format by determining the time difference between the
 * first commit on the branch and the most recent commit. This assumes that
 * the commits are linear.//from  www  . j  a v  a  2  s . c om
 *
 * @param repository
 * @param objectId
 *            if null or empty, HEAD is assumed.
 * @param includeTotal
 * @param dateFormat
 * @param timezone
 * @return list of metrics
 */
public static List<Metric> getDateMetrics(Repository repository, String objectId, boolean includeTotal,
        String dateFormat, TimeZone timezone) {
    Metric total = new Metric("TOTAL");
    final Map<String, Metric> metricMap = new HashMap<String, Metric>();

    if (JGitUtils.hasCommits(repository)) {
        final List<RefModel> tags = JGitUtils.getTags(repository, true, -1);
        final Map<ObjectId, RefModel> tagMap = new HashMap<ObjectId, RefModel>();
        for (RefModel tag : tags) {
            tagMap.put(tag.getReferencedObjectId(), tag);
        }
        RevWalk revWalk = null;
        try {
            // resolve branch
            ObjectId branchObject;
            if (StringUtils.isEmpty(objectId)) {
                branchObject = JGitUtils.getDefaultBranch(repository);
            } else {
                branchObject = repository.resolve(objectId);
            }

            revWalk = new RevWalk(repository);
            RevCommit lastCommit = revWalk.parseCommit(branchObject);
            revWalk.markStart(lastCommit);

            DateFormat df;
            if (StringUtils.isEmpty(dateFormat)) {
                // dynamically determine date format
                RevCommit firstCommit = JGitUtils.getFirstCommit(repository, branchObject.getName());
                int diffDays = (lastCommit.getCommitTime() - firstCommit.getCommitTime()) / (60 * 60 * 24);
                total.duration = diffDays;
                if (diffDays <= 365) {
                    // Days
                    df = new SimpleDateFormat("yyyy-MM-dd");
                } else {
                    // Months
                    df = new SimpleDateFormat("yyyy-MM");
                }
            } else {
                // use specified date format
                df = new SimpleDateFormat(dateFormat);
            }
            df.setTimeZone(timezone);

            Iterable<RevCommit> revlog = revWalk;
            for (RevCommit rev : revlog) {
                Date d = JGitUtils.getAuthorDate(rev);
                String p = df.format(d);
                if (!metricMap.containsKey(p)) {
                    metricMap.put(p, new Metric(p));
                }
                Metric m = metricMap.get(p);
                m.count++;
                total.count++;
                if (tagMap.containsKey(rev.getId())) {
                    m.tag++;
                    total.tag++;
                }
            }
        } catch (Throwable t) {
            error(t, repository, "{0} failed to mine log history for date metrics of {1}", objectId);
        } finally {
            if (revWalk != null) {
                revWalk.dispose();
            }
        }
    }
    List<String> keys = new ArrayList<String>(metricMap.keySet());
    Collections.sort(keys);
    List<Metric> metrics = new ArrayList<Metric>();
    for (String key : keys) {
        metrics.add(metricMap.get(key));
    }
    if (includeTotal) {
        metrics.add(0, total);
    }
    return metrics;
}

From source file:com.github.kaitoy.goslings.server.dao.jgit.ReferenceDaoImpl.java

License:Open Source License

/**
 * this method converts a {@link Ref} object to {@link Tag} object.
 *
 * @param ref a {@link Ref} object representing a tag.
 * @return a new {@link Tag} instance.//  ww  w  .j av  a 2  s  .c om
 */
private Tag convertToTag(Ref ref) {
    ObjectId peeledObjId = ref.getPeeledObjectId();
    if (peeledObjId == null) {
        // lightweight tag
        return new Tag(ref.getName(), null, ref.getObjectId().getName());
    } else {
        // annotated tag
        return new Tag(ref.getName(), ref.getObjectId().getName(), peeledObjId.getName());
    }
}

From source file:com.github.koraktor.mavanagaiata.git.jgit.JGitRepository.java

License:Open Source License

/**
 * Returns a commit object for the given object ID
 *
 * @return The commit object for the given object ID
 * @see RevCommit//  www  .java2  s  .  c o  m
 * @throws GitRepositoryException if the commit object cannot be retrieved
 */
protected RevCommit getCommit(ObjectId id) throws GitRepositoryException {
    if (this.commitCache.containsKey(id)) {
        return this.commitCache.get(id);
    }

    try {
        RevWalk revWalk = this.getRevWalk();
        RevCommit commit = revWalk.parseCommit(id);

        this.commitCache.put(id, commit);

        return commit;
    } catch (IncorrectObjectTypeException e) {
        throw new GitRepositoryException(String.format("Object \"%s\" is not a commit.", id.getName()), e);
    } catch (MissingObjectException e) {
        throw new GitRepositoryException(String.format("Commit \"%s\" is missing.", id.getName()), e);
    } catch (IOException e) {
        throw new GitRepositoryException(String.format("Commit \"%s\" could not be loaded.", id.getName()), e);
    }
}

From source file:com.google.gerrit.acceptance.git.GitUtil.java

License:Apache License

private static Commit createCommit(Git git, PersonIdent i, String msg, String changeId)
        throws GitAPIException, IOException {

    final CommitCommand commitCmd = git.commit();
    commitCmd.setAmend(changeId != null);
    commitCmd.setAuthor(i);/*from www.ja va2 s. c  o m*/
    commitCmd.setCommitter(i);

    if (changeId == null) {
        ObjectId id = computeChangeId(git, i, msg);
        changeId = "I" + id.getName();
    }
    msg = ChangeIdUtil.insertId(msg, ObjectId.fromString(changeId.substring(1)));
    commitCmd.setMessage(msg);

    RevCommit c = commitCmd.call();
    return new Commit(c, changeId);
}

From source file:com.google.gerrit.acceptance.git.ssh.GitUtil.java

License:Apache License

public static String createCommit(Git git, PersonIdent i, String msg, boolean insertChangeId)
        throws GitAPIException, IOException {
    ObjectId changeId = null;
    if (insertChangeId) {
        changeId = computeChangeId(git, i, msg);
        msg = ChangeIdUtil.insertId(msg, changeId);
    }//from   w w  w  .  j  av  a 2s.  c o m

    final CommitCommand commitCmd = git.commit();
    commitCmd.setAuthor(i);
    commitCmd.setCommitter(i);
    commitCmd.setMessage(msg);
    commitCmd.call();

    return changeId != null ? "I" + changeId.getName() : null;
}

From source file:com.google.gerrit.acceptance.rest.account.ExternalIdIT.java

License:Apache License

private String insertExternalIdWithoutAccountId(Repository repo, RevWalk rw, String externalId)
        throws IOException {
    ObjectId rev = ExternalIdReader.readRevision(repo);
    NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);

    ExternalId extId = ExternalId.create(ExternalId.Key.parse(externalId), admin.id);

    try (ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId noteId = extId.key().sha1();
        Config c = new Config();
        extId.writeToConfig(c);//w  w  w  . ja v  a  2 s .c  o  m
        c.unset("externalId", extId.key().get(), "accountId");
        byte[] raw = c.toText().getBytes(UTF_8);
        ObjectId dataBlob = ins.insert(OBJ_BLOB, raw);
        noteMap.set(noteId, dataBlob);

        ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(),
                admin.getIdent());
        return noteId.getName();
    }
}

From source file:com.google.gerrit.acceptance.rest.account.ExternalIdIT.java

License:Apache License

private String insertExternalIdWithKeyThatDoesntMatchNoteId(Repository repo, RevWalk rw, String externalId)
        throws IOException {
    ObjectId rev = ExternalIdReader.readRevision(repo);
    NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);

    ExternalId extId = ExternalId.create(ExternalId.Key.parse(externalId), admin.id);

    try (ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId noteId = ExternalId.Key.parse(externalId + "x").sha1();
        Config c = new Config();
        extId.writeToConfig(c);//from w ww .  ja  v a  2  s. c  om
        byte[] raw = c.toText().getBytes(UTF_8);
        ObjectId dataBlob = ins.insert(OBJ_BLOB, raw);
        noteMap.set(noteId, dataBlob);

        ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(),
                admin.getIdent());
        return noteId.getName();
    }
}

From source file:com.google.gerrit.acceptance.rest.account.ExternalIdIT.java

License:Apache License

private String insertExternalIdWithInvalidConfig(Repository repo, RevWalk rw, String externalId)
        throws IOException {
    ObjectId rev = ExternalIdReader.readRevision(repo);
    NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);

    try (ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId noteId = ExternalId.Key.parse(externalId).sha1();
        byte[] raw = "bad-config".getBytes(UTF_8);
        ObjectId dataBlob = ins.insert(OBJ_BLOB, raw);
        noteMap.set(noteId, dataBlob);/*from w w w  .  ja  va2 s .co  m*/

        ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(),
                admin.getIdent());
        return noteId.getName();
    }
}

From source file:com.google.gerrit.acceptance.rest.account.ExternalIdIT.java

License:Apache License

private String insertExternalIdWithEmptyNote(Repository repo, RevWalk rw, String externalId)
        throws IOException {
    ObjectId rev = ExternalIdReader.readRevision(repo);
    NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);

    try (ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId noteId = ExternalId.Key.parse(externalId).sha1();
        byte[] raw = "".getBytes(UTF_8);
        ObjectId dataBlob = ins.insert(OBJ_BLOB, raw);
        noteMap.set(noteId, dataBlob);/* ww w. j a v  a 2 s.  c o  m*/

        ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(),
                admin.getIdent());
        return noteId.getName();
    }
}