Getting commits between annotated tags with JGit
JGit does not support getting commits between annotated tags (or any revision and an annotated tag) as you might expect. Following code will work perfectly with commit hashs and branch identifiers:
public Iterable<RevCommit> getJGitLogBetween(final String rev1, final String rev2) throws IOException, GitAPIException {
Ref refFrom = repo.getRef(rev1);
Ref refTo = repo.getRef(rev2);
return new Git(repo).log().addRange(refFrom.getObjectId(), refTo.getObjectId()).call();
}
Calling this with an annotated tag (as the maven release plugin is creating, for example):
@Test
public void testGetGitLogBetweenAnnotatedTagAndHead() throws Exception {
Repository repository = RepositoryHelper.openMyRepository();
GitLogBetween gitLogBetween = new GitLogBetween(repository, new CommitDataModelMapper());
final Iterable<RevCommit> res = gitLogBetween.getJGitLogBetween(ANNOTATED_TEST_TAG, "HEAD");
assertThat(res).isNotNull();
}
Will throw an exception like:
Object 0df488d824422dcfe52273d3035772412b5b4818 is not a commit.
org.eclipse.jgit.errors.IncorrectObjectTypeException: Object 0df488d824422dcfe52273d3035772412b5b4818 is not a commit.
If you want this to work with annotated tags as well, the revision has to be 'unpeeled' before. This code will work, for branches, commits and all kind of tags.
See this forum post, where I got this information.
I stumbled across this issue writing a library to automatically create changelogs for Jira based on the git history. It is still in early development, READMEs and more information will be provided soon and plugins for Jenkins and Jira are in development.