Skip to content

Instantly share code, notes, and snippets.

@robinst
Created February 12, 2016 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinst/997da20d09a82d85a3e9 to your computer and use it in GitHub Desktop.
Save robinst/997da20d09a82d85a3e9 to your computer and use it in GitHub Desktop.
`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)) {
walk.parseBody(commit);
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