Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JGit list remote tags and sort by creation date

Tags:

git

jgit

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 ?

like image 264
Rebse Avatar asked Oct 21 '22 15:10

Rebse


1 Answers

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).

like image 74
Magnus Bäck Avatar answered Nov 02 '22 06:11

Magnus Bäck