Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get conflicts before merge with JGit?

I am trying to perform with JGit all the merge scenarios from a project that had conflicts.

Is it possible to get the conflicts prior to the actual merge? In other words, is it possible to simulate a merge with JGit?

My goal is to access the conflicting lines of each file for all the merge scenarios of a project.

like image 861
Alcemir Santos Avatar asked Apr 02 '16 10:04

Alcemir Santos


Video Answer


1 Answers

You can use one of the ThreeWayMergers 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.

like image 62
Rüdiger Herrmann Avatar answered Sep 20 '22 15:09

Rüdiger Herrmann