I’m trying Jgit and I want to retrieve all tags of a cloned repository and I want to sort them by Date. That’s why I’m trying the following:
String path = "path/to/my/git/repo/.git"; Repository repository = builder.setGitDir(new File(path)).readEnvironment().findGitDir().build(); try (RevWalk walk = new RevWalk(repository)) { List<Ref> taglist = new Git(repository).tagList().call(); Collections.sort(taglist, new Comparator<Ref>() { public int compare(Ref o1, Ref o2) { Date d1 = null; 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); } }); }catch (GitAPIException e){ e.printStackTrace(); } }
The problem is that some of Ref returned by this.taglist = new Git(repository).tagList().call()
are not Tag: in fact an exception is raised by parseTag()
and its message is:
org.eclipse.jgit.errors.IncorrectObjectTypeException: Object 3ea2b388b5ef02622312fe08e6935a5f9c655e00 is not a tag.
Can someone explain me why this happen and how can i solve the problem?
Thank you all.
Advertisement
Answer
As I said in the comment, i solved changing the walk.parseTag(o1.getObjectId()).getTaggerIdent().getWhen();
into walk.parseCommit(o1.getObjectId()).getAuthorIdent().getWhen();
and same for o2.
Hope this will help others.