14

How do I merge in JGit?

Let's say I want to merge master with foo branch, how do I do this?

Tower
  • 98,741
  • 129
  • 357
  • 507

4 Answers4

8

To merge, you can use the MergeCommand (in package org.eclipse.jgit.api), after a CheckoutCommand. To provide you with an example, because indeed Jgit lacks examples:

Git git = ... // you get it through a CloneCommand, InitCommand 
              // or through the file system

CheckoutCommand coCmd = git.checkout(); 
// Commands are part of the api module, which include git-like calls
coCmd.setName("master");
coCmd.setCreateBranch(false); // probably not needed, just to make sure
coCmd.call(); // switch to "master" branch

MergeCommand mgCmd = git.merge();
mgCmd.include("foo"); // "foo" is considered as a Ref to a branch
MergeResult res = mgCmd.call(); // actually do the merge

if (res.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING)){
   System.out.println(res.getConflicts().toString());
   // inform the user he has to handle the conflicts
}

I did not try the code so it might not be perfect, but it's just to provide a start. And I didn't include the imports. Developing with JGit implies a lot of tries based on the javadoc

Vince
  • 1,570
  • 3
  • 27
  • 48
  • 4
    The class org.eclipse.jgit.api.MergeCommand that I am using, does not decalre a public method include(String). There are 3 public methods with the name "include", but none of them takes in just a String. – David Oct 25 '18 at 14:05
4

You will find in the JGit repository various test classes for Merge, including for instance the SimpleMergeTest

Merger ourMerger = MergeStrategy.OURS.newMerger(db);
boolean merge = ourMerger.merge(new ObjectId[] { db.resolve("a"), db.resolve("c") });
assertTrue(merge);
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The very same link you gave to Github states that merging is a missing feature. What's up with that? – Tower Aug 27 '12 at 10:47
  • @rFactor It could be the case if it provided only a wrapper around the `git merge` command instead of a fully native java implementation, but that seems to not be the case anymore. – VonC Aug 27 '12 at 11:05
  • are you able to confirm that? I'm not on a dev machine so I cannot figure out much at this point, or I'll just check it at some point and post here my findings. – Tower Aug 27 '12 at 11:08
  • @rFactor no I haven't tested it directly. I will look forward to read your conclusions. – VonC Aug 27 '12 at 11:10
4

Passing an ObjectId is convenient for me, e.g.

void merge(String uri,File file){
    try(Git git = Git.cloneRepository()
                     .setURI(uri)
                     .setBranch("master")
                     .setDirectory(file)
                     .call()){
        ObjectId objectId = git.getRepository().resolve("origin/develop");
        MergeResult mergeResult = git.merge().include(objectId).call();
        log.debug(mergeResult.getMergeStatus());
    } catch (GitAPIException | IOException e) {
        log.error("error",e);
    }
}

furthermore, you can find more examples here: https://github.com/centic9/jgit-cookbook

NeB Nep
  • 163
  • 12
  • 1
    Using https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/MergeChanges.java was helpful for me. One thing to realize is that the `MergeCommand` (result of `git.merge()` - a builder pattern) is not capable of specifying the author of the merge commit, so it will pick whatever is configured for the user running the program. To set the author explicitly, use `setCommit(false)` on the `MergeCommand` and then commit separately using `git.commit().setAuthor(...).call();`. – Vlad May 18 '22 at 09:25
2

JGit has a full blown Java implementation of the git resolve merge strategy since 2010. If you need examples look at the corresponding JGit test cases and have a look how EGit is using the MergeCommand, look at class org.eclipse.egit.core.op.MergeOperation.

Matthias Sohn
  • 264
  • 2
  • 1