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) throws IOException 

Source Link

Document

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

Usage

From source file:com.google.gitiles.blame.BlameServlet.java

License:Open Source License

private static SoyListData toSoyData(GitilesView view, ObjectReader reader, List<Region> regions,
        DateFormatter df) throws IOException {
    Map<ObjectId, String> abbrevShas = Maps.newHashMap();
    SoyListData result = new SoyListData();

    for (int i = 0; i < regions.size(); i++) {
        Region r = regions.get(i);
        int c = i % CLASSES.size();
        if (r.getSourceCommit() == null) {
            // JGit bug may fail to blame some regions. We should fix this
            // upstream, but handle it for now.
            result.add(NULLS.get(c));// ww w.  j a  v  a2 s .c om
        } else {
            String abbrevSha = abbrevShas.get(r.getSourceCommit());
            if (abbrevSha == null) {
                abbrevSha = reader.abbreviate(r.getSourceCommit()).name();
                abbrevShas.put(r.getSourceCommit(), abbrevSha);
            }
            Map<String, Object> e = Maps.newHashMapWithExpectedSize(6);
            e.put("abbrevSha", abbrevSha);
            e.put("blameUrl", GitilesView.blame().copyFrom(view).setRevision(r.getSourceCommit().name())
                    .setPathPart(r.getSourcePath()).toUrl());
            e.put("commitUrl",
                    GitilesView.revision().copyFrom(view).setRevision(r.getSourceCommit().name()).toUrl());
            e.put("diffUrl", GitilesView.diff().copyFrom(view).setRevision(r.getSourceCommit().name())
                    .setPathPart(r.getSourcePath()).toUrl());
            e.put("author", CommitSoyData.toSoyData(r.getSourceAuthor(), df));
            e.put("class", CLASSES.get(c));
            result.add(e);
        }
        // Pad the list with null regions so we can iterate in parallel in the
        // template. We can't do this by maintaining an index variable into the
        // regions list because Soy {let} is an unmodifiable alias scoped to a
        // single block.
        for (int j = 0; j < r.getCount() - 1; j++) {
            result.add(NULLS.get(c));
        }
    }
    return result;
}

From source file:com.google.gitiles.BlameServlet.java

License:Open Source License

private static List<Map<String, ?>> toSoyData(GitilesView view, ObjectReader reader, List<Region> regions,
        GitDateFormatter df) throws IOException {
    Map<ObjectId, String> abbrevShas = Maps.newHashMap();
    List<Map<String, ?>> result = Lists.newArrayListWithCapacity(regions.size());
    for (BlameCache.Region r : regions) {
        if (r.getSourceCommit() == null) {
            // JGit bug may fail to blame some regions. We should fix this
            // upstream, but handle it for now.
            result.add(ImmutableMap.of("count", r.getCount()));
        } else {/* w  ww . j  av a  2s.  c o m*/
            String abbrevSha = abbrevShas.get(r.getSourceCommit());
            if (abbrevSha == null) {
                abbrevSha = reader.abbreviate(r.getSourceCommit()).name();
                abbrevShas.put(r.getSourceCommit(), abbrevSha);
            }
            result.add(ImmutableMap.of("abbrevSha", abbrevSha, "url",
                    GitilesView.blame().copyFrom(view).setRevision(r.getSourceCommit().name())
                            .setPathPart(r.getSourcePath()).toUrl(),
                    "author", CommitSoyData.toSoyData(r.getSourceAuthor(), df), "count", r.getCount()));
        }
    }
    return result;
}

From source file:com.google.gitiles.CommitSoyData.java

License:Open Source License

public Map<String, Object> toSoyData(RevCommit commit, KeySet keys) throws IOException {
    Map<String, Object> data = Maps.newHashMapWithExpectedSize(KeySet.DEFAULT.keys.size());
    if (keys.keys.contains("author")) {
        data.put("author", toSoyData(commit.getAuthorIdent(), dateFormatter));
    }//from   w ww.  ja  va 2s.co m
    if (keys.keys.contains("committer")) {
        data.put("committer", toSoyData(commit.getCommitterIdent(), dateFormatter));
    }
    if (keys.keys.contains("sha")) {
        data.put("sha", ObjectId.toString(commit));
    }
    if (keys.keys.contains("abbrevSha")) {
        ObjectReader reader = repo.getObjectDatabase().newReader();
        try {
            data.put("abbrevSha", reader.abbreviate(commit).name());
        } finally {
            reader.release();
        }
    }
    if (keys.keys.contains("url")) {
        data.put("url", GitilesView.revision().copyFrom(view).setRevision(commit).toUrl());
    }
    if (keys.keys.contains("logUrl")) {
        Revision rev = view.getRevision();
        GitilesView.Builder logView = GitilesView.log().copyFrom(view)
                .setRevision(rev.getId().equals(commit) ? rev.getName() : commit.name(), commit)
                .setOldRevision(Revision.NULL).setTreePath(null);
        data.put("logUrl", logView.toUrl());
    }
    if (keys.keys.contains("tree")) {
        data.put("tree", ObjectId.toString(commit.getTree()));
    }
    if (keys.keys.contains("treeUrl")) {
        data.put("treeUrl", GitilesView.path().copyFrom(view).setTreePath("/").toUrl());
    }
    if (keys.keys.contains("parents")) {
        data.put("parents", toSoyData(view, commit.getParents()));
    }
    if (keys.keys.contains("shortMessage")) {
        data.put("shortMessage", commit.getShortMessage());
    }
    if (keys.keys.contains("branches")) {
        data.put("branches", getRefsById(commit, Constants.R_HEADS));
    }
    if (keys.keys.contains("tags")) {
        data.put("tags", getRefsById(commit, Constants.R_TAGS));
    }
    if (keys.keys.contains("message")) {
        if (linkifier != null) {
            data.put("message", linkifier.linkify(req, commit.getFullMessage()));
        } else {
            data.put("message", commit.getFullMessage());
        }
    }
    if (keys.keys.contains("diffTree")) {
        data.put("diffTree", computeDiffTree(commit));
    }
    checkState(keys.keys.size() == data.size(), "bad commit data keys: %s != %s", keys.keys, data.keySet());
    return ImmutableMap.copyOf(data);
}

From source file:net.mobid.codetraq.runnables.GitChecker.java

License:Open Source License

private String safeAbbreviate(ObjectReader reader, ObjectId id) {
    try {//from w w  w .  j a v a  2 s.c  o m
        return reader.abbreviate(id).name();
    } catch (IOException cannotAbbreviate) {
        return id.name();
    }
}

From source file:org.eclipse.egit.ui.internal.components.RefContentProposal.java

License:Open Source License

public String getDescription() {
    if (objectId == null)
        return null;
    ObjectReader reader = db.newObjectReader();
    try {/*  www . j ava  2 s  . co m*/
        final ObjectLoader loader = reader.open(objectId);
        final StringBuilder sb = new StringBuilder();
        sb.append(refName);
        sb.append('\n');
        sb.append(reader.abbreviate(objectId).name());
        sb.append(" - "); //$NON-NLS-1$

        switch (loader.getType()) {
        case Constants.OBJ_COMMIT:
            RevCommit c = new RevWalk(db).parseCommit(objectId);
            appendObjectSummary(sb, UIText.RefContentProposal_commit, c.getAuthorIdent(), c.getFullMessage());
            break;
        case Constants.OBJ_TAG:
            RevWalk walk = new RevWalk(db);
            RevTag t = walk.parseTag(objectId);
            appendObjectSummary(sb, UIText.RefContentProposal_tag, t.getTaggerIdent(), t.getFullMessage());
            break;
        case Constants.OBJ_TREE:
            sb.append(UIText.RefContentProposal_tree);
            break;
        case Constants.OBJ_BLOB:
            sb.append(UIText.RefContentProposal_blob);
            break;
        default:
            sb.append(UIText.RefContentProposal_unknownObject);
        }
        return sb.toString();
    } catch (IOException e) {
        Activator.logError(NLS.bind(UIText.RefContentProposal_errorReadingObject, objectId), e);
        return null;
    } finally {
        reader.release();
    }
}

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$
    }// w w w  .  j a  v a  2 s  .c  om
    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);
}