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

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

Introduction

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

Prototype

@NonNull
public RevTag parseTag(AnyObjectId id)
        throws MissingObjectException, IncorrectObjectTypeException, IOException 

Source Link

Document

Locate a reference to an annotated tag and immediately parse its content.

Usage

From source file:ch.sourcepond.maven.release.scm.git.GitRepository.java

License:Apache License

@Override
public ProposedTag fromRef(final Ref gitTag) throws SCMException {
    notNull(gitTag, "gitTag");

    final RevWalk walk = new RevWalk(getGit().getRepository());
    final ObjectId tagId = gitTag.getObjectId();
    JSONObject message;/*  w  ww  . j av  a  2 s. c o m*/
    try {
        final RevTag tag = walk.parseTag(tagId);
        message = (JSONObject) JSONValue.parse(tag.getFullMessage());
    } catch (final IOException e) {
        throw new SCMException(e,
                "Error while looking up tag because RevTag could not be parsed! Object-id was %s", tagId);
    } finally {
        walk.dispose();
    }
    if (message == null) {
        message = new JSONObject();
        message.put(VERSION, "0");
        message.put(BUILD_NUMBER, "0");
    }
    return new GitProposedTag(getGit(), log, gitTag, stripRefPrefix(gitTag.getName()), message,
            config.getRemoteUrlOrNull());
}

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>/*  w  w  w  . j  av a  2s . c  om*/
 * 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);/*from w  w w. j  a  v  a2 s . c om*/
    tags.put(commit2.getName(), tag2);

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

From source file:com.madgag.agit.TagViewer.java

License:Open Source License

@Override
public void onContentChanged() {
    Log.d(TAG, "updateUI called");
    tagRef = repo().getTags().get(tagName);
    if (objectSummaryView == null) {
        return;/*w ww . j ava 2s.  c o m*/
    }

    if (tagRef == null) {
        actionBar.setTitle("unknown tag");
    } else {
        ObjectId peeledObjectId = repo().peel(tagRef).getPeeledObjectId();
        ObjectId taggedId = peeledObjectId == null ? tagRef.getObjectId() : peeledObjectId;
        RevWalk revWalk = new RevWalk(repo());

        ObjectId tagId = tagRef.getObjectId();
        try {
            // objectSummaryView.setObject(revWalk.parseAny(taggedId));

            final RevObject immediateTagRefObject = revWalk.parseAny(tagId);
            repositoryScope.doWith(repo(), new Runnable() {
                public void run() {
                    objectSummaryView.setObject(immediateTagRefObject);
                }
            });

            if (immediateTagRefObject instanceof RevTag) {
                revTag = revWalk.parseTag(tagId);
                actionBar.setTitle(revTag.getTagName());
            }

        } catch (IOException e) {
            Log.e(TAG, "Couldn't get parse tag", e);
            Toast.makeText(this, "Couldn't get tag " + tagId, Toast.LENGTH_LONG).show();
        }
    }
}

From source file:com.meltmedia.cadmium.core.git.GitService.java

License:Apache License

public String getBranchName() throws Exception {
    Repository repository = git.getRepository();
    if (ObjectId.isId(repository.getFullBranch())
            && repository.getFullBranch().equals(repository.resolve("HEAD").getName())) {
        RevWalk revs = null;
        try {/* w w w  . ja v a  2  s.c  o  m*/
            log.trace("Trying to resolve tagname: {}", repository.getFullBranch());
            ObjectId tagRef = ObjectId.fromString(repository.getFullBranch());
            revs = new RevWalk(repository);
            RevCommit commit = revs.parseCommit(tagRef);
            Map<String, Ref> allTags = repository.getTags();
            for (String key : allTags.keySet()) {
                Ref ref = allTags.get(key);
                RevTag tag = revs.parseTag(ref.getObjectId());
                log.trace("Checking ref {}, {}", commit.getName(), tag.getObject());
                if (tag.getObject().equals(commit)) {
                    return key;
                }
            }
        } catch (Exception e) {
            log.warn("Invalid id: {}", repository.getFullBranch(), e);
        } finally {
            revs.release();
        }
    }
    return repository.getBranch();
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.GitLabelingSupport.java

License:Apache License

@Nullable
private RevTag getTagObject(@NotNull Repository r, @NotNull Ref tagRef) {
    ObjectId tagId = tagRef.getObjectId();
    if (tagId == null)
        return null;
    RevWalk walk = new RevWalk(r);
    try {/*from   ww w .j  a  v a2 s . co m*/
        return walk.parseTag(tagId);
    } catch (Exception e) {
        return null;
    } finally {
        walk.release();
    }
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.tests.GitLabelingSupportTest.java

License:Apache License

public void testLabels() throws Exception {
    VcsRoot root = vcsRoot().withFetchUrl(getRemoteRepositoryDir("repo.git")).build();
    // ensure that all revisions reachable from master are fetched
    buildGit().getLabelingSupport().label("test_label", "2276eaf76a658f96b5cf3eb25f3e1fda90f6b653", root,
            CheckoutRules.DEFAULT);/*from   www.  j  a v  a 2 s .c om*/
    Repository r = new RepositoryBuilder().setGitDir(getRemoteRepositoryDir("repo.git")).build();
    RevWalk revWalk = new RevWalk(r);
    try {
        Ref tagRef = r.getTags().get("test_label");
        RevTag t = revWalk.parseTag(tagRef.getObjectId());
        assertEquals(t.getObject().name(), "2276eaf76a658f96b5cf3eb25f3e1fda90f6b653");
    } finally {
        r.close();
    }
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.tests.GitLabelingSupportTest.java

License:Apache License

@Test
public void tag_with_specified_username() throws Exception {
    VcsRoot root = vcsRoot().withFetchUrl(GitUtils.toURL(getRemoteRepositoryDir("repo.git")))
            .withUsernameForTags("John Doe <john.doe@some.org>").build();
    buildGit().getLabelingSupport().label("label_with_specified_username",
            "465ad9f630e451b9f2b782ffb09804c6a98c4bb9", root, CheckoutRules.DEFAULT);

    Repository r = new RepositoryBuilder().setGitDir(getRemoteRepositoryDir("repo.git")).build();
    RevWalk revWalk = new RevWalk(r);
    try {/*from  ww  w  .j a  v  a2s  .  c om*/
        Ref tagRef = r.getTags().get("label_with_specified_username");
        RevTag t = revWalk.parseTag(tagRef.getObjectId());
        PersonIdent tagger = t.getTaggerIdent();
        assertEquals(tagger.getName(), "John Doe");
        assertEquals(tagger.getEmailAddress(), "john.doe@some.org");
    } finally {
        revWalk.release();
        r.close();
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public Tag tagCreate(TagCreateRequest request) throws GitException {
    String commit = request.getCommit();
    if (commit == null) {
        commit = Constants.HEAD;//from   ww w  . j a  v a 2s .c  o m
    }

    try {
        RevWalk revWalk = new RevWalk(repository);
        RevObject revObject;
        try {
            revObject = revWalk.parseAny(repository.resolve(commit));
        } finally {
            revWalk.close();
        }

        TagCommand tagCommand = getGit().tag().setName(request.getName()).setObjectId(revObject)
                .setMessage(request.getMessage()).setForceUpdate(request.isForce());

        GitUser tagger = getUser();
        if (tagger != null) {
            tagCommand.setTagger(new PersonIdent(tagger.getName(), tagger.getEmail()));
        }

        Ref revTagRef = tagCommand.call();
        RevTag revTag = revWalk.parseTag(revTagRef.getLeaf().getObjectId());
        return newDto(Tag.class).withName(revTag.getTagName());
    } catch (IOException | GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.egit.core.RepositoryUtil.java

License:Open Source License

/**
 * Tries to map a commit to a symbolic reference.
 * <p>//from w w w  . j av  a  2  s. com
 * This value will be cached for the given commit ID unless refresh is
 * specified. The return value will be the full name, e.g.
 * "refs/remotes/someBranch", "refs/tags/v.1.0"
 * <p>
 * Since this mapping is not unique, the following precedence rules are
 * used:
 * <ul>
 * <li>Tags take precedence over branches</li>
 * <li>Local branches take preference over remote branches</li>
 * <li>Newer references take precedence over older ones where time stamps
 * are available</li>
 * <li>If there are still ambiguities, the reference name with the highest
 * lexicographic value will be returned</li>
 * </ul>
 *
 * @param repository
 *            the {@link Repository}
 * @param commitId
 *            a commit
 * @param refresh
 *            if true, the cache will be invalidated
 * @return the symbolic reference, or <code>null</code> if no such reference
 *         can be found
 */
public String mapCommitToRef(Repository repository, String commitId, boolean refresh) {
    synchronized (commitMappingCache) {

        if (!ObjectId.isId(commitId)) {
            return null;
        }

        Map<String, String> cacheEntry = commitMappingCache.get(repository.getDirectory().toString());
        if (!refresh && cacheEntry != null && cacheEntry.containsKey(commitId)) {
            // this may be null in fact
            return cacheEntry.get(commitId);
        }
        if (cacheEntry == null) {
            cacheEntry = new HashMap<String, String>();
            commitMappingCache.put(repository.getDirectory().getPath(), cacheEntry);
        } else {
            cacheEntry.clear();
        }

        Map<String, Date> tagMap = new HashMap<String, Date>();
        try {
            RevWalk rw = new RevWalk(repository);
            Map<String, Ref> tags = repository.getRefDatabase().getRefs(Constants.R_TAGS);
            for (Ref tagRef : tags.values()) {
                RevTag tag = rw.parseTag(repository.resolve(tagRef.getName()));
                if (tag.getObject().name().equals(commitId)) {
                    Date timestamp;
                    if (tag.getTaggerIdent() != null) {
                        timestamp = tag.getTaggerIdent().getWhen();
                    } else {
                        timestamp = null;
                    }
                    tagMap.put(tagRef.getName(), timestamp);
                }
            }
        } catch (IOException e) {
            // ignore here
        }

        String cacheValue = null;

        if (!tagMap.isEmpty()) {
            // we try to obtain the "latest" tag
            Date compareDate = new Date(0);
            for (Map.Entry<String, Date> tagEntry : tagMap.entrySet()) {
                if (tagEntry.getValue() != null && tagEntry.getValue().after(compareDate)) {
                    compareDate = tagEntry.getValue();
                    cacheValue = tagEntry.getKey();
                }
            }
            // if we don't have time stamps, we sort
            if (cacheValue == null) {
                String compareString = ""; //$NON-NLS-1$
                for (String tagName : tagMap.keySet()) {
                    if (tagName.compareTo(compareString) >= 0) {
                        cacheValue = tagName;
                        compareString = tagName;
                    }
                }
            }
        }

        if (cacheValue == null) {
            // we didnt't find a tag, so let's look for local branches
            Set<String> branchNames = new TreeSet<String>();
            // put this into a sorted set
            try {
                Map<String, Ref> remoteBranches = repository.getRefDatabase().getRefs(Constants.R_HEADS);
                for (Ref branch : remoteBranches.values()) {
                    if (branch.getObjectId().name().equals(commitId)) {
                        branchNames.add(branch.getName());
                    }
                }
            } catch (IOException e) {
                // ignore here
            }
            if (!branchNames.isEmpty()) {
                // get the last (sorted) entry
                cacheValue = branchNames.toArray(new String[branchNames.size()])[branchNames.size() - 1];
            }
        }

        if (cacheValue == null) {
            // last try: remote branches
            Set<String> branchNames = new TreeSet<String>();
            // put this into a sorted set
            try {
                Map<String, Ref> remoteBranches = repository.getRefDatabase().getRefs(Constants.R_REMOTES);
                for (Ref branch : remoteBranches.values()) {
                    if (branch.getObjectId().name().equals(commitId)) {
                        branchNames.add(branch.getName());
                    }
                }
                if (!branchNames.isEmpty()) {
                    // get the last (sorted) entry
                    cacheValue = branchNames.toArray(new String[branchNames.size()])[branchNames.size() - 1];
                }
            } catch (IOException e) {
                // ignore here
            }
        }
        cacheEntry.put(commitId, cacheValue);
        return cacheValue;
    }
}