You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MergeCommand.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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.Map;
  53. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  54. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  55. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  56. import org.eclipse.jgit.api.errors.GitAPIException;
  57. import org.eclipse.jgit.api.errors.InvalidMergeHeadsException;
  58. import org.eclipse.jgit.api.errors.JGitInternalException;
  59. import org.eclipse.jgit.api.errors.NoHeadException;
  60. import org.eclipse.jgit.api.errors.NoMessageException;
  61. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  62. import org.eclipse.jgit.dircache.DirCacheCheckout;
  63. import org.eclipse.jgit.internal.JGitText;
  64. import org.eclipse.jgit.lib.AnyObjectId;
  65. import org.eclipse.jgit.lib.Config.ConfigEnum;
  66. import org.eclipse.jgit.lib.Constants;
  67. import org.eclipse.jgit.lib.NullProgressMonitor;
  68. import org.eclipse.jgit.lib.ObjectId;
  69. import org.eclipse.jgit.lib.ObjectIdRef;
  70. import org.eclipse.jgit.lib.ProgressMonitor;
  71. import org.eclipse.jgit.lib.Ref;
  72. import org.eclipse.jgit.lib.Ref.Storage;
  73. import org.eclipse.jgit.lib.RefUpdate;
  74. import org.eclipse.jgit.lib.RefUpdate.Result;
  75. import org.eclipse.jgit.lib.Repository;
  76. import org.eclipse.jgit.merge.MergeConfig;
  77. import org.eclipse.jgit.merge.MergeMessageFormatter;
  78. import org.eclipse.jgit.merge.MergeStrategy;
  79. import org.eclipse.jgit.merge.Merger;
  80. import org.eclipse.jgit.merge.ResolveMerger;
  81. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  82. import org.eclipse.jgit.merge.SquashMessageFormatter;
  83. import org.eclipse.jgit.revwalk.RevCommit;
  84. import org.eclipse.jgit.revwalk.RevWalk;
  85. import org.eclipse.jgit.revwalk.RevWalkUtils;
  86. import org.eclipse.jgit.treewalk.FileTreeIterator;
  87. import org.eclipse.jgit.util.StringUtils;
  88. /**
  89. * A class used to execute a {@code Merge} command. It has setters for all
  90. * supported options and arguments of this command and a {@link #call()} method
  91. * to finally execute the command. Each instance of this class should only be
  92. * used for one invocation of the command (means: one call to {@link #call()})
  93. *
  94. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-merge.html"
  95. * >Git documentation about Merge</a>
  96. */
  97. public class MergeCommand extends GitCommand<MergeResult> {
  98. private MergeStrategy mergeStrategy = MergeStrategy.RECURSIVE;
  99. private List<Ref> commits = new LinkedList<Ref>();
  100. private Boolean squash;
  101. private FastForwardMode fastForwardMode;
  102. private String message;
  103. private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
  104. /**
  105. * The modes available for fast forward merges corresponding to the
  106. * <code>--ff</code>, <code>--no-ff</code> and <code>--ff-only</code>
  107. * options under <code>branch.&lt;name&gt;.mergeoptions</code>.
  108. */
  109. public enum FastForwardMode implements ConfigEnum {
  110. /**
  111. * Corresponds to the default --ff option (for a fast forward update the
  112. * branch pointer only).
  113. */
  114. FF,
  115. /**
  116. * Corresponds to the --no-ff option (create a merge commit even for a
  117. * fast forward).
  118. */
  119. NO_FF,
  120. /**
  121. * Corresponds to the --ff-only option (abort unless the merge is a fast
  122. * forward).
  123. */
  124. FF_ONLY;
  125. public String toConfigValue() {
  126. return "--" + name().toLowerCase().replace('_', '-'); //$NON-NLS-1$
  127. }
  128. public boolean matchConfigValue(String in) {
  129. if (StringUtils.isEmptyOrNull(in))
  130. return false;
  131. if (!in.startsWith("--")) //$NON-NLS-1$
  132. return false;
  133. return name().equalsIgnoreCase(in.substring(2).replace('-', '_'));
  134. }
  135. /**
  136. * The modes available for fast forward merges corresponding to the
  137. * options under <code>merge.ff</code>.
  138. */
  139. public enum Merge {
  140. /**
  141. * {@link FastForwardMode#FF}.
  142. */
  143. TRUE,
  144. /**
  145. * {@link FastForwardMode#NO_FF}.
  146. */
  147. FALSE,
  148. /**
  149. * {@link FastForwardMode#FF_ONLY}.
  150. */
  151. ONLY;
  152. /**
  153. * Map from <code>FastForwardMode</code> to
  154. * <code>FastForwardMode.Merge</code>.
  155. *
  156. * @param ffMode
  157. * the <code>FastForwardMode</code> value to be mapped
  158. * @return the mapped <code>FastForwardMode.Merge</code> value
  159. */
  160. public static Merge valueOf(FastForwardMode ffMode) {
  161. switch (ffMode) {
  162. case NO_FF:
  163. return FALSE;
  164. case FF_ONLY:
  165. return ONLY;
  166. default:
  167. return TRUE;
  168. }
  169. }
  170. }
  171. /**
  172. * Map from <code>FastForwardMode.Merge</code> to
  173. * <code>FastForwardMode</code>.
  174. *
  175. * @param ffMode
  176. * the <code>FastForwardMode.Merge</code> value to be mapped
  177. * @return the mapped <code>FastForwardMode</code> value
  178. */
  179. public static FastForwardMode valueOf(FastForwardMode.Merge ffMode) {
  180. switch (ffMode) {
  181. case FALSE:
  182. return NO_FF;
  183. case ONLY:
  184. return FF_ONLY;
  185. default:
  186. return FF;
  187. }
  188. }
  189. }
  190. private Boolean commit;
  191. /**
  192. * @param repo
  193. */
  194. protected MergeCommand(Repository repo) {
  195. super(repo);
  196. }
  197. /**
  198. * Executes the {@code Merge} command with all the options and parameters
  199. * collected by the setter methods (e.g. {@link #include(Ref)}) of this
  200. * class. Each instance of this class should only be used for one invocation
  201. * of the command. Don't call this method twice on an instance.
  202. *
  203. * @return the result of the merge
  204. */
  205. @SuppressWarnings("boxing")
  206. public MergeResult call() throws GitAPIException, NoHeadException,
  207. ConcurrentRefUpdateException, CheckoutConflictException,
  208. InvalidMergeHeadsException, WrongRepositoryStateException, NoMessageException {
  209. checkCallable();
  210. fallBackToConfiguration();
  211. checkParameters();
  212. RevWalk revWalk = null;
  213. DirCacheCheckout dco = null;
  214. try {
  215. Ref head = repo.exactRef(Constants.HEAD);
  216. if (head == null)
  217. throw new NoHeadException(
  218. JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
  219. StringBuilder refLogMessage = new StringBuilder("merge "); //$NON-NLS-1$
  220. // Check for FAST_FORWARD, ALREADY_UP_TO_DATE
  221. revWalk = new RevWalk(repo);
  222. // we know for now there is only one commit
  223. Ref ref = commits.get(0);
  224. refLogMessage.append(ref.getName());
  225. // handle annotated tags
  226. ref = repo.peel(ref);
  227. ObjectId objectId = ref.getPeeledObjectId();
  228. if (objectId == null)
  229. objectId = ref.getObjectId();
  230. RevCommit srcCommit = revWalk.lookupCommit(objectId);
  231. ObjectId headId = head.getObjectId();
  232. if (headId == null) {
  233. revWalk.parseHeaders(srcCommit);
  234. dco = new DirCacheCheckout(repo,
  235. repo.lockDirCache(), srcCommit.getTree());
  236. dco.setFailOnConflict(true);
  237. dco.checkout();
  238. RefUpdate refUpdate = repo
  239. .updateRef(head.getTarget().getName());
  240. refUpdate.setNewObjectId(objectId);
  241. refUpdate.setExpectedOldObjectId(null);
  242. refUpdate.setRefLogMessage("initial pull", false); //$NON-NLS-1$
  243. if (refUpdate.update() != Result.NEW)
  244. throw new NoHeadException(
  245. JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
  246. setCallable(false);
  247. return new MergeResult(srcCommit, srcCommit, new ObjectId[] {
  248. null, srcCommit }, MergeStatus.FAST_FORWARD,
  249. mergeStrategy, null, null);
  250. }
  251. RevCommit headCommit = revWalk.lookupCommit(headId);
  252. if (revWalk.isMergedInto(srcCommit, headCommit)) {
  253. setCallable(false);
  254. return new MergeResult(headCommit, srcCommit, new ObjectId[] {
  255. headCommit, srcCommit },
  256. MergeStatus.ALREADY_UP_TO_DATE, mergeStrategy, null, null);
  257. } else if (revWalk.isMergedInto(headCommit, srcCommit)
  258. && fastForwardMode != FastForwardMode.NO_FF) {
  259. // FAST_FORWARD detected: skip doing a real merge but only
  260. // update HEAD
  261. refLogMessage.append(": " + MergeStatus.FAST_FORWARD); //$NON-NLS-1$
  262. dco = new DirCacheCheckout(repo,
  263. headCommit.getTree(), repo.lockDirCache(),
  264. srcCommit.getTree());
  265. dco.setFailOnConflict(true);
  266. dco.checkout();
  267. String msg = null;
  268. ObjectId newHead, base = null;
  269. MergeStatus mergeStatus = null;
  270. if (!squash) {
  271. updateHead(refLogMessage, srcCommit, headId);
  272. newHead = base = srcCommit;
  273. mergeStatus = MergeStatus.FAST_FORWARD;
  274. } else {
  275. msg = JGitText.get().squashCommitNotUpdatingHEAD;
  276. newHead = base = headId;
  277. mergeStatus = MergeStatus.FAST_FORWARD_SQUASHED;
  278. List<RevCommit> squashedCommits = RevWalkUtils.find(
  279. revWalk, srcCommit, headCommit);
  280. String squashMessage = new SquashMessageFormatter().format(
  281. squashedCommits, head);
  282. repo.writeSquashCommitMsg(squashMessage);
  283. }
  284. setCallable(false);
  285. return new MergeResult(newHead, base, new ObjectId[] {
  286. headCommit, srcCommit }, mergeStatus, mergeStrategy,
  287. null, msg);
  288. } else {
  289. if (fastForwardMode == FastForwardMode.FF_ONLY) {
  290. return new MergeResult(headCommit, srcCommit,
  291. new ObjectId[] { headCommit, srcCommit },
  292. MergeStatus.ABORTED, mergeStrategy, null, null);
  293. }
  294. String mergeMessage = ""; //$NON-NLS-1$
  295. if (!squash) {
  296. if (message != null)
  297. mergeMessage = message;
  298. else
  299. mergeMessage = new MergeMessageFormatter().format(
  300. commits, head);
  301. repo.writeMergeCommitMsg(mergeMessage);
  302. repo.writeMergeHeads(Arrays.asList(ref.getObjectId()));
  303. } else {
  304. List<RevCommit> squashedCommits = RevWalkUtils.find(
  305. revWalk, srcCommit, headCommit);
  306. String squashMessage = new SquashMessageFormatter().format(
  307. squashedCommits, head);
  308. repo.writeSquashCommitMsg(squashMessage);
  309. }
  310. Merger merger = mergeStrategy.newMerger(repo);
  311. merger.setProgressMonitor(monitor);
  312. boolean noProblems;
  313. Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults = null;
  314. Map<String, MergeFailureReason> failingPaths = null;
  315. List<String> unmergedPaths = null;
  316. if (merger instanceof ResolveMerger) {
  317. ResolveMerger resolveMerger = (ResolveMerger) merger;
  318. resolveMerger.setCommitNames(new String[] {
  319. "BASE", "HEAD", ref.getName() }); //$NON-NLS-1$ //$NON-NLS-2$
  320. resolveMerger.setWorkingTreeIterator(new FileTreeIterator(repo));
  321. noProblems = merger.merge(headCommit, srcCommit);
  322. lowLevelResults = resolveMerger
  323. .getMergeResults();
  324. failingPaths = resolveMerger.getFailingPaths();
  325. unmergedPaths = resolveMerger.getUnmergedPaths();
  326. } else
  327. noProblems = merger.merge(headCommit, srcCommit);
  328. refLogMessage.append(": Merge made by "); //$NON-NLS-1$
  329. if (!revWalk.isMergedInto(headCommit, srcCommit))
  330. refLogMessage.append(mergeStrategy.getName());
  331. else
  332. refLogMessage.append("recursive"); //$NON-NLS-1$
  333. refLogMessage.append('.');
  334. if (noProblems) {
  335. dco = new DirCacheCheckout(repo,
  336. headCommit.getTree(), repo.lockDirCache(),
  337. merger.getResultTreeId());
  338. dco.setFailOnConflict(true);
  339. dco.checkout();
  340. String msg = null;
  341. ObjectId newHeadId = null;
  342. MergeStatus mergeStatus = null;
  343. if (!commit && squash) {
  344. mergeStatus = MergeStatus.MERGED_SQUASHED_NOT_COMMITTED;
  345. }
  346. if (!commit && !squash) {
  347. mergeStatus = MergeStatus.MERGED_NOT_COMMITTED;
  348. }
  349. if (commit && !squash) {
  350. try (Git git = new Git(getRepository())) {
  351. newHeadId = git.commit()
  352. .setReflogComment(refLogMessage.toString())
  353. .call().getId();
  354. }
  355. mergeStatus = MergeStatus.MERGED;
  356. }
  357. if (commit && squash) {
  358. msg = JGitText.get().squashCommitNotUpdatingHEAD;
  359. newHeadId = headCommit.getId();
  360. mergeStatus = MergeStatus.MERGED_SQUASHED;
  361. }
  362. return new MergeResult(newHeadId, null,
  363. new ObjectId[] { headCommit.getId(),
  364. srcCommit.getId() }, mergeStatus,
  365. mergeStrategy, null, msg);
  366. } else {
  367. if (failingPaths != null) {
  368. repo.writeMergeCommitMsg(null);
  369. repo.writeMergeHeads(null);
  370. return new MergeResult(null, merger.getBaseCommitId(),
  371. new ObjectId[] {
  372. headCommit.getId(), srcCommit.getId() },
  373. MergeStatus.FAILED, mergeStrategy,
  374. lowLevelResults, failingPaths, null);
  375. } else {
  376. String mergeMessageWithConflicts = new MergeMessageFormatter()
  377. .formatWithConflicts(mergeMessage,
  378. unmergedPaths);
  379. repo.writeMergeCommitMsg(mergeMessageWithConflicts);
  380. return new MergeResult(null, merger.getBaseCommitId(),
  381. new ObjectId[] { headCommit.getId(),
  382. srcCommit.getId() },
  383. MergeStatus.CONFLICTING, mergeStrategy,
  384. lowLevelResults, null);
  385. }
  386. }
  387. }
  388. } catch (org.eclipse.jgit.errors.CheckoutConflictException e) {
  389. List<String> conflicts = (dco == null) ? Collections
  390. .<String> emptyList() : dco.getConflicts();
  391. throw new CheckoutConflictException(conflicts, e);
  392. } catch (IOException e) {
  393. throw new JGitInternalException(
  394. MessageFormat.format(
  395. JGitText.get().exceptionCaughtDuringExecutionOfMergeCommand,
  396. e), e);
  397. } finally {
  398. if (revWalk != null)
  399. revWalk.close();
  400. }
  401. }
  402. private void checkParameters() throws InvalidMergeHeadsException {
  403. if (squash.booleanValue() && fastForwardMode == FastForwardMode.NO_FF) {
  404. throw new JGitInternalException(
  405. JGitText.get().cannotCombineSquashWithNoff);
  406. }
  407. if (commits.size() != 1)
  408. throw new InvalidMergeHeadsException(
  409. commits.isEmpty() ? JGitText.get().noMergeHeadSpecified
  410. : MessageFormat.format(
  411. JGitText.get().mergeStrategyDoesNotSupportHeads,
  412. mergeStrategy.getName(),
  413. Integer.valueOf(commits.size())));
  414. }
  415. /**
  416. * Use values from the configuation if they have not been explicitly defined
  417. * via the setters
  418. */
  419. private void fallBackToConfiguration() {
  420. MergeConfig config = MergeConfig.getConfigForCurrentBranch(repo);
  421. if (squash == null)
  422. squash = Boolean.valueOf(config.isSquash());
  423. if (commit == null)
  424. commit = Boolean.valueOf(config.isCommit());
  425. if (fastForwardMode == null)
  426. fastForwardMode = config.getFastForwardMode();
  427. }
  428. private void updateHead(StringBuilder refLogMessage, ObjectId newHeadId,
  429. ObjectId oldHeadID) throws IOException,
  430. ConcurrentRefUpdateException {
  431. RefUpdate refUpdate = repo.updateRef(Constants.HEAD);
  432. refUpdate.setNewObjectId(newHeadId);
  433. refUpdate.setRefLogMessage(refLogMessage.toString(), false);
  434. refUpdate.setExpectedOldObjectId(oldHeadID);
  435. Result rc = refUpdate.update();
  436. switch (rc) {
  437. case NEW:
  438. case FAST_FORWARD:
  439. return;
  440. case REJECTED:
  441. case LOCK_FAILURE:
  442. throw new ConcurrentRefUpdateException(
  443. JGitText.get().couldNotLockHEAD, refUpdate.getRef(), rc);
  444. default:
  445. throw new JGitInternalException(MessageFormat.format(
  446. JGitText.get().updatingRefFailed, Constants.HEAD,
  447. newHeadId.toString(), rc));
  448. }
  449. }
  450. /**
  451. *
  452. * @param mergeStrategy
  453. * the {@link MergeStrategy} to be used
  454. * @return {@code this}
  455. */
  456. public MergeCommand setStrategy(MergeStrategy mergeStrategy) {
  457. checkCallable();
  458. this.mergeStrategy = mergeStrategy;
  459. return this;
  460. }
  461. /**
  462. * @param aCommit
  463. * a reference to a commit which is merged with the current head
  464. * @return {@code this}
  465. */
  466. public MergeCommand include(Ref aCommit) {
  467. checkCallable();
  468. commits.add(aCommit);
  469. return this;
  470. }
  471. /**
  472. * @param aCommit
  473. * the Id of a commit which is merged with the current head
  474. * @return {@code this}
  475. */
  476. public MergeCommand include(AnyObjectId aCommit) {
  477. return include(aCommit.getName(), aCommit);
  478. }
  479. /**
  480. * @param name
  481. * a name given to the commit
  482. * @param aCommit
  483. * the Id of a commit which is merged with the current head
  484. * @return {@code this}
  485. */
  486. public MergeCommand include(String name, AnyObjectId aCommit) {
  487. return include(new ObjectIdRef.Unpeeled(Storage.LOOSE, name,
  488. aCommit.copy()));
  489. }
  490. /**
  491. * If <code>true</code>, will prepare the next commit in working tree and
  492. * index as if a real merge happened, but do not make the commit or move the
  493. * HEAD. Otherwise, perform the merge and commit the result.
  494. * <p>
  495. * In case the merge was successful but this flag was set to
  496. * <code>true</code> a {@link MergeResult} with status
  497. * {@link MergeStatus#MERGED_SQUASHED} or
  498. * {@link MergeStatus#FAST_FORWARD_SQUASHED} is returned.
  499. *
  500. * @param squash
  501. * whether to squash commits or not
  502. * @return {@code this}
  503. * @since 2.0
  504. */
  505. public MergeCommand setSquash(boolean squash) {
  506. checkCallable();
  507. this.squash = Boolean.valueOf(squash);
  508. return this;
  509. }
  510. /**
  511. * Sets the fast forward mode.
  512. *
  513. * @param fastForwardMode
  514. * corresponds to the --ff/--no-ff/--ff-only options. --ff is the
  515. * default option.
  516. * @return {@code this}
  517. * @since 2.2
  518. */
  519. public MergeCommand setFastForward(FastForwardMode fastForwardMode) {
  520. checkCallable();
  521. this.fastForwardMode = fastForwardMode;
  522. return this;
  523. }
  524. /**
  525. * Controls whether the merge command should automatically commit after a
  526. * successful merge
  527. *
  528. * @param commit
  529. * <code>true</code> if this command should commit (this is the
  530. * default behavior). <code>false</code> if this command should
  531. * not commit. In case the merge was successful but this flag was
  532. * set to <code>false</code> a {@link MergeResult} with type
  533. * {@link MergeResult} with status
  534. * {@link MergeStatus#MERGED_NOT_COMMITTED} is returned
  535. * @return {@code this}
  536. * @since 3.0
  537. */
  538. public MergeCommand setCommit(boolean commit) {
  539. this.commit = Boolean.valueOf(commit);
  540. return this;
  541. }
  542. /**
  543. * Set the commit message to be used for the merge commit (in case one is
  544. * created)
  545. *
  546. * @param message
  547. * the message to be used for the merge commit
  548. * @return {@code this}
  549. * @since 3.5
  550. */
  551. public MergeCommand setMessage(String message) {
  552. this.message = message;
  553. return this;
  554. }
  555. /**
  556. * The progress monitor associated with the diff operation. By default, this
  557. * is set to <code>NullProgressMonitor</code>
  558. *
  559. * @see NullProgressMonitor
  560. *
  561. * @param monitor
  562. * A progress monitor
  563. * @return this instance
  564. * @since 4.2
  565. */
  566. public MergeCommand setProgressMonitor(ProgressMonitor monitor) {
  567. if (monitor == null) {
  568. monitor = NullProgressMonitor.INSTANCE;
  569. }
  570. this.monitor = monitor;
  571. return this;
  572. }
  573. }