Example usage for org.eclipse.jgit.revwalk RevWalk peel

List of usage examples for org.eclipse.jgit.revwalk RevWalk peel

Introduction

In this page you can find the example usage for org.eclipse.jgit.revwalk RevWalk peel.

Prototype

public RevObject peel(RevObject obj) throws MissingObjectException, IOException 

Source Link

Document

Peel back annotated tags until a non-tag object is found.

Usage

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

License:Open Source License

/**
 * Returns a map of raw JGit tags available in this repository
 * <p>//from www .  j  a va2  s.  c  o  m
 * The keys of the map are the SHA IDs of the objects referenced by the
 * tags. The map's values are the raw tags themselves.
 * <p>
 * <em>Note</em>: Only annotated tags referencing commit objects will be
 * returned.
 *
 * @return A map of raw JGit tags in this repository
 * @throws GitRepositoryException if an error occurs while determining the
 *         tags in this repository
 */
protected Map<String, RevTag> getRawTags() throws GitRepositoryException {
    RevWalk revWalk = this.getRevWalk();
    Map<String, Ref> tagRefs = this.repository.getTags();
    Map<String, RevTag> tags = new HashMap<String, RevTag>();

    try {
        for (Map.Entry<String, Ref> tag : tagRefs.entrySet()) {
            try {
                RevTag revTag = revWalk.parseTag(tag.getValue().getObjectId());
                RevObject object = revWalk.peel(revTag);
                if (!(object instanceof RevCommit)) {
                    continue;
                }
                tags.put(object.getName(), revTag);
            } catch (IncorrectObjectTypeException e) {
                continue;
            }
        }
    } catch (MissingObjectException e) {
        throw new GitRepositoryException("The tags could not be resolved.", e);
    } catch (IOException e) {
        throw new GitRepositoryException("The tags could not be resolved.", e);
    }

    return tags;
}

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

License:Open Source License

@Test
public void testGetRawTags() throws Exception {
    RevWalk revWalk = mock(RevWalk.class);
    whenNew(RevWalk.class).withArguments(this.repo).thenReturn(revWalk);

    Map<String, Ref> tagRefs = new HashMap<String, Ref>();
    Ref tagRef1 = mock(Ref.class);
    Ref tagRef2 = mock(Ref.class);
    tagRefs.put("1.0.0", tagRef1);
    tagRefs.put("2.0.0", tagRef2);
    when(this.repo.getTags()).thenReturn(tagRefs);

    RevTag tag1 = this.createTag();
    RevTag tag2 = this.createTag();
    RevCommit commit1 = this.createCommit();
    RevObject commit2 = this.createCommit();
    when(tagRef1.getObjectId()).thenReturn(tag1);
    when(revWalk.parseTag(tag1)).thenReturn(tag1);
    when(revWalk.peel(tag1)).thenReturn(commit1);
    when(tagRef2.getObjectId()).thenReturn(tag2);
    when(revWalk.parseTag(tag2)).thenReturn(tag2);
    when(revWalk.peel(tag2)).thenReturn(commit2);

    Map<String, RevTag> tags = new HashMap<String, RevTag>();
    tags.put(commit1.getName(), tag1);/*w w  w .  java  2  s  .  c  om*/
    tags.put(commit2.getName(), tag2);

    assertThat(this.repository.getRawTags(), is(equalTo(tags)));
}

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

License:Open Source License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);

    RevWalk rw = new RevWalk(repo);
    try {/*from   w ww.j  av a  2s  .  c  om*/
        RevObject obj = rw.peel(rw.parseAny(view.getRevision().getId()));
        RevTree root;

        switch (obj.getType()) {
        case OBJ_COMMIT:
            root = ((RevCommit) obj).getTree();
            break;
        case OBJ_TREE:
            root = (RevTree) obj;
            break;
        default:
            res.setStatus(SC_NOT_FOUND);
            return;
        }

        TreeWalk tw;
        FileType type;
        String path = view.getTreePath();
        if (path.isEmpty()) {
            tw = new TreeWalk(rw.getObjectReader());
            tw.addTree(root);
            tw.setRecursive(false);
            type = FileType.TREE;
        } else {
            tw = TreeWalk.forPath(rw.getObjectReader(), path, root);
            if (tw == null) {
                res.setStatus(SC_NOT_FOUND);
                return;
            }
            type = FileType.forEntry(tw);
            if (type == FileType.TREE) {
                tw.enterSubtree();
                tw.setRecursive(false);
            }
        }

        switch (type) {
        case TREE:
            showTree(req, res, rw, tw, obj);
            break;
        case SYMLINK:
            showSymlink(req, res, rw, tw);
            break;
        case REGULAR_FILE:
        case EXECUTABLE_FILE:
            showFile(req, res, rw, tw);
            break;
        case GITLINK:
            showGitlink(req, res, rw, tw, root);
            break;
        default:
            log.error("Bad file type: %s", type);
            res.setStatus(SC_NOT_FOUND);
            break;
        }
    } catch (LargeObjectException e) {
        res.setStatus(SC_INTERNAL_SERVER_ERROR);
    } finally {
        rw.release();
    }
}

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

License:Open Source License

public static Revision peel(String name, ObjectId id, RevWalk walk) throws MissingObjectException, IOException {
    RevObject obj = walk.parseAny(id);//from  w ww .  j  a v a  2  s .  c  o  m
    RevObject peeled = walk.peel(obj);
    return new Revision(name, obj, obj.getType(), peeled, peeled.getType());
}

From source file:com.mangosolutions.rcloud.rawgist.repository.git.BareCommitCommand.java

/**
 * Sets default values for not explicitly specified options. Then validates
 * that all required data has been provided.
 *
 * @param state//from   w  ww .j  av a  2 s . com
 *            the state of the repository we are working on
 * @param rw
 *            the RevWalk to use
 *
 * @throws NoMessageException
 *             if the commit message has not been specified
 */
private void processOptions(RepositoryState state, RevWalk rw) throws NoMessageException {
    if (committer == null) {
        committer = new PersonIdent(repo);
    }
    if (author == null && !amend) {
        author = committer;
    }
    if (allowEmpty == null) {
        // JGit allows empty commits by default. Only when pathes are
        // specified the commit should not be empty. This behaviour differs
        // from native git but can only be adapted in the next release.
        // TODO(ch) align the defaults with native git
        allowEmpty = (only.isEmpty()) ? Boolean.TRUE : Boolean.FALSE;
    }
    // when doing a merge commit parse MERGE_HEAD and MERGE_MSG files
    if (state == RepositoryState.MERGING_RESOLVED || isMergeDuringRebase(state)) {
        try {
            parents = repo.readMergeHeads();
            if (parents != null) {
                for (int i = 0; i < parents.size(); i++) {
                    RevObject ro = rw.parseAny(parents.get(i));
                    if (ro instanceof RevTag)
                        parents.set(i, rw.peel(ro));
                }
            }
        } catch (IOException e) {
            throw new JGitInternalException(MessageFormat.format(
                    JGitText.get().exceptionOccurredDuringReadingOfGIT_DIR, Constants.MERGE_HEAD, e), e);
        }
        if (message == null) {
            try {
                message = repo.readMergeCommitMsg();
            } catch (IOException e) {
                throw new JGitInternalException(MessageFormat.format(
                        JGitText.get().exceptionOccurredDuringReadingOfGIT_DIR, Constants.MERGE_MSG, e), e);
            }
        }
    } else if (state == RepositoryState.SAFE && message == null) {
        try {
            message = repo.readSquashCommitMsg();
            if (message != null) {
                repo.writeSquashCommitMsg(null /* delete */);
            }
        } catch (IOException e) {
            throw new JGitInternalException(MessageFormat
                    .format(JGitText.get().exceptionOccurredDuringReadingOfGIT_DIR, Constants.MERGE_MSG, e), e);
        }

    }
    if (message == null) {
        // as long as we don't support -C option we have to have
        // an explicit message
        throw new NoMessageException(JGitText.get().commitMessageNotSpecified);
    }
}

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

License:Open Source License

private static ObjectId peelRef(final Repository repository, ObjectId refId)
        throws MissingObjectException, IOException {
    RevWalk walker = new RevWalk(repository);
    try {/*from  ww  w .  ja v  a 2s  .co m*/
        return walker.peel(walker.parseAny(refId));
    } finally {
        if (walker != null) {
            walker.release();
        }
    }
}

From source file:org.eclipse.egit.ui.internal.history.CommitInfoBuilder.java

License:Open Source License

/**
 * Finds next door tagged revision. Searches forwards (in descendants) or
 * backwards (in ancestors)/*  w  w  w  .j ava  2  s  .  c  om*/
 *
 * @param searchDescendant
 *            if <code>false</code>, will search for tagged revision in
 *            ancestors
 * @param monitor
 * @return {@link Ref} or <code>null</code> if no tag found
 * @throws IOException
 * @throws OperationCanceledException
 */
private Ref getNextTag(boolean searchDescendant, IProgressMonitor monitor)
        throws IOException, OperationCanceledException {
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    RevWalk revWalk = new RevWalk(db);
    revWalk.setRetainBody(false);
    Map<String, Ref> tagsMap = db.getTags();
    Ref tagRef = null;

    for (Ref ref : tagsMap.values()) {
        if (monitor.isCanceled())
            throw new OperationCanceledException();
        // both RevCommits must be allocated using same RevWalk instance,
        // otherwise isMergedInto returns wrong result!
        RevCommit current = revWalk.parseCommit(commit);
        // tags can point to any object, we only want tags pointing at
        // commits
        RevObject any = revWalk.peel(revWalk.parseAny(ref.getObjectId()));
        if (!(any instanceof RevCommit))
            continue;
        RevCommit newTag = (RevCommit) any;
        if (newTag.getId().equals(commit))
            continue;

        // check if newTag matches our criteria
        if (isMergedInto(revWalk, newTag, current, searchDescendant)) {
            if (monitor.isCanceled())
                throw new OperationCanceledException();
            if (tagRef != null) {
                RevCommit oldTag = revWalk.parseCommit(tagRef.getObjectId());

                // both oldTag and newTag satisfy search criteria, so taking
                // the closest one
                if (isMergedInto(revWalk, oldTag, newTag, searchDescendant))
                    tagRef = ref;
            } else
                tagRef = ref;
        }
    }
    return tagRef;
}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.RefTreeUpdateCommand.java

License:Apache License

private static Ref toRef(final RevWalk rw, final ObjectId id, final String name, final boolean mustExist)
        throws IOException {
    if (ObjectId.zeroId().equals(id)) {
        return null;
    }/*from  w  w  w. j av  a2  s  . c om*/

    try {
        RevObject o = rw.parseAny(id);
        if (o instanceof RevTag) {
            RevObject p = rw.peel(o);
            return new ObjectIdRef.PeeledTag(NETWORK, name, id, p.copy());
        }
        return new ObjectIdRef.PeeledNonTag(NETWORK, name, id);
    } catch (MissingObjectException e) {
        if (mustExist) {
            throw e;
        }
        return new ObjectIdRef.Unpeeled(NETWORK, name, id);
    }
}