Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jgit-dev] How to get commits betwee two commits


in our application there's a method that tries to get all commits between two commits (the same as git log since..until from command line). It seems that there's no method óin the JGit API that easily supports this so we implemented it for ourselves. It's a very simple method: we create a RevWalk starting from the first commit and go through the tree until we find the second commit. Something like this:

boolean inRange = false;
boolean isZeroOldId = oldId.equals(ObjectId.zeroId());
boolean reachedOld = false;
for (RevCommit next : walk) {
if (isZeroOldId) {
// oldId == 0000.. means we have a new ref
// In this case we take only commits reachable only from one ref (our new ref)
if (next.getParentCount() > 1 || isMergedToAnotherHead(next, headId, localRepository)) {
} else {
if (headId.toObjectId().equals(next.getId()) || (next.getCommitTime() >= oldTime && next.getCommitTime() <= headTime) 
&& !oldId.toObjectId().equals(next.getId()) || include(oldCommit, next)) {
inRange = true;
} else if (oldId.toObjectId().equals(next.getId())) {
inRange = false;
reachedOld = true;
} else if (inRange) {
However, we had many problems with this approach. My question is if there's an easier way to implement this?

Thanks in advance,
Ákos Tajti

Back to the top