JGit: Can I find out whether or not a particular tag exists in a Git repository without checking it out?

DP_ :

I need to write a piece of code in Java which tests whether or not a particular tag exists in a Git repository.

Most obvious way to do it it this:

git = Git.cloneRepository()
        .setURI(gitUri)
        .setDirectory(dir)
        .call();
git.checkout().setName(String.format("refs/tags/%s", version)).call();

If tag version does not exist, an exception will be thrown.

But this way requires me to have a directory (dir) into which the repository will be checked out.

Is it possible to find out whether or not a tag exists in a remote repository without checking it out on disk? If yes, how can I do it?

Rüdiger Herrmann :

The LsRemoteCommand is able to list the refs that a remote repository has.

The command can be configured to include tags like this:

LsRemoteCommand ls = Git.lsRemoteRepository();
Collection<Ref> remoteRefs = ls
    .setRemote("url-to-remote-repo")
    .setHeads(true) // true by default, set to false if not interested in refs/heads/*
    .setTags(true)  // include tags in result
    .call();

If you prefer to get a map of ref-names to Ref objects, you can execute the command with callAsMap().

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=10679&siteId=1