JGit Find Merge Base [message #1781943] |
Wed, 14 February 2018 20:54 |
Ricky Veach Messages: 5 Registered: April 2011 |
Junior Member |
|
|
I am trying to find the common ancestor between any 2 commits, also known as the merge base.
I tried using code like the one found at https://stackoverflow.com/a/26436588/1016482 , but it seems to fail in a specific situation.
If I give it 2 branches, where one branch has diverged with many new commits, it returns the correct merge base commit.
However, if I give it a commit and the commit's parent, it doesn't return the commit's parent as the merge base. Instead it returns the commit 2 parents from that.
Example:
Code:
public static RevCommit getCommonAncestor(Repository repository, RevCommit commit1,
RevCommit commit2) {
RevWalk walk = null;
try {
walk = new RevWalk(repository);
walk.setRevFilter(RevFilter.MERGE_BASE);
walk.markStart(commit1);
walk.markStart(commit2);
final RevCommit base = walk.next();
if ((base != null) && (walk.next() == null)) {
return base;
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (walk != null) {
walk.reset();
walk.close();
walk.dispose();
}
}
return null;
}
Repository:
Head -> A -> B -> C -> D
So Commit A is the parent of Head and so on.
Expected Result of `getCommonAncestor(repository, CommitA, HEAD)` should be commit A.
What is actually returned is commit C.
Is there something I am doing wrong? Can I not use merge base in this circumstance or is this a bug?
I am using `org.eclipse.jgit` version `4.10.0.201712302008-r`.
[Updated on: Wed, 14 February 2018 22:07] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02890 seconds