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 21KB

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