Skip to content

Instantly share code, notes, and snippets.

@paulwellnerbou
Created June 18, 2015 16:32
Show Gist options
  • Save paulwellnerbou/67c1758055710a7eb88e to your computer and use it in GitHub Desktop.
Save paulwellnerbou/67c1758055710a7eb88e to your computer and use it in GitHub Desktop.
Getting git log between commits, branches and annotated tags with JGit
package de.wellnerbou.gitjira.jgit;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import java.io.IOException;
public class GitLogBetween {
private final Repository repo;
private final CommitDataModelMapper commitDataModelMapper;
public GitLogBetween(final Repository repository, final CommitDataModelMapper commitDataModelMapper) {
this.repo = repository;
this.commitDataModelMapper = commitDataModelMapper;
}
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(getActualRefObjectId(refFrom), getActualRefObjectId(refTo)).call();
}
private ObjectId getActualRefObjectId(Ref ref) {
final Ref repoPeeled = repo.peel(ref);
if(repoPeeled.getPeeledObjectId() != null) {
return repoPeeled.getPeeledObjectId();
}
return ref.getObjectId();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment