I'm trying to find commits on branch foo
that aren't on master
. I have this working for the simple cases, but once new changes have been merged from master
into foo
it breaks down.
In more detail:
- If you have commits A, B, and C on
master
- then you create branch
foo
and add commits D and E there - then you create commit F on
master
- then you merge F from
master
intofoo
(using a merge commit)
now trying to get the merge base returns F
instead of C
. So I can't iterate over commits from C
to foo
's head (giving C
, D
, E
, F
, which I'll then narrow down to D
& E
using isMergedInto
)
public RevCommit findMergeBase(ObjectId foo, ObjectId bar) throws MissingObjectException,
IncorrectObjectTypeException, IOException {
RevWalk walk = null;
try {
walk = new RevWalk(repo);
RevCommit fooHead = walk.parseCommit(foo);
RevCommit barHead = walk.parseCommit(bar);
walk.setRevFilter(RevFilter.MERGE_BASE);
walk.markStart(fooHead);
walk.markStart(barHead);
RevCommit base = null;
while (true) {
RevCommit commit = walk.next();
if (commit == null)
break;
base = commit;
}
return base;
}
finally {
if (walk != null) {
walk.reset();
walk.close();
walk.dispose();
}
}
}