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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With