Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » JGit Find Merge Base
JGit Find Merge Base [message #1781943] Wed, 14 February 2018 20:54 Go to next message
Ricky Veach is currently offline Ricky VeachFriend
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

Re: JGit Find Merge Base [message #1781945 is a reply to message #1781943] Wed, 14 February 2018 22:51 Go to previous messageGo to next message
Ricky Veach is currently offline Ricky VeachFriend
Messages: 5
Registered: April 2011
Junior Member
I don't know why, but the method works correctly when I re-parse the commits again before trying the merge base.
			walk = new RevWalk(git.getRepository());

			commit1 = walk.parseCommit(commit1);
			commit2 = walk.parseCommit(commit2);


The only thing I did with the commits outside the method was retrieve them via another method.

	public static RevCommit getCommit(Repository repository, String commitId) {
		RevCommit result = null;
		RevWalk walk = null;

		try {
			final ObjectId commitObject = repository.resolve(commitId);

			if (commitObject != null) {
				walk = new RevWalk(repository);

				result = walk.parseCommit(commitObject);
			}
		}
		catch (RevisionSyntaxException | IOException e) {
			e.printStackTrace();
		}
		finally {
			if (walk != null) {
				walk.close();
				walk.dispose();
			}
		}

		return result;
	}


Can any one explain if I am doing something wrong that would require this extra code?
Re: JGit Find Merge Base [message #1782580 is a reply to message #1781945] Mon, 26 February 2018 16:50 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1032
Registered: July 2009
Senior Member
JGit only knows the objectId of objects which weren't parsed yet. Only when you parse them jgit gets access to the object's data
Re: JGit Find Merge Base [message #1782581 is a reply to message #1782580] Mon, 26 February 2018 16:53 Go to previous message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1032
Registered: July 2009
Senior Member
RevCommits need to be produced by the same RevWalk instance otherwise you can't compare them
Previous Topic:EGit auto capitalize branch names
Next Topic:JGit DiffCommand result is different from command line Git
Goto Forum:
  


Current Time: Thu Sep 20 08:29:07 GMT 2018

Powered by FUDForum. Page generated in 0.02487 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top