Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
`git merge-base --fork-point` using JGit (not optimal)
public static Optional<RevCommit> findForkPoint(Repository repository, String base, String tip) throws IOException {
try (RevWalk walk = new RevWalk(repository)) {
RevCommit tipCommit = walk.lookupCommit(repository.resolve(tip));
List<ReflogEntry> reflog = repository.getReflogReader(base).getReverseEntries();
if (reflog.isEmpty()) {
return Optional.empty();
// The `<=` is deliberate because we want to check both new and old IDs for the oldest entry
for (int i = 0; i <= reflog.size(); i++) {
ObjectId id = i < reflog.size() ? reflog.get(i).getNewId() : reflog.get(i - 1).getOldId();
RevCommit commit = walk.lookupCommit(id);
if (walk.isMergedInto(commit, tipCommit)) {
return Optional.of(commit);
return Optional.empty();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.