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>
  5.  * and other copyright owners as documented in the project's IP log.
  6.  *
  7.  * This program and the accompanying materials are made available
  8.  * under the terms of the Eclipse Distribution License v1.0 which
  9.  * accompanies this distribution, is reproduced below, and is
  10.  * available at http://www.eclipse.org/org/documents/edl-v10.php
  11.  *
  12.  * All rights reserved.
  13.  *
  14.  * Redistribution and use in source and binary forms, with or
  15.  * without modification, are permitted provided that the following
  16.  * conditions are met:
  17.  *
  18.  * - Redistributions of source code must retain the above copyright
  19.  *   notice, this list of conditions and the following disclaimer.
  20.  *
  21.  * - Redistributions in binary form must reproduce the above
  22.  *   copyright notice, this list of conditions and the following
  23.  *   disclaimer in the documentation and/or other materials provided
  24.  *   with the distribution.
  25.  *
  26.  * - Neither the name of the Eclipse Foundation, Inc. nor the
  27.  *   names of its contributors may be used to endorse or promote
  28.  *   products derived from this software without specific prior
  29.  *   written permission.
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32.  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.  */
  45. package org.eclipse.jgit.api;

  46. import java.io.IOException;
  47. import java.text.MessageFormat;
  48. import java.util.Arrays;
  49. import java.util.Collections;
  50. import java.util.LinkedList;
  51. import java.util.List;
  52. import java.util.Locale;
  53. import java.util.Map;

  54. import org.eclipse.jgit.annotations.Nullable;
  55. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  56. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  57. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  58. import org.eclipse.jgit.api.errors.GitAPIException;
  59. import org.eclipse.jgit.api.errors.InvalidMergeHeadsException;
  60. import org.eclipse.jgit.api.errors.JGitInternalException;
  61. import org.eclipse.jgit.api.errors.NoHeadException;
  62. import org.eclipse.jgit.api.errors.NoMessageException;
  63. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  64. import org.eclipse.jgit.dircache.DirCacheCheckout;
  65. import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
  66. import org.eclipse.jgit.internal.JGitText;
  67. import org.eclipse.jgit.lib.AnyObjectId;
  68. import org.eclipse.jgit.lib.Config.ConfigEnum;
  69. import org.eclipse.jgit.lib.Constants;
  70. import org.eclipse.jgit.lib.NullProgressMonitor;
  71. import org.eclipse.jgit.lib.ObjectId;
  72. import org.eclipse.jgit.lib.ObjectIdRef;
  73. import org.eclipse.jgit.lib.ProgressMonitor;
  74. import org.eclipse.jgit.lib.Ref;
  75. import org.eclipse.jgit.lib.Ref.Storage;
  76. import org.eclipse.jgit.lib.RefUpdate;
  77. import org.eclipse.jgit.lib.RefUpdate.Result;
  78. import org.eclipse.jgit.lib.Repository;
  79. import org.eclipse.jgit.merge.MergeConfig;
  80. import org.eclipse.jgit.merge.MergeMessageFormatter;
  81. import org.eclipse.jgit.merge.MergeStrategy;
  82. import org.eclipse.jgit.merge.Merger;
  83. import org.eclipse.jgit.merge.ResolveMerger;
  84. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  85. import org.eclipse.jgit.merge.SquashMessageFormatter;
  86. import org.eclipse.jgit.revwalk.RevCommit;
  87. import org.eclipse.jgit.revwalk.RevWalk;
  88. import org.eclipse.jgit.revwalk.RevWalkUtils;
  89. import org.eclipse.jgit.treewalk.FileTreeIterator;
  90. import org.eclipse.jgit.util.StringUtils;

  91. /**
  92.  * A class used to execute a {@code Merge} command. It has setters for all
  93.  * supported options and arguments of this command and a {@link #call()} method
  94.  * to finally execute the command. Each instance of this class should only be
  95.  * used for one invocation of the command (means: one call to {@link #call()})
  96.  *
  97.  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-merge.html"
  98.  *      >Git documentation about Merge</a>
  99.  */
  100. public class MergeCommand extends GitCommand<MergeResult> {

  101.     private MergeStrategy mergeStrategy = MergeStrategy.RECURSIVE;

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

  103.     private Boolean squash;

  104.     private FastForwardMode fastForwardMode;

  105.     private String message;

  106.     private boolean insertChangeId;

  107.     private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;

  108.     /**
  109.      * The modes available for fast forward merges corresponding to the
  110.      * <code>--ff</code>, <code>--no-ff</code> and <code>--ff-only</code>
  111.      * options under <code>branch.&lt;name&gt;.mergeoptions</code>.
  112.      */
  113.     public enum FastForwardMode implements ConfigEnum {
  114.         /**
  115.          * Corresponds to the default --ff option (for a fast forward update the
  116.          * branch pointer only).
  117.          */
  118.         FF,
  119.         /**
  120.          * Corresponds to the --no-ff option (create a merge commit even for a
  121.          * fast forward).
  122.          */
  123.         NO_FF,
  124.         /**
  125.          * Corresponds to the --ff-only option (abort unless the merge is a fast
  126.          * forward).
  127.          */
  128.         FF_ONLY;

  129.         @Override
  130.         public String toConfigValue() {
  131.             return "--" + name().toLowerCase(Locale.ROOT).replace('_', '-'); //$NON-NLS-1$
  132.         }

  133.         @Override
  134.         public boolean matchConfigValue(String in) {
  135.             if (StringUtils.isEmptyOrNull(in))
  136.                 return false;
  137.             if (!in.startsWith("--")) //$NON-NLS-1$
  138.                 return false;
  139.             return name().equalsIgnoreCase(in.substring(2).replace('-', '_'));
  140.         }

  141.         /**
  142.          * The modes available for fast forward merges corresponding to the
  143.          * options under <code>merge.ff</code>.
  144.          */
  145.         public enum Merge {
  146.             /**
  147.              * {@link FastForwardMode#FF}.
  148.              */
  149.             TRUE,
  150.             /**
  151.              * {@link FastForwardMode#NO_FF}.
  152.              */
  153.             FALSE,
  154.             /**
  155.              * {@link FastForwardMode#FF_ONLY}.
  156.              */
  157.             ONLY;

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

  177.         /**
  178.          * Map from <code>FastForwardMode.Merge</code> to
  179.          * <code>FastForwardMode</code>.
  180.          *
  181.          * @param ffMode
  182.          *            the <code>FastForwardMode.Merge</code> value to be mapped
  183.          * @return the mapped <code>FastForwardMode</code> value
  184.          */
  185.         public static FastForwardMode valueOf(FastForwardMode.Merge ffMode) {
  186.             switch (ffMode) {
  187.             case FALSE:
  188.                 return NO_FF;
  189.             case ONLY:
  190.                 return FF_ONLY;
  191.             default:
  192.                 return FF;
  193.             }
  194.         }
  195.     }

  196.     private Boolean commit;

  197.     /**
  198.      * Constructor for MergeCommand.
  199.      *
  200.      * @param repo
  201.      *            the {@link org.eclipse.jgit.lib.Repository}
  202.      */
  203.     protected MergeCommand(Repository repo) {
  204.         super(repo);
  205.     }

  206.     /**
  207.      * {@inheritDoc}
  208.      * <p>
  209.      * Execute the {@code Merge} command with all the options and parameters
  210.      * collected by the setter methods (e.g. {@link #include(Ref)}) of this
  211.      * class. Each instance of this class should only be used for one invocation
  212.      * of the command. Don't call this method twice on an instance.
  213.      */
  214.     @Override
  215.     @SuppressWarnings("boxing")
  216.     public MergeResult call() throws GitAPIException, NoHeadException,
  217.             ConcurrentRefUpdateException, CheckoutConflictException,
  218.             InvalidMergeHeadsException, WrongRepositoryStateException, NoMessageException {
  219.         checkCallable();
  220.         fallBackToConfiguration();
  221.         checkParameters();

  222.         DirCacheCheckout dco = null;
  223.         try (RevWalk revWalk = new RevWalk(repo)) {
  224.             Ref head = repo.exactRef(Constants.HEAD);
  225.             if (head == null)
  226.                 throw new NoHeadException(
  227.                         JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
  228.             StringBuilder refLogMessage = new StringBuilder("merge "); //$NON-NLS-1$

  229.             // Check for FAST_FORWARD, ALREADY_UP_TO_DATE

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

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

  233.             // handle annotated tags
  234.             ref = repo.getRefDatabase().peel(ref);
  235.             ObjectId objectId = ref.getPeeledObjectId();
  236.             if (objectId == null)
  237.                 objectId = ref.getObjectId();

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

  239.             ObjectId headId = head.getObjectId();
  240.             if (headId == null) {
  241.                 revWalk.parseHeaders(srcCommit);
  242.                 dco = new DirCacheCheckout(repo,
  243.                         repo.lockDirCache(), srcCommit.getTree());
  244.                 dco.setFailOnConflict(true);
  245.                 dco.setProgressMonitor(monitor);
  246.                 dco.checkout();
  247.                 RefUpdate refUpdate = repo
  248.                         .updateRef(head.getTarget().getName());
  249.                 refUpdate.setNewObjectId(objectId);
  250.                 refUpdate.setExpectedOldObjectId(null);
  251.                 refUpdate.setRefLogMessage("initial pull", false); //$NON-NLS-1$
  252.                 if (refUpdate.update() != Result.NEW)
  253.                     throw new NoHeadException(
  254.                             JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
  255.                 setCallable(false);
  256.                 return new MergeResult(srcCommit, srcCommit, new ObjectId[] {
  257.                         null, srcCommit }, MergeStatus.FAST_FORWARD,
  258.                         mergeStrategy, null, null);
  259.             }

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

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

  355.                     String msg = null;
  356.                     ObjectId newHeadId = null;
  357.                     MergeStatus mergeStatus = null;
  358.                     if (!commit && squash) {
  359.                         mergeStatus = MergeStatus.MERGED_SQUASHED_NOT_COMMITTED;
  360.                     }
  361.                     if (!commit && !squash) {
  362.                         mergeStatus = MergeStatus.MERGED_NOT_COMMITTED;
  363.                     }
  364.                     if (commit && !squash) {
  365.                         try (Git git = new Git(getRepository())) {
  366.                             newHeadId = git.commit()
  367.                                     .setReflogComment(refLogMessage.toString())
  368.                                     .setInsertChangeId(insertChangeId)
  369.                                     .call().getId();
  370.                         }
  371.                         mergeStatus = MergeStatus.MERGED;
  372.                         getRepository().autoGC(monitor);
  373.                     }
  374.                     if (commit && squash) {
  375.                         msg = JGitText.get().squashCommitNotUpdatingHEAD;
  376.                         newHeadId = headCommit.getId();
  377.                         mergeStatus = MergeStatus.MERGED_SQUASHED;
  378.                     }
  379.                     return new MergeResult(newHeadId, null,
  380.                             new ObjectId[] { headCommit.getId(),
  381.                                     srcCommit.getId() }, mergeStatus,
  382.                             mergeStrategy, null, msg);
  383.                 } else {
  384.                     if (failingPaths != null) {
  385.                         repo.writeMergeCommitMsg(null);
  386.                         repo.writeMergeHeads(null);
  387.                         return new MergeResult(null, merger.getBaseCommitId(),
  388.                                 new ObjectId[] {
  389.                                         headCommit.getId(), srcCommit.getId() },
  390.                                 MergeStatus.FAILED, mergeStrategy,
  391.                                 lowLevelResults, failingPaths, null);
  392.                     } else {
  393.                         String mergeMessageWithConflicts = new MergeMessageFormatter()
  394.                                 .formatWithConflicts(mergeMessage,
  395.                                         unmergedPaths);
  396.                         repo.writeMergeCommitMsg(mergeMessageWithConflicts);
  397.                         return new MergeResult(null, merger.getBaseCommitId(),
  398.                                 new ObjectId[] { headCommit.getId(),
  399.                                         srcCommit.getId() },
  400.                                 MergeStatus.CONFLICTING, mergeStrategy,
  401.                                 lowLevelResults, null);
  402.                     }
  403.                 }
  404.             }
  405.         } catch (org.eclipse.jgit.errors.CheckoutConflictException e) {
  406.             List<String> conflicts = (dco == null) ? Collections
  407.                     .<String> emptyList() : dco.getConflicts();
  408.             throw new CheckoutConflictException(conflicts, e);
  409.         } catch (IOException e) {
  410.             throw new JGitInternalException(
  411.                     MessageFormat.format(
  412.                             JGitText.get().exceptionCaughtDuringExecutionOfMergeCommand,
  413.                             e), e);
  414.         }
  415.     }

  416.     private void checkParameters() throws InvalidMergeHeadsException {
  417.         if (squash.booleanValue() && fastForwardMode == FastForwardMode.NO_FF) {
  418.             throw new JGitInternalException(
  419.                     JGitText.get().cannotCombineSquashWithNoff);
  420.         }

  421.         if (commits.size() != 1)
  422.             throw new InvalidMergeHeadsException(
  423.                     commits.isEmpty() ? JGitText.get().noMergeHeadSpecified
  424.                             : MessageFormat.format(
  425.                                     JGitText.get().mergeStrategyDoesNotSupportHeads,
  426.                                     mergeStrategy.getName(),
  427.                                     Integer.valueOf(commits.size())));
  428.     }

  429.     /**
  430.      * Use values from the configuation if they have not been explicitly defined
  431.      * via the setters
  432.      */
  433.     private void fallBackToConfiguration() {
  434.         MergeConfig config = MergeConfig.getConfigForCurrentBranch(repo);
  435.         if (squash == null)
  436.             squash = Boolean.valueOf(config.isSquash());
  437.         if (commit == null)
  438.             commit = Boolean.valueOf(config.isCommit());
  439.         if (fastForwardMode == null)
  440.             fastForwardMode = config.getFastForwardMode();
  441.     }

  442.     private void updateHead(StringBuilder refLogMessage, ObjectId newHeadId,
  443.             ObjectId oldHeadID) throws IOException,
  444.             ConcurrentRefUpdateException {
  445.         RefUpdate refUpdate = repo.updateRef(Constants.HEAD);
  446.         refUpdate.setNewObjectId(newHeadId);
  447.         refUpdate.setRefLogMessage(refLogMessage.toString(), false);
  448.         refUpdate.setExpectedOldObjectId(oldHeadID);
  449.         Result rc = refUpdate.update();
  450.         switch (rc) {
  451.         case NEW:
  452.         case FAST_FORWARD:
  453.             return;
  454.         case REJECTED:
  455.         case LOCK_FAILURE:
  456.             throw new ConcurrentRefUpdateException(
  457.                     JGitText.get().couldNotLockHEAD, refUpdate.getRef(), rc);
  458.         default:
  459.             throw new JGitInternalException(MessageFormat.format(
  460.                     JGitText.get().updatingRefFailed, Constants.HEAD,
  461.                     newHeadId.toString(), rc));
  462.         }
  463.     }

  464.     /**
  465.      * Set merge strategy
  466.      *
  467.      * @param mergeStrategy
  468.      *            the {@link org.eclipse.jgit.merge.MergeStrategy} to be used
  469.      * @return {@code this}
  470.      */
  471.     public MergeCommand setStrategy(MergeStrategy mergeStrategy) {
  472.         checkCallable();
  473.         this.mergeStrategy = mergeStrategy;
  474.         return this;
  475.     }

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

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

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

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

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

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

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

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

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