You can use one of the ThreeWayMerger
s to determine if two commits can be merged.
By default JGit's MergeCommand
uses the recursive merge strategy. Therefore you would probably want to use this merger.
Make sure to create an in core merger (set the second parameter of newMerger
to true
). While in this mode, the merger does not touch the work directory.
ThreeWayMerger merger = MergeStrategy.RECURSIVE.newMerger(repository, true);
boolean canMerge = merger.merge(headCommit, commitToMerge);
The merge method returns true
if the given commits can be merged and false
otherwise.
The base commit can either be explicitly set with setBase
or the common ancestor is used.
The ResolveMerger
and RecursiveMerger
also provide methods to query which file(s) cannot be merged.