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

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