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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. }
  384. if (failingPaths != null) {
  385. repo.writeMergeCommitMsg(null);
  386. repo.writeMergeHeads(null);
  387. return new MergeResult(null, merger.getBaseCommitId(),
  388. new ObjectId[] { headCommit.getId(),
  389. srcCommit.getId() },
  390. MergeStatus.FAILED, mergeStrategy, lowLevelResults,
  391. failingPaths, null);
  392. }
  393. String mergeMessageWithConflicts = new MergeMessageFormatter()
  394. .formatWithConflicts(mergeMessage, unmergedPaths);
  395. repo.writeMergeCommitMsg(mergeMessageWithConflicts);
  396. return new MergeResult(null, merger.getBaseCommitId(),
  397. new ObjectId[] { headCommit.getId(),
  398. srcCommit.getId() },
  399. MergeStatus.CONFLICTING, mergeStrategy, lowLevelResults,
  400. null);
  401. }
  402. } catch (org.eclipse.jgit.errors.CheckoutConflictException e) {
  403. List<String> conflicts = (dco == null) ? Collections
  404. .<String> emptyList() : dco.getConflicts();
  405. throw new CheckoutConflictException(conflicts, e);
  406. } catch (IOException e) {
  407. throw new JGitInternalException(
  408. MessageFormat.format(
  409. JGitText.get().exceptionCaughtDuringExecutionOfMergeCommand,
  410. e), e);
  411. }
  412. }
  413. private void checkParameters() throws InvalidMergeHeadsException {
  414. if (squash.booleanValue() && fastForwardMode == FastForwardMode.NO_FF) {
  415. throw new JGitInternalException(
  416. JGitText.get().cannotCombineSquashWithNoff);
  417. }
  418. if (commits.size() != 1)
  419. throw new InvalidMergeHeadsException(
  420. commits.isEmpty() ? JGitText.get().noMergeHeadSpecified
  421. : MessageFormat.format(
  422. JGitText.get().mergeStrategyDoesNotSupportHeads,
  423. mergeStrategy.getName(),
  424. Integer.valueOf(commits.size())));
  425. }
  426. /**
  427. * Use values from the configuration if they have not been explicitly
  428. * defined via the setters
  429. */
  430. private void fallBackToConfiguration() {
  431. MergeConfig config = MergeConfig.getConfigForCurrentBranch(repo);
  432. if (squash == null)
  433. squash = Boolean.valueOf(config.isSquash());
  434. if (commit == null)
  435. commit = Boolean.valueOf(config.isCommit());
  436. if (fastForwardMode == null)
  437. fastForwardMode = config.getFastForwardMode();
  438. }
  439. private void updateHead(StringBuilder refLogMessage, ObjectId newHeadId,
  440. ObjectId oldHeadID) throws IOException,
  441. ConcurrentRefUpdateException {
  442. RefUpdate refUpdate = repo.updateRef(Constants.HEAD);
  443. refUpdate.setNewObjectId(newHeadId);
  444. refUpdate.setRefLogMessage(refLogMessage.toString(), false);
  445. refUpdate.setExpectedOldObjectId(oldHeadID);
  446. Result rc = refUpdate.update();
  447. switch (rc) {
  448. case NEW:
  449. case FAST_FORWARD:
  450. return;
  451. case REJECTED:
  452. case LOCK_FAILURE:
  453. throw new ConcurrentRefUpdateException(
  454. JGitText.get().couldNotLockHEAD, refUpdate.getRef(), rc);
  455. default:
  456. throw new JGitInternalException(MessageFormat.format(
  457. JGitText.get().updatingRefFailed, Constants.HEAD,
  458. newHeadId.toString(), rc));
  459. }
  460. }
  461. /**
  462. * Set merge strategy
  463. *
  464. * @param mergeStrategy
  465. * the {@link org.eclipse.jgit.merge.MergeStrategy} to be used
  466. * @return {@code this}
  467. */
  468. public MergeCommand setStrategy(MergeStrategy mergeStrategy) {
  469. checkCallable();
  470. this.mergeStrategy = mergeStrategy;
  471. return this;
  472. }
  473. /**
  474. * Reference to a commit to be merged with the current head
  475. *
  476. * @param aCommit
  477. * a reference to a commit which is merged with the current head
  478. * @return {@code this}
  479. */
  480. public MergeCommand include(Ref aCommit) {
  481. checkCallable();
  482. commits.add(aCommit);
  483. return this;
  484. }
  485. /**
  486. * Id of a commit which is to be merged with the current head
  487. *
  488. * @param aCommit
  489. * the Id of a commit which is merged with the current head
  490. * @return {@code this}
  491. */
  492. public MergeCommand include(AnyObjectId aCommit) {
  493. return include(aCommit.getName(), aCommit);
  494. }
  495. /**
  496. * Include a commit
  497. *
  498. * @param name
  499. * a name of a {@code Ref} pointing to the commit
  500. * @param aCommit
  501. * the Id of a commit which is merged with the current head
  502. * @return {@code this}
  503. */
  504. public MergeCommand include(String name, AnyObjectId aCommit) {
  505. return include(new ObjectIdRef.Unpeeled(Storage.LOOSE, name,
  506. aCommit.copy()));
  507. }
  508. /**
  509. * If <code>true</code>, will prepare the next commit in working tree and
  510. * index as if a real merge happened, but do not make the commit or move the
  511. * HEAD. Otherwise, perform the merge and commit the result.
  512. * <p>
  513. * In case the merge was successful but this flag was set to
  514. * <code>true</code> a {@link org.eclipse.jgit.api.MergeResult} with status
  515. * {@link org.eclipse.jgit.api.MergeResult.MergeStatus#MERGED_SQUASHED} or
  516. * {@link org.eclipse.jgit.api.MergeResult.MergeStatus#FAST_FORWARD_SQUASHED}
  517. * is returned.
  518. *
  519. * @param squash
  520. * whether to squash commits or not
  521. * @return {@code this}
  522. * @since 2.0
  523. */
  524. public MergeCommand setSquash(boolean squash) {
  525. checkCallable();
  526. this.squash = Boolean.valueOf(squash);
  527. return this;
  528. }
  529. /**
  530. * Sets the fast forward mode.
  531. *
  532. * @param fastForwardMode
  533. * corresponds to the --ff/--no-ff/--ff-only options. If
  534. * {@code null} use the value of the {@code merge.ff} option
  535. * configured in git config. If this option is not configured
  536. * --ff is the built-in default.
  537. * @return {@code this}
  538. * @since 2.2
  539. */
  540. public MergeCommand setFastForward(
  541. @Nullable FastForwardMode fastForwardMode) {
  542. checkCallable();
  543. this.fastForwardMode = fastForwardMode;
  544. return this;
  545. }
  546. /**
  547. * Controls whether the merge command should automatically commit after a
  548. * successful merge
  549. *
  550. * @param commit
  551. * <code>true</code> if this command should commit (this is the
  552. * default behavior). <code>false</code> if this command should
  553. * not commit. In case the merge was successful but this flag was
  554. * set to <code>false</code> a
  555. * {@link org.eclipse.jgit.api.MergeResult} with type
  556. * {@link org.eclipse.jgit.api.MergeResult} with status
  557. * {@link org.eclipse.jgit.api.MergeResult.MergeStatus#MERGED_NOT_COMMITTED}
  558. * is returned
  559. * @return {@code this}
  560. * @since 3.0
  561. */
  562. public MergeCommand setCommit(boolean commit) {
  563. this.commit = Boolean.valueOf(commit);
  564. return this;
  565. }
  566. /**
  567. * Set the commit message to be used for the merge commit (in case one is
  568. * created)
  569. *
  570. * @param message
  571. * the message to be used for the merge commit
  572. * @return {@code this}
  573. * @since 3.5
  574. */
  575. public MergeCommand setMessage(String message) {
  576. this.message = message;
  577. return this;
  578. }
  579. /**
  580. * If set to true a change id will be inserted into the commit message
  581. *
  582. * An existing change id is not replaced. An initial change id (I000...)
  583. * will be replaced by the change id.
  584. *
  585. * @param insertChangeId
  586. * whether to insert a change id
  587. * @return {@code this}
  588. * @since 5.0
  589. */
  590. public MergeCommand setInsertChangeId(boolean insertChangeId) {
  591. checkCallable();
  592. this.insertChangeId = insertChangeId;
  593. return this;
  594. }
  595. /**
  596. * The progress monitor associated with the diff operation. By default, this
  597. * is set to <code>NullProgressMonitor</code>
  598. *
  599. * @see NullProgressMonitor
  600. * @param monitor
  601. * A progress monitor
  602. * @return this instance
  603. * @since 4.2
  604. */
  605. public MergeCommand setProgressMonitor(ProgressMonitor monitor) {
  606. if (monitor == null) {
  607. monitor = NullProgressMonitor.INSTANCE;
  608. }
  609. this.monitor = monitor;
  610. return this;
  611. }
  612. }