MergeCommand.java

  1. /*
  2.  * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  3.  * Copyright (C) 2010-2014, Stefan Lay <stefan.lay@sap.com>
  4.  * Copyright (C) 2016, Laurent Delaigue <laurent.delaigue@obeo.fr> and others
  5.  *
  6.  * This program and the accompanying materials are made available under the
  7.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  8.  * https://www.eclipse.org/org/documents/edl-v10.php.
  9.  *
  10.  * SPDX-License-Identifier: BSD-3-Clause
  11.  */
  12. package org.eclipse.jgit.api;

  13. import java.io.IOException;
  14. import java.text.MessageFormat;
  15. import java.util.Arrays;
  16. import java.util.Collections;
  17. import java.util.LinkedList;
  18. import java.util.List;
  19. import java.util.Locale;
  20. import java.util.Map;

  21. import org.eclipse.jgit.annotations.Nullable;
  22. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  23. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  24. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  25. import org.eclipse.jgit.api.errors.GitAPIException;
  26. import org.eclipse.jgit.api.errors.InvalidMergeHeadsException;
  27. import org.eclipse.jgit.api.errors.JGitInternalException;
  28. import org.eclipse.jgit.api.errors.NoHeadException;
  29. import org.eclipse.jgit.api.errors.NoMessageException;
  30. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  31. import org.eclipse.jgit.dircache.DirCacheCheckout;
  32. import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
  33. import org.eclipse.jgit.internal.JGitText;
  34. import org.eclipse.jgit.lib.AnyObjectId;
  35. import org.eclipse.jgit.lib.Config.ConfigEnum;
  36. import org.eclipse.jgit.lib.Constants;
  37. import org.eclipse.jgit.lib.NullProgressMonitor;
  38. import org.eclipse.jgit.lib.ObjectId;
  39. import org.eclipse.jgit.lib.ObjectIdRef;
  40. import org.eclipse.jgit.lib.ProgressMonitor;
  41. import org.eclipse.jgit.lib.Ref;
  42. import org.eclipse.jgit.lib.Ref.Storage;
  43. import org.eclipse.jgit.lib.RefUpdate;
  44. import org.eclipse.jgit.lib.RefUpdate.Result;
  45. import org.eclipse.jgit.lib.Repository;
  46. import org.eclipse.jgit.merge.MergeConfig;
  47. import org.eclipse.jgit.merge.MergeMessageFormatter;
  48. import org.eclipse.jgit.merge.MergeStrategy;
  49. import org.eclipse.jgit.merge.Merger;
  50. import org.eclipse.jgit.merge.ResolveMerger;
  51. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  52. import org.eclipse.jgit.merge.SquashMessageFormatter;
  53. import org.eclipse.jgit.revwalk.RevCommit;
  54. import org.eclipse.jgit.revwalk.RevWalk;
  55. import org.eclipse.jgit.revwalk.RevWalkUtils;
  56. import org.eclipse.jgit.treewalk.FileTreeIterator;
  57. import org.eclipse.jgit.util.StringUtils;

  58. /**
  59.  * A class used to execute a {@code Merge} command. It has setters for all
  60.  * supported options and arguments of this command and a {@link #call()} method
  61.  * to finally execute the command. Each instance of this class should only be
  62.  * used for one invocation of the command (means: one call to {@link #call()})
  63.  *
  64.  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-merge.html"
  65.  *      >Git documentation about Merge</a>
  66.  */
  67. public class MergeCommand extends GitCommand<MergeResult> {

  68.     private MergeStrategy mergeStrategy = MergeStrategy.RECURSIVE;

  69.     private List<Ref> commits = new LinkedList<>();

  70.     private Boolean squash;

  71.     private FastForwardMode fastForwardMode;

  72.     private String message;

  73.     private boolean insertChangeId;

  74.     private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;

  75.     /**
  76.      * The modes available for fast forward merges corresponding to the
  77.      * <code>--ff</code>, <code>--no-ff</code> and <code>--ff-only</code>
  78.      * options under <code>branch.&lt;name&gt;.mergeoptions</code>.
  79.      */
  80.     public enum FastForwardMode implements ConfigEnum {
  81.         /**
  82.          * Corresponds to the default --ff option (for a fast forward update the
  83.          * branch pointer only).
  84.          */
  85.         FF,
  86.         /**
  87.          * Corresponds to the --no-ff option (create a merge commit even for a
  88.          * fast forward).
  89.          */
  90.         NO_FF,
  91.         /**
  92.          * Corresponds to the --ff-only option (abort unless the merge is a fast
  93.          * forward).
  94.          */
  95.         FF_ONLY;

  96.         @Override
  97.         public String toConfigValue() {
  98.             return "--" + name().toLowerCase(Locale.ROOT).replace('_', '-'); //$NON-NLS-1$
  99.         }

  100.         @Override
  101.         public boolean matchConfigValue(String in) {
  102.             if (StringUtils.isEmptyOrNull(in))
  103.                 return false;
  104.             if (!in.startsWith("--")) //$NON-NLS-1$
  105.                 return false;
  106.             return name().equalsIgnoreCase(in.substring(2).replace('-', '_'));
  107.         }

  108.         /**
  109.          * The modes available for fast forward merges corresponding to the
  110.          * options under <code>merge.ff</code>.
  111.          */
  112.         public enum Merge {
  113.             /**
  114.              * {@link FastForwardMode#FF}.
  115.              */
  116.             TRUE,
  117.             /**
  118.              * {@link FastForwardMode#NO_FF}.
  119.              */
  120.             FALSE,
  121.             /**
  122.              * {@link FastForwardMode#FF_ONLY}.
  123.              */
  124.             ONLY;

  125.             /**
  126.              * Map from <code>FastForwardMode</code> to
  127.              * <code>FastForwardMode.Merge</code>.
  128.              *
  129.              * @param ffMode
  130.              *            the <code>FastForwardMode</code> value to be mapped
  131.              * @return the mapped <code>FastForwardMode.Merge</code> value
  132.              */
  133.             public static Merge valueOf(FastForwardMode ffMode) {
  134.                 switch (ffMode) {
  135.                 case NO_FF:
  136.                     return FALSE;
  137.                 case FF_ONLY:
  138.                     return ONLY;
  139.                 default:
  140.                     return TRUE;
  141.                 }
  142.             }
  143.         }

  144.         /**
  145.          * Map from <code>FastForwardMode.Merge</code> to
  146.          * <code>FastForwardMode</code>.
  147.          *
  148.          * @param ffMode
  149.          *            the <code>FastForwardMode.Merge</code> value to be mapped
  150.          * @return the mapped <code>FastForwardMode</code> value
  151.          */
  152.         public static FastForwardMode valueOf(FastForwardMode.Merge ffMode) {
  153.             switch (ffMode) {
  154.             case FALSE:
  155.                 return NO_FF;
  156.             case ONLY:
  157.                 return FF_ONLY;
  158.             default:
  159.                 return FF;
  160.             }
  161.         }
  162.     }

  163.     private Boolean commit;

  164.     /**
  165.      * Constructor for MergeCommand.
  166.      *
  167.      * @param repo
  168.      *            the {@link org.eclipse.jgit.lib.Repository}
  169.      */
  170.     protected MergeCommand(Repository repo) {
  171.         super(repo);
  172.     }

  173.     /**
  174.      * {@inheritDoc}
  175.      * <p>
  176.      * Execute the {@code Merge} command with all the options and parameters
  177.      * collected by the setter methods (e.g. {@link #include(Ref)}) of this
  178.      * class. Each instance of this class should only be used for one invocation
  179.      * of the command. Don't call this method twice on an instance.
  180.      */
  181.     @Override
  182.     @SuppressWarnings("boxing")
  183.     public MergeResult call() throws GitAPIException, NoHeadException,
  184.             ConcurrentRefUpdateException, CheckoutConflictException,
  185.             InvalidMergeHeadsException, WrongRepositoryStateException, NoMessageException {
  186.         checkCallable();
  187.         fallBackToConfiguration();
  188.         checkParameters();

  189.         DirCacheCheckout dco = null;
  190.         try (RevWalk revWalk = new RevWalk(repo)) {
  191.             Ref head = repo.exactRef(Constants.HEAD);
  192.             if (head == null)
  193.                 throw new NoHeadException(
  194.                         JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
  195.             StringBuilder refLogMessage = new StringBuilder("merge "); //$NON-NLS-1$

  196.             // Check for FAST_FORWARD, ALREADY_UP_TO_DATE

  197.             // we know for now there is only one commit
  198.             Ref ref = commits.get(0);

  199.             refLogMessage.append(ref.getName());

  200.             // handle annotated tags
  201.             ref = repo.getRefDatabase().peel(ref);
  202.             ObjectId objectId = ref.getPeeledObjectId();
  203.             if (objectId == null)
  204.                 objectId = ref.getObjectId();

  205.             RevCommit srcCommit = revWalk.lookupCommit(objectId);

  206.             ObjectId headId = head.getObjectId();
  207.             if (headId == null) {
  208.                 revWalk.parseHeaders(srcCommit);
  209.                 dco = new DirCacheCheckout(repo,
  210.                         repo.lockDirCache(), srcCommit.getTree());
  211.                 dco.setFailOnConflict(true);
  212.                 dco.setProgressMonitor(monitor);
  213.                 dco.checkout();
  214.                 RefUpdate refUpdate = repo
  215.                         .updateRef(head.getTarget().getName());
  216.                 refUpdate.setNewObjectId(objectId);
  217.                 refUpdate.setExpectedOldObjectId(null);
  218.                 refUpdate.setRefLogMessage("initial pull", false); //$NON-NLS-1$
  219.                 if (refUpdate.update() != Result.NEW)
  220.                     throw new NoHeadException(
  221.                             JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
  222.                 setCallable(false);
  223.                 return new MergeResult(srcCommit, srcCommit, new ObjectId[] {
  224.                         null, srcCommit }, MergeStatus.FAST_FORWARD,
  225.                         mergeStrategy, null, null);
  226.             }

  227.             RevCommit headCommit = revWalk.lookupCommit(headId);

  228.             if (revWalk.isMergedInto(srcCommit, headCommit)) {
  229.                 setCallable(false);
  230.                 return new MergeResult(headCommit, srcCommit, new ObjectId[] {
  231.                         headCommit, srcCommit },
  232.                         MergeStatus.ALREADY_UP_TO_DATE, mergeStrategy, null, null);
  233.             } else if (revWalk.isMergedInto(headCommit, srcCommit)
  234.                     && fastForwardMode != FastForwardMode.NO_FF) {
  235.                 // FAST_FORWARD detected: skip doing a real merge but only
  236.                 // update HEAD
  237.                 refLogMessage.append(": " + MergeStatus.FAST_FORWARD); //$NON-NLS-1$
  238.                 dco = new DirCacheCheckout(repo,
  239.                         headCommit.getTree(), repo.lockDirCache(),
  240.                         srcCommit.getTree());
  241.                 dco.setProgressMonitor(monitor);
  242.                 dco.setFailOnConflict(true);
  243.                 dco.checkout();
  244.                 String msg = null;
  245.                 ObjectId newHead, base = null;
  246.                 MergeStatus mergeStatus = null;
  247.                 if (!squash) {
  248.                     updateHead(refLogMessage, srcCommit, headId);
  249.                     newHead = base = srcCommit;
  250.                     mergeStatus = MergeStatus.FAST_FORWARD;
  251.                 } else {
  252.                     msg = JGitText.get().squashCommitNotUpdatingHEAD;
  253.                     newHead = base = headId;
  254.                     mergeStatus = MergeStatus.FAST_FORWARD_SQUASHED;
  255.                     List<RevCommit> squashedCommits = RevWalkUtils.find(
  256.                             revWalk, srcCommit, headCommit);
  257.                     String squashMessage = new SquashMessageFormatter().format(
  258.                             squashedCommits, head);
  259.                     repo.writeSquashCommitMsg(squashMessage);
  260.                 }
  261.                 setCallable(false);
  262.                 return new MergeResult(newHead, base, new ObjectId[] {
  263.                         headCommit, srcCommit }, mergeStatus, mergeStrategy,
  264.                         null, msg);
  265.             } else {
  266.                 if (fastForwardMode == FastForwardMode.FF_ONLY) {
  267.                     return new MergeResult(headCommit, srcCommit,
  268.                             new ObjectId[] { headCommit, srcCommit },
  269.                             MergeStatus.ABORTED, mergeStrategy, null, null);
  270.                 }
  271.                 String mergeMessage = ""; //$NON-NLS-1$
  272.                 if (!squash) {
  273.                     if (message != null)
  274.                         mergeMessage = message;
  275.                     else
  276.                         mergeMessage = new MergeMessageFormatter().format(
  277.                             commits, head);
  278.                     repo.writeMergeCommitMsg(mergeMessage);
  279.                     repo.writeMergeHeads(Arrays.asList(ref.getObjectId()));
  280.                 } else {
  281.                     List<RevCommit> squashedCommits = RevWalkUtils.find(
  282.                             revWalk, srcCommit, headCommit);
  283.                     String squashMessage = new SquashMessageFormatter().format(
  284.                             squashedCommits, head);
  285.                     repo.writeSquashCommitMsg(squashMessage);
  286.                 }
  287.                 Merger merger = mergeStrategy.newMerger(repo);
  288.                 merger.setProgressMonitor(monitor);
  289.                 boolean noProblems;
  290.                 Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults = null;
  291.                 Map<String, MergeFailureReason> failingPaths = null;
  292.                 List<String> unmergedPaths = null;
  293.                 if (merger instanceof ResolveMerger) {
  294.                     ResolveMerger resolveMerger = (ResolveMerger) merger;
  295.                     resolveMerger.setCommitNames(new String[] {
  296.                             "BASE", "HEAD", ref.getName() }); //$NON-NLS-1$ //$NON-NLS-2$
  297.                     resolveMerger.setWorkingTreeIterator(new FileTreeIterator(repo));
  298.                     noProblems = merger.merge(headCommit, srcCommit);
  299.                     lowLevelResults = resolveMerger
  300.                             .getMergeResults();
  301.                     failingPaths = resolveMerger.getFailingPaths();
  302.                     unmergedPaths = resolveMerger.getUnmergedPaths();
  303.                     if (!resolveMerger.getModifiedFiles().isEmpty()) {
  304.                         repo.fireEvent(new WorkingTreeModifiedEvent(
  305.                                 resolveMerger.getModifiedFiles(), null));
  306.                     }
  307.                 } else
  308.                     noProblems = merger.merge(headCommit, srcCommit);
  309.                 refLogMessage.append(": Merge made by "); //$NON-NLS-1$
  310.                 if (!revWalk.isMergedInto(headCommit, srcCommit))
  311.                     refLogMessage.append(mergeStrategy.getName());
  312.                 else
  313.                     refLogMessage.append("recursive"); //$NON-NLS-1$
  314.                 refLogMessage.append('.');
  315.                 if (noProblems) {
  316.                     dco = new DirCacheCheckout(repo,
  317.                             headCommit.getTree(), repo.lockDirCache(),
  318.                             merger.getResultTreeId());
  319.                     dco.setFailOnConflict(true);
  320.                     dco.setProgressMonitor(monitor);
  321.                     dco.checkout();

  322.                     String msg = null;
  323.                     ObjectId newHeadId = null;
  324.                     MergeStatus mergeStatus = null;
  325.                     if (!commit && squash) {
  326.                         mergeStatus = MergeStatus.MERGED_SQUASHED_NOT_COMMITTED;
  327.                     }
  328.                     if (!commit && !squash) {
  329.                         mergeStatus = MergeStatus.MERGED_NOT_COMMITTED;
  330.                     }
  331.                     if (commit && !squash) {
  332.                         try (Git git = new Git(getRepository())) {
  333.                             newHeadId = git.commit()
  334.                                     .setReflogComment(refLogMessage.toString())
  335.                                     .setInsertChangeId(insertChangeId)
  336.                                     .call().getId();
  337.                         }
  338.                         mergeStatus = MergeStatus.MERGED;
  339.                         getRepository().autoGC(monitor);
  340.                     }
  341.                     if (commit && squash) {
  342.                         msg = JGitText.get().squashCommitNotUpdatingHEAD;
  343.                         newHeadId = headCommit.getId();
  344.                         mergeStatus = MergeStatus.MERGED_SQUASHED;
  345.                     }
  346.                     return new MergeResult(newHeadId, null,
  347.                             new ObjectId[] { headCommit.getId(),
  348.                                     srcCommit.getId() }, mergeStatus,
  349.                             mergeStrategy, null, msg);
  350.                 }
  351.                 if (failingPaths != null) {
  352.                     repo.writeMergeCommitMsg(null);
  353.                     repo.writeMergeHeads(null);
  354.                     return new MergeResult(null, merger.getBaseCommitId(),
  355.                             new ObjectId[] { headCommit.getId(),
  356.                                     srcCommit.getId() },
  357.                             MergeStatus.FAILED, mergeStrategy, lowLevelResults,
  358.                             failingPaths, null);
  359.                 }
  360.                 String mergeMessageWithConflicts = new MergeMessageFormatter()
  361.                         .formatWithConflicts(mergeMessage, unmergedPaths);
  362.                 repo.writeMergeCommitMsg(mergeMessageWithConflicts);
  363.                 return new MergeResult(null, merger.getBaseCommitId(),
  364.                         new ObjectId[] { headCommit.getId(),
  365.                                 srcCommit.getId() },
  366.                         MergeStatus.CONFLICTING, mergeStrategy, lowLevelResults,
  367.                         null);
  368.             }
  369.         } catch (org.eclipse.jgit.errors.CheckoutConflictException e) {
  370.             List<String> conflicts = (dco == null) ? Collections
  371.                     .<String> emptyList() : dco.getConflicts();
  372.             throw new CheckoutConflictException(conflicts, e);
  373.         } catch (IOException e) {
  374.             throw new JGitInternalException(
  375.                     MessageFormat.format(
  376.                             JGitText.get().exceptionCaughtDuringExecutionOfMergeCommand,
  377.                             e), e);
  378.         }
  379.     }

  380.     private void checkParameters() throws InvalidMergeHeadsException {
  381.         if (squash.booleanValue() && fastForwardMode == FastForwardMode.NO_FF) {
  382.             throw new JGitInternalException(
  383.                     JGitText.get().cannotCombineSquashWithNoff);
  384.         }

  385.         if (commits.size() != 1)
  386.             throw new InvalidMergeHeadsException(
  387.                     commits.isEmpty() ? JGitText.get().noMergeHeadSpecified
  388.                             : MessageFormat.format(
  389.                                     JGitText.get().mergeStrategyDoesNotSupportHeads,
  390.                                     mergeStrategy.getName(),
  391.                                     Integer.valueOf(commits.size())));
  392.     }

  393.     /**
  394.      * Use values from the configuration if they have not been explicitly
  395.      * defined via the setters
  396.      */
  397.     private void fallBackToConfiguration() {
  398.         MergeConfig config = MergeConfig.getConfigForCurrentBranch(repo);
  399.         if (squash == null)
  400.             squash = Boolean.valueOf(config.isSquash());
  401.         if (commit == null)
  402.             commit = Boolean.valueOf(config.isCommit());
  403.         if (fastForwardMode == null)
  404.             fastForwardMode = config.getFastForwardMode();
  405.     }

  406.     private void updateHead(StringBuilder refLogMessage, ObjectId newHeadId,
  407.             ObjectId oldHeadID) throws IOException,
  408.             ConcurrentRefUpdateException {
  409.         RefUpdate refUpdate = repo.updateRef(Constants.HEAD);
  410.         refUpdate.setNewObjectId(newHeadId);
  411.         refUpdate.setRefLogMessage(refLogMessage.toString(), false);
  412.         refUpdate.setExpectedOldObjectId(oldHeadID);
  413.         Result rc = refUpdate.update();
  414.         switch (rc) {
  415.         case NEW:
  416.         case FAST_FORWARD:
  417.             return;
  418.         case REJECTED:
  419.         case LOCK_FAILURE:
  420.             throw new ConcurrentRefUpdateException(
  421.                     JGitText.get().couldNotLockHEAD, refUpdate.getRef(), rc);
  422.         default:
  423.             throw new JGitInternalException(MessageFormat.format(
  424.                     JGitText.get().updatingRefFailed, Constants.HEAD,
  425.                     newHeadId.toString(), rc));
  426.         }
  427.     }

  428.     /**
  429.      * Set merge strategy
  430.      *
  431.      * @param mergeStrategy
  432.      *            the {@link org.eclipse.jgit.merge.MergeStrategy} to be used
  433.      * @return {@code this}
  434.      */
  435.     public MergeCommand setStrategy(MergeStrategy mergeStrategy) {
  436.         checkCallable();
  437.         this.mergeStrategy = mergeStrategy;
  438.         return this;
  439.     }

  440.     /**
  441.      * Reference to a commit to be merged with the current head
  442.      *
  443.      * @param aCommit
  444.      *            a reference to a commit which is merged with the current head
  445.      * @return {@code this}
  446.      */
  447.     public MergeCommand include(Ref aCommit) {
  448.         checkCallable();
  449.         commits.add(aCommit);
  450.         return this;
  451.     }

  452.     /**
  453.      * Id of a commit which is to be merged with the current head
  454.      *
  455.      * @param aCommit
  456.      *            the Id of a commit which is merged with the current head
  457.      * @return {@code this}
  458.      */
  459.     public MergeCommand include(AnyObjectId aCommit) {
  460.         return include(aCommit.getName(), aCommit);
  461.     }

  462.     /**
  463.      * Include a commit
  464.      *
  465.      * @param name
  466.      *            a name of a {@code Ref} pointing to the commit
  467.      * @param aCommit
  468.      *            the Id of a commit which is merged with the current head
  469.      * @return {@code this}
  470.      */
  471.     public MergeCommand include(String name, AnyObjectId aCommit) {
  472.         return include(new ObjectIdRef.Unpeeled(Storage.LOOSE, name,
  473.                 aCommit.copy()));
  474.     }

  475.     /**
  476.      * If <code>true</code>, will prepare the next commit in working tree and
  477.      * index as if a real merge happened, but do not make the commit or move the
  478.      * HEAD. Otherwise, perform the merge and commit the result.
  479.      * <p>
  480.      * In case the merge was successful but this flag was set to
  481.      * <code>true</code> a {@link org.eclipse.jgit.api.MergeResult} with status
  482.      * {@link org.eclipse.jgit.api.MergeResult.MergeStatus#MERGED_SQUASHED} or
  483.      * {@link org.eclipse.jgit.api.MergeResult.MergeStatus#FAST_FORWARD_SQUASHED}
  484.      * is returned.
  485.      *
  486.      * @param squash
  487.      *            whether to squash commits or not
  488.      * @return {@code this}
  489.      * @since 2.0
  490.      */
  491.     public MergeCommand setSquash(boolean squash) {
  492.         checkCallable();
  493.         this.squash = Boolean.valueOf(squash);
  494.         return this;
  495.     }

  496.     /**
  497.      * Sets the fast forward mode.
  498.      *
  499.      * @param fastForwardMode
  500.      *            corresponds to the --ff/--no-ff/--ff-only options. If
  501.      *            {@code null} use the value of the {@code merge.ff} option
  502.      *            configured in git config. If this option is not configured
  503.      *            --ff is the built-in default.
  504.      * @return {@code this}
  505.      * @since 2.2
  506.      */
  507.     public MergeCommand setFastForward(
  508.             @Nullable FastForwardMode fastForwardMode) {
  509.         checkCallable();
  510.         this.fastForwardMode = fastForwardMode;
  511.         return this;
  512.     }

  513.     /**
  514.      * Controls whether the merge command should automatically commit after a
  515.      * successful merge
  516.      *
  517.      * @param commit
  518.      *            <code>true</code> if this command should commit (this is the
  519.      *            default behavior). <code>false</code> if this command should
  520.      *            not commit. In case the merge was successful but this flag was
  521.      *            set to <code>false</code> a
  522.      *            {@link org.eclipse.jgit.api.MergeResult} with type
  523.      *            {@link org.eclipse.jgit.api.MergeResult} with status
  524.      *            {@link org.eclipse.jgit.api.MergeResult.MergeStatus#MERGED_NOT_COMMITTED}
  525.      *            is returned
  526.      * @return {@code this}
  527.      * @since 3.0
  528.      */
  529.     public MergeCommand setCommit(boolean commit) {
  530.         this.commit = Boolean.valueOf(commit);
  531.         return this;
  532.     }

  533.     /**
  534.      * Set the commit message to be used for the merge commit (in case one is
  535.      * created)
  536.      *
  537.      * @param message
  538.      *            the message to be used for the merge commit
  539.      * @return {@code this}
  540.      * @since 3.5
  541.      */
  542.     public MergeCommand setMessage(String message) {
  543.         this.message = message;
  544.         return this;
  545.     }

  546.     /**
  547.      * If set to true a change id will be inserted into the commit message
  548.      *
  549.      * An existing change id is not replaced. An initial change id (I000...)
  550.      * will be replaced by the change id.
  551.      *
  552.      * @param insertChangeId
  553.      *            whether to insert a change id
  554.      * @return {@code this}
  555.      * @since 5.0
  556.      */
  557.     public MergeCommand setInsertChangeId(boolean insertChangeId) {
  558.         checkCallable();
  559.         this.insertChangeId = insertChangeId;
  560.         return this;
  561.     }

  562.     /**
  563.      * The progress monitor associated with the diff operation. By default, this
  564.      * is set to <code>NullProgressMonitor</code>
  565.      *
  566.      * @see NullProgressMonitor
  567.      * @param monitor
  568.      *            A progress monitor
  569.      * @return this instance
  570.      * @since 4.2
  571.      */
  572.     public MergeCommand setProgressMonitor(ProgressMonitor monitor) {
  573.         if (monitor == null) {
  574.             monitor = NullProgressMonitor.INSTANCE;
  575.         }
  576.         this.monitor = monitor;
  577.         return this;
  578.     }
  579. }