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, 2021 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.CommitConfig;
  36. import org.eclipse.jgit.lib.Config.ConfigEnum;
  37. import org.eclipse.jgit.lib.Constants;
  38. import org.eclipse.jgit.lib.NullProgressMonitor;
  39. import org.eclipse.jgit.lib.ObjectId;
  40. import org.eclipse.jgit.lib.ObjectIdRef;
  41. import org.eclipse.jgit.lib.ProgressMonitor;
  42. import org.eclipse.jgit.lib.Ref;
  43. import org.eclipse.jgit.lib.Ref.Storage;
  44. import org.eclipse.jgit.lib.RefUpdate;
  45. import org.eclipse.jgit.lib.RefUpdate.Result;
  46. import org.eclipse.jgit.lib.Repository;
  47. import org.eclipse.jgit.merge.ContentMergeStrategy;
  48. import org.eclipse.jgit.merge.MergeConfig;
  49. import org.eclipse.jgit.merge.MergeMessageFormatter;
  50. import org.eclipse.jgit.merge.MergeStrategy;
  51. import org.eclipse.jgit.merge.Merger;
  52. import org.eclipse.jgit.merge.ResolveMerger;
  53. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  54. import org.eclipse.jgit.merge.SquashMessageFormatter;
  55. import org.eclipse.jgit.revwalk.RevCommit;
  56. import org.eclipse.jgit.revwalk.RevWalk;
  57. import org.eclipse.jgit.revwalk.RevWalkUtils;
  58. import org.eclipse.jgit.treewalk.FileTreeIterator;
  59. import org.eclipse.jgit.util.StringUtils;

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

  70.     private MergeStrategy mergeStrategy = MergeStrategy.RECURSIVE;

  71.     private ContentMergeStrategy contentStrategy;

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

  73.     private Boolean squash;

  74.     private FastForwardMode fastForwardMode;

  75.     private String message;

  76.     private boolean insertChangeId;

  77.     private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;

  78.     /**
  79.      * Values for the "merge.conflictStyle" git config.
  80.      *
  81.      * @since 5.12
  82.      */
  83.     public enum ConflictStyle {

  84.         /** "merge" style: only ours/theirs. This is the default. */
  85.         MERGE,

  86.         /** "diff3" style: ours/base/theirs. */
  87.         DIFF3
  88.     }

  89.     /**
  90.      * The modes available for fast forward merges corresponding to the
  91.      * <code>--ff</code>, <code>--no-ff</code> and <code>--ff-only</code>
  92.      * options under <code>branch.&lt;name&gt;.mergeoptions</code>.
  93.      */
  94.     public enum FastForwardMode implements ConfigEnum {
  95.         /**
  96.          * Corresponds to the default --ff option (for a fast forward update the
  97.          * branch pointer only).
  98.          */
  99.         FF,
  100.         /**
  101.          * Corresponds to the --no-ff option (create a merge commit even for a
  102.          * fast forward).
  103.          */
  104.         NO_FF,
  105.         /**
  106.          * Corresponds to the --ff-only option (abort unless the merge is a fast
  107.          * forward).
  108.          */
  109.         FF_ONLY;

  110.         @Override
  111.         public String toConfigValue() {
  112.             return "--" + name().toLowerCase(Locale.ROOT).replace('_', '-'); //$NON-NLS-1$
  113.         }

  114.         @Override
  115.         public boolean matchConfigValue(String in) {
  116.             if (StringUtils.isEmptyOrNull(in))
  117.                 return false;
  118.             if (!in.startsWith("--")) //$NON-NLS-1$
  119.                 return false;
  120.             return name().equalsIgnoreCase(in.substring(2).replace('-', '_'));
  121.         }

  122.         /**
  123.          * The modes available for fast forward merges corresponding to the
  124.          * options under <code>merge.ff</code>.
  125.          */
  126.         public enum Merge {
  127.             /**
  128.              * {@link FastForwardMode#FF}.
  129.              */
  130.             TRUE,
  131.             /**
  132.              * {@link FastForwardMode#NO_FF}.
  133.              */
  134.             FALSE,
  135.             /**
  136.              * {@link FastForwardMode#FF_ONLY}.
  137.              */
  138.             ONLY;

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

  158.         /**
  159.          * Map from <code>FastForwardMode.Merge</code> to
  160.          * <code>FastForwardMode</code>.
  161.          *
  162.          * @param ffMode
  163.          *            the <code>FastForwardMode.Merge</code> value to be mapped
  164.          * @return the mapped <code>FastForwardMode</code> value
  165.          */
  166.         public static FastForwardMode valueOf(FastForwardMode.Merge ffMode) {
  167.             switch (ffMode) {
  168.             case FALSE:
  169.                 return NO_FF;
  170.             case ONLY:
  171.                 return FF_ONLY;
  172.             default:
  173.                 return FF;
  174.             }
  175.         }
  176.     }

  177.     private Boolean commit;

  178.     /**
  179.      * Constructor for MergeCommand.
  180.      *
  181.      * @param repo
  182.      *            the {@link org.eclipse.jgit.lib.Repository}
  183.      */
  184.     protected MergeCommand(Repository repo) {
  185.         super(repo);
  186.     }

  187.     /**
  188.      * {@inheritDoc}
  189.      * <p>
  190.      * Execute the {@code Merge} command with all the options and parameters
  191.      * collected by the setter methods (e.g. {@link #include(Ref)}) of this
  192.      * class. Each instance of this class should only be used for one invocation
  193.      * of the command. Don't call this method twice on an instance.
  194.      */
  195.     @Override
  196.     @SuppressWarnings("boxing")
  197.     public MergeResult call() throws GitAPIException, NoHeadException,
  198.             ConcurrentRefUpdateException, CheckoutConflictException,
  199.             InvalidMergeHeadsException, WrongRepositoryStateException, NoMessageException {
  200.         checkCallable();
  201.         fallBackToConfiguration();
  202.         checkParameters();

  203.         DirCacheCheckout dco = null;
  204.         try (RevWalk revWalk = new RevWalk(repo)) {
  205.             Ref head = repo.exactRef(Constants.HEAD);
  206.             if (head == null)
  207.                 throw new NoHeadException(
  208.                         JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
  209.             StringBuilder refLogMessage = new StringBuilder("merge "); //$NON-NLS-1$

  210.             // Check for FAST_FORWARD, ALREADY_UP_TO_DATE

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

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

  214.             // handle annotated tags
  215.             ref = repo.getRefDatabase().peel(ref);
  216.             ObjectId objectId = ref.getPeeledObjectId();
  217.             if (objectId == null)
  218.                 objectId = ref.getObjectId();

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

  220.             ObjectId headId = head.getObjectId();
  221.             if (headId == null) {
  222.                 revWalk.parseHeaders(srcCommit);
  223.                 dco = new DirCacheCheckout(repo,
  224.                         repo.lockDirCache(), srcCommit.getTree());
  225.                 dco.setFailOnConflict(true);
  226.                 dco.setProgressMonitor(monitor);
  227.                 dco.checkout();
  228.                 RefUpdate refUpdate = repo
  229.                         .updateRef(head.getTarget().getName());
  230.                 refUpdate.setNewObjectId(objectId);
  231.                 refUpdate.setExpectedOldObjectId(null);
  232.                 refUpdate.setRefLogMessage("initial pull", false); //$NON-NLS-1$
  233.                 if (refUpdate.update() != Result.NEW)
  234.                     throw new NoHeadException(
  235.                             JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
  236.                 setCallable(false);
  237.                 return new MergeResult(srcCommit, srcCommit, new ObjectId[] {
  238.                         null, srcCommit }, MergeStatus.FAST_FORWARD,
  239.                         mergeStrategy, null, null);
  240.             }

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

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

  337.                     String msg = null;
  338.                     ObjectId newHeadId = null;
  339.                     MergeStatus mergeStatus = null;
  340.                     if (!commit && squash) {
  341.                         mergeStatus = MergeStatus.MERGED_SQUASHED_NOT_COMMITTED;
  342.                     }
  343.                     if (!commit && !squash) {
  344.                         mergeStatus = MergeStatus.MERGED_NOT_COMMITTED;
  345.                     }
  346.                     if (commit && !squash) {
  347.                         try (Git git = new Git(getRepository())) {
  348.                             newHeadId = git.commit()
  349.                                     .setReflogComment(refLogMessage.toString())
  350.                                     .setInsertChangeId(insertChangeId)
  351.                                     .call().getId();
  352.                         }
  353.                         mergeStatus = MergeStatus.MERGED;
  354.                         getRepository().autoGC(monitor);
  355.                     }
  356.                     if (commit && squash) {
  357.                         msg = JGitText.get().squashCommitNotUpdatingHEAD;
  358.                         newHeadId = headCommit.getId();
  359.                         mergeStatus = MergeStatus.MERGED_SQUASHED;
  360.                     }
  361.                     return new MergeResult(newHeadId, null,
  362.                             new ObjectId[] { headCommit.getId(),
  363.                                     srcCommit.getId() }, mergeStatus,
  364.                             mergeStrategy, null, msg);
  365.                 }
  366.                 if (failingPaths != null) {
  367.                     repo.writeMergeCommitMsg(null);
  368.                     repo.writeMergeHeads(null);
  369.                     return new MergeResult(null, merger.getBaseCommitId(),
  370.                             new ObjectId[] { headCommit.getId(),
  371.                                     srcCommit.getId() },
  372.                             MergeStatus.FAILED, mergeStrategy, lowLevelResults,
  373.                             failingPaths, null);
  374.                 }
  375.                 CommitConfig cfg = repo.getConfig().get(CommitConfig.KEY);
  376.                 char commentChar = cfg.getCommentChar(message);
  377.                 String mergeMessageWithConflicts = new MergeMessageFormatter()
  378.                         .formatWithConflicts(mergeMessage, unmergedPaths,
  379.                                 commentChar);
  380.                 repo.writeMergeCommitMsg(mergeMessageWithConflicts);
  381.                 return new MergeResult(null, merger.getBaseCommitId(),
  382.                         new ObjectId[] { headCommit.getId(),
  383.                                 srcCommit.getId() },
  384.                         MergeStatus.CONFLICTING, mergeStrategy, lowLevelResults,
  385.                         null);
  386.             }
  387.         } catch (org.eclipse.jgit.errors.CheckoutConflictException e) {
  388.             List<String> conflicts = (dco == null) ? Collections
  389.                     .<String> emptyList() : dco.getConflicts();
  390.             throw new CheckoutConflictException(conflicts, e);
  391.         } catch (IOException e) {
  392.             throw new JGitInternalException(
  393.                     MessageFormat.format(
  394.                             JGitText.get().exceptionCaughtDuringExecutionOfMergeCommand,
  395.                             e), e);
  396.         }
  397.     }

  398.     private void checkParameters() throws InvalidMergeHeadsException {
  399.         if (squash.booleanValue() && fastForwardMode == FastForwardMode.NO_FF) {
  400.             throw new JGitInternalException(
  401.                     JGitText.get().cannotCombineSquashWithNoff);
  402.         }

  403.         if (commits.size() != 1)
  404.             throw new InvalidMergeHeadsException(
  405.                     commits.isEmpty() ? JGitText.get().noMergeHeadSpecified
  406.                             : MessageFormat.format(
  407.                                     JGitText.get().mergeStrategyDoesNotSupportHeads,
  408.                                     mergeStrategy.getName(),
  409.                                     Integer.valueOf(commits.size())));
  410.     }

  411.     /**
  412.      * Use values from the configuration if they have not been explicitly
  413.      * defined via the setters
  414.      */
  415.     private void fallBackToConfiguration() {
  416.         MergeConfig config = MergeConfig.getConfigForCurrentBranch(repo);
  417.         if (squash == null)
  418.             squash = Boolean.valueOf(config.isSquash());
  419.         if (commit == null)
  420.             commit = Boolean.valueOf(config.isCommit());
  421.         if (fastForwardMode == null)
  422.             fastForwardMode = config.getFastForwardMode();
  423.     }

  424.     private void updateHead(StringBuilder refLogMessage, ObjectId newHeadId,
  425.             ObjectId oldHeadID) throws IOException,
  426.             ConcurrentRefUpdateException {
  427.         RefUpdate refUpdate = repo.updateRef(Constants.HEAD);
  428.         refUpdate.setNewObjectId(newHeadId);
  429.         refUpdate.setRefLogMessage(refLogMessage.toString(), false);
  430.         refUpdate.setExpectedOldObjectId(oldHeadID);
  431.         Result rc = refUpdate.update();
  432.         switch (rc) {
  433.         case NEW:
  434.         case FAST_FORWARD:
  435.             return;
  436.         case REJECTED:
  437.         case LOCK_FAILURE:
  438.             throw new ConcurrentRefUpdateException(
  439.                     JGitText.get().couldNotLockHEAD, refUpdate.getRef(), rc);
  440.         default:
  441.             throw new JGitInternalException(MessageFormat.format(
  442.                     JGitText.get().updatingRefFailed, Constants.HEAD,
  443.                     newHeadId.toString(), rc));
  444.         }
  445.     }

  446.     /**
  447.      * Set merge strategy
  448.      *
  449.      * @param mergeStrategy
  450.      *            the {@link org.eclipse.jgit.merge.MergeStrategy} to be used
  451.      * @return {@code this}
  452.      */
  453.     public MergeCommand setStrategy(MergeStrategy mergeStrategy) {
  454.         checkCallable();
  455.         this.mergeStrategy = mergeStrategy;
  456.         return this;
  457.     }

  458.     /**
  459.      * Sets the content merge strategy to use if the
  460.      * {@link #setStrategy(MergeStrategy) merge strategy} is "resolve" or
  461.      * "recursive".
  462.      *
  463.      * @param strategy
  464.      *            the {@link ContentMergeStrategy} to be used
  465.      * @return {@code this}
  466.      * @since 5.12
  467.      */
  468.     public MergeCommand setContentMergeStrategy(ContentMergeStrategy strategy) {
  469.         checkCallable();
  470.         this.contentStrategy = strategy;
  471.         return this;
  472.     }

  473.     /**
  474.      * Reference to a commit to be merged with the current head
  475.      *
  476.      * @param aCommit
  477.      *            a reference to a commit which is merged with the current head
  478.      * @return {@code this}
  479.      */
  480.     public MergeCommand include(Ref aCommit) {
  481.         checkCallable();
  482.         commits.add(aCommit);
  483.         return this;
  484.     }

  485.     /**
  486.      * Id of a commit which is to be merged with the current head
  487.      *
  488.      * @param aCommit
  489.      *            the Id of a commit which is merged with the current head
  490.      * @return {@code this}
  491.      */
  492.     public MergeCommand include(AnyObjectId aCommit) {
  493.         return include(aCommit.getName(), aCommit);
  494.     }

  495.     /**
  496.      * Include a commit
  497.      *
  498.      * @param name
  499.      *            a name of a {@code Ref} pointing to the commit
  500.      * @param aCommit
  501.      *            the Id of a commit which is merged with the current head
  502.      * @return {@code this}
  503.      */
  504.     public MergeCommand include(String name, AnyObjectId aCommit) {
  505.         return include(new ObjectIdRef.Unpeeled(Storage.LOOSE, name,
  506.                 aCommit.copy()));
  507.     }

  508.     /**
  509.      * If <code>true</code>, will prepare the next commit in working tree and
  510.      * index as if a real merge happened, but do not make the commit or move the
  511.      * HEAD. Otherwise, perform the merge and commit the result.
  512.      * <p>
  513.      * In case the merge was successful but this flag was set to
  514.      * <code>true</code> a {@link org.eclipse.jgit.api.MergeResult} with status
  515.      * {@link org.eclipse.jgit.api.MergeResult.MergeStatus#MERGED_SQUASHED} or
  516.      * {@link org.eclipse.jgit.api.MergeResult.MergeStatus#FAST_FORWARD_SQUASHED}
  517.      * is returned.
  518.      *
  519.      * @param squash
  520.      *            whether to squash commits or not
  521.      * @return {@code this}
  522.      * @since 2.0
  523.      */
  524.     public MergeCommand setSquash(boolean squash) {
  525.         checkCallable();
  526.         this.squash = Boolean.valueOf(squash);
  527.         return this;
  528.     }

  529.     /**
  530.      * Sets the fast forward mode.
  531.      *
  532.      * @param fastForwardMode
  533.      *            corresponds to the --ff/--no-ff/--ff-only options. If
  534.      *            {@code null} use the value of the {@code merge.ff} option
  535.      *            configured in git config. If this option is not configured
  536.      *            --ff is the built-in default.
  537.      * @return {@code this}
  538.      * @since 2.2
  539.      */
  540.     public MergeCommand setFastForward(
  541.             @Nullable FastForwardMode fastForwardMode) {
  542.         checkCallable();
  543.         this.fastForwardMode = fastForwardMode;
  544.         return this;
  545.     }

  546.     /**
  547.      * Controls whether the merge command should automatically commit after a
  548.      * successful merge
  549.      *
  550.      * @param commit
  551.      *            <code>true</code> if this command should commit (this is the
  552.      *            default behavior). <code>false</code> if this command should
  553.      *            not commit. In case the merge was successful but this flag was
  554.      *            set to <code>false</code> a
  555.      *            {@link org.eclipse.jgit.api.MergeResult} with type
  556.      *            {@link org.eclipse.jgit.api.MergeResult} with status
  557.      *            {@link org.eclipse.jgit.api.MergeResult.MergeStatus#MERGED_NOT_COMMITTED}
  558.      *            is returned
  559.      * @return {@code this}
  560.      * @since 3.0
  561.      */
  562.     public MergeCommand setCommit(boolean commit) {
  563.         this.commit = Boolean.valueOf(commit);
  564.         return this;
  565.     }

  566.     /**
  567.      * Set the commit message to be used for the merge commit (in case one is
  568.      * created)
  569.      *
  570.      * @param message
  571.      *            the message to be used for the merge commit
  572.      * @return {@code this}
  573.      * @since 3.5
  574.      */
  575.     public MergeCommand setMessage(String message) {
  576.         this.message = message;
  577.         return this;
  578.     }

  579.     /**
  580.      * If set to true a change id will be inserted into the commit message
  581.      *
  582.      * An existing change id is not replaced. An initial change id (I000...)
  583.      * will be replaced by the change id.
  584.      *
  585.      * @param insertChangeId
  586.      *            whether to insert a change id
  587.      * @return {@code this}
  588.      * @since 5.0
  589.      */
  590.     public MergeCommand setInsertChangeId(boolean insertChangeId) {
  591.         checkCallable();
  592.         this.insertChangeId = insertChangeId;
  593.         return this;
  594.     }

  595.     /**
  596.      * The progress monitor associated with the diff operation. By default, this
  597.      * is set to <code>NullProgressMonitor</code>
  598.      *
  599.      * @see NullProgressMonitor
  600.      * @param monitor
  601.      *            A progress monitor
  602.      * @return this instance
  603.      * @since 4.2
  604.      */
  605.     public MergeCommand setProgressMonitor(ProgressMonitor monitor) {
  606.         if (monitor == null) {
  607.             monitor = NullProgressMonitor.INSTANCE;
  608.         }
  609.         this.monitor = monitor;
  610.         return this;
  611.     }
  612. }