Created
February 12, 2016 04:28
-
-
Save robinst/997da20d09a82d85a3e9 to your computer and use it in GitHub Desktop.
`git merge-base --fork-point` using JGit (not optimal)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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