Created
June 18, 2015 16:32
-
-
Save paulwellnerbou/67c1758055710a7eb88e to your computer and use it in GitHub Desktop.
Getting git log between commits, branches and annotated tags with JGit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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