I need to list the tags of a remote Git repository and sort them by creation date via JGit 3.2.0 api.

Didn't find a way with lsremote, so I have only sort by name:

System.out.println("Listing remote repository " + REMOTE_URL);
Collection<Ref> tags = Git.lsRemoteRepository()
    .setTags(true)
    .setRemote(REMOTE_URL)
    .call();

ArrayList<Ref> taglist = new ArrayList<>(tags);
Collections.sort(taglist, new Comparator<Ref>()
{
  public int compare(Ref o1, Ref o2) {
   return o1.getName().compareTo(o2.getName());
 }
});

for (Ref ref : taglist) {
  System.out.println("Ref: " + ref.getName());
  System.out.println("ObjectId : " + ref.getObjectId());
  System.out.println("Ref short: " + Repository.shortenRefName(ref.getName()));
  }
}

How to sort the tags by creation date?

It works with a local repository, f.e. after cloning a repository:

// open a cloned repository
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File(localPath + "/.git"))
  .readEnvironment()
  .findGitDir()
  .build();

final RevWalk walk = new RevWalk(repository);
List<Ref> call = new Git(repository).tagList().call();
RevTag rt;

Collections.sort(call, new Comparator<Ref>()
{
  public int compare(Ref o1, Ref o2)
  {
    java.util.Date d1 = null;
    java.util.Date d2 = null;
    try
    {
      d1 = walk.parseTag(o1.getObjectId()).getTaggerIdent().getWhen();
      d2 = walk.parseTag(o2.getObjectId()).getTaggerIdent().getWhen();

    } catch (IOException e)
    {
      e.printStackTrace();
    }
    return d1.compareTo(d2);
  }
});

Is there any other way without having to clone a repository first ?

有帮助吗?

解决方案

No, not possible. The ls-remote interface doesn't expose the creation time of tags. You'll have to clone the Git (or at least fetch all its tags, which in most cases will be pretty much equivalent to cloning the git).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top