6

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.

G. Sylvie Davies
  • 5,049
  • 3
  • 21
  • 30
  • I see you're new to SO. If you feel an answer solved the problem, please mark it as 'accepted' by clicking the green check mark. This helps keep the focus on older posts which still don't have answers. – Rüdiger Herrmann Apr 04 '16 at 13:40
  • I understand that @RüdigerHerrmann, but thanks for the advice. I just did't have seen your answer yet. I've tried something similar that i found in other SO question, which mentioned the JGit test classes. It seems that going by this path I could get what I want. I edited my question, so I present the closest I've got from my goal. – Alcemir Santos Apr 05 '16 at 14:17
  • Please do **first** search for solutions before [asking questions](http://stackoverflow.com/help/how-to-ask) whose answer you don't need so not to waste others time. Your edit is actually a different question. Please create a new post if you have a different quesstion. – Rüdiger Herrmann Apr 05 '16 at 14:45
  • Actually, don't. It is the same thing. It was there since the beginning that my goal is the access the conflicting lines of a conflicting merge, not to know wether a merge result in conflict. Maybe the head is a bit misleading, but the body I think it was quite clear. I am sorry if lose your time. Thanks anyway! – Alcemir Santos Apr 06 '16 at 13:40
  • Just to clarify my previous comment: the first part of your post asks how to _get the conflicts prior to the actual merge_. The part after EDIT is about _doing the actual merge_ and why it _executes only the first time_. For me that's a different thing altogther and therefore should be a separate question. – Rüdiger Herrmann Apr 06 '16 at 15:13
  • OK. Accepted your answer and posted another question. – Alcemir Santos Apr 06 '16 at 19:14

1 Answers1

5

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.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79