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.

CheckoutCommand.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  3. * Copyright (C) 2011, Matthias Sohn <matthias.sohn@sap.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.api;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.text.MessageFormat;
  48. import java.util.ArrayList;
  49. import java.util.EnumSet;
  50. import java.util.LinkedList;
  51. import java.util.List;
  52. import org.eclipse.jgit.api.CheckoutResult.Status;
  53. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  54. import org.eclipse.jgit.api.errors.GitAPIException;
  55. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  56. import org.eclipse.jgit.api.errors.JGitInternalException;
  57. import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
  58. import org.eclipse.jgit.api.errors.RefNotFoundException;
  59. import org.eclipse.jgit.dircache.DirCache;
  60. import org.eclipse.jgit.dircache.DirCacheCheckout;
  61. import org.eclipse.jgit.dircache.DirCacheEditor;
  62. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  63. import org.eclipse.jgit.dircache.DirCacheEntry;
  64. import org.eclipse.jgit.dircache.DirCacheIterator;
  65. import org.eclipse.jgit.errors.AmbiguousObjectException;
  66. import org.eclipse.jgit.errors.UnmergedPathException;
  67. import org.eclipse.jgit.internal.JGitText;
  68. import org.eclipse.jgit.lib.AnyObjectId;
  69. import org.eclipse.jgit.lib.Constants;
  70. import org.eclipse.jgit.lib.FileMode;
  71. import org.eclipse.jgit.lib.ObjectId;
  72. import org.eclipse.jgit.lib.ObjectReader;
  73. import org.eclipse.jgit.lib.Ref;
  74. import org.eclipse.jgit.lib.RefUpdate;
  75. import org.eclipse.jgit.lib.RefUpdate.Result;
  76. import org.eclipse.jgit.lib.Repository;
  77. import org.eclipse.jgit.revwalk.RevCommit;
  78. import org.eclipse.jgit.revwalk.RevTree;
  79. import org.eclipse.jgit.revwalk.RevWalk;
  80. import org.eclipse.jgit.treewalk.TreeWalk;
  81. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  82. import org.eclipse.jgit.util.FileUtils;
  83. /**
  84. * Checkout a branch to the working tree.
  85. * <p>
  86. * Examples (<code>git</code> is a {@link Git} instance):
  87. * <p>
  88. * Check out an existing branch:
  89. *
  90. * <pre>
  91. * git.checkout().setName(&quot;feature&quot;).call();
  92. * </pre>
  93. * <p>
  94. * Check out paths from the index:
  95. *
  96. * <pre>
  97. * git.checkout().addPath(&quot;file1.txt&quot;).addPath(&quot;file2.txt&quot;).call();
  98. * </pre>
  99. * <p>
  100. * Check out a path from a commit:
  101. *
  102. * <pre>
  103. * git.checkout().setStartPoint(&quot;HEAD&circ;&quot;).addPath(&quot;file1.txt&quot;).call();
  104. * </pre>
  105. *
  106. * <p>
  107. * Create a new branch and check it out:
  108. *
  109. * <pre>
  110. * git.checkout().setCreateBranch(true).setName(&quot;newbranch&quot;).call();
  111. * </pre>
  112. * <p>
  113. * Create a new tracking branch for a remote branch and check it out:
  114. *
  115. * <pre>
  116. * git.checkout().setCreateBranch(true).setName(&quot;stable&quot;)
  117. * .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
  118. * .setStartPoint(&quot;origin/stable&quot;).call();
  119. * </pre>
  120. *
  121. * @see <a
  122. * href="http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html"
  123. * >Git documentation about Checkout</a>
  124. */
  125. public class CheckoutCommand extends GitCommand<Ref> {
  126. /**
  127. * Stage to check out, see {@link CheckoutCommand#setStage(Stage)}.
  128. */
  129. public static enum Stage {
  130. /**
  131. * Base stage (#1)
  132. */
  133. BASE(DirCacheEntry.STAGE_1),
  134. /**
  135. * Ours stage (#2)
  136. */
  137. OURS(DirCacheEntry.STAGE_2),
  138. /**
  139. * Theirs stage (#3)
  140. */
  141. THEIRS(DirCacheEntry.STAGE_3);
  142. private final int number;
  143. private Stage(int number) {
  144. this.number = number;
  145. }
  146. }
  147. private String name;
  148. private boolean force = false;
  149. private boolean createBranch = false;
  150. private boolean orphan = false;
  151. private CreateBranchCommand.SetupUpstreamMode upstreamMode;
  152. private String startPoint = null;
  153. private RevCommit startCommit;
  154. private Stage checkoutStage = null;
  155. private CheckoutResult status;
  156. private List<String> paths;
  157. private boolean checkoutAllPaths;
  158. /**
  159. * @param repo
  160. */
  161. protected CheckoutCommand(Repository repo) {
  162. super(repo);
  163. this.paths = new LinkedList<String>();
  164. }
  165. /**
  166. * @throws RefAlreadyExistsException
  167. * when trying to create (without force) a branch with a name
  168. * that already exists
  169. * @throws RefNotFoundException
  170. * if the start point or branch can not be found
  171. * @throws InvalidRefNameException
  172. * if the provided name is <code>null</code> or otherwise
  173. * invalid
  174. * @throws CheckoutConflictException
  175. * if the checkout results in a conflict
  176. * @return the newly created branch
  177. */
  178. public Ref call() throws GitAPIException, RefAlreadyExistsException,
  179. RefNotFoundException, InvalidRefNameException,
  180. CheckoutConflictException {
  181. checkCallable();
  182. try {
  183. processOptions();
  184. if (checkoutAllPaths || !paths.isEmpty()) {
  185. checkoutPaths();
  186. status = new CheckoutResult(Status.OK, paths);
  187. setCallable(false);
  188. return null;
  189. }
  190. if (createBranch) {
  191. Git git = new Git(repo);
  192. CreateBranchCommand command = git.branchCreate();
  193. command.setName(name);
  194. if (startCommit != null)
  195. command.setStartPoint(startCommit);
  196. else
  197. command.setStartPoint(startPoint);
  198. if (upstreamMode != null)
  199. command.setUpstreamMode(upstreamMode);
  200. command.call();
  201. }
  202. Ref headRef = repo.getRef(Constants.HEAD);
  203. String shortHeadRef = getShortBranchName(headRef);
  204. String refLogMessage = "checkout: moving from " + shortHeadRef; //$NON-NLS-1$
  205. ObjectId branch;
  206. if (orphan) {
  207. if (startPoint == null && startCommit == null) {
  208. Result r = repo.updateRef(Constants.HEAD).link(
  209. getBranchName());
  210. if (!EnumSet.of(Result.NEW, Result.FORCED).contains(r))
  211. throw new JGitInternalException(MessageFormat.format(
  212. JGitText.get().checkoutUnexpectedResult,
  213. r.name()));
  214. this.status = CheckoutResult.NOT_TRIED_RESULT;
  215. return repo.getRef(Constants.HEAD);
  216. }
  217. branch = getStartPointObjectId();
  218. } else {
  219. branch = repo.resolve(name);
  220. if (branch == null)
  221. throw new RefNotFoundException(MessageFormat.format(
  222. JGitText.get().refNotResolved, name));
  223. }
  224. RevWalk revWalk = new RevWalk(repo);
  225. AnyObjectId headId = headRef.getObjectId();
  226. RevCommit headCommit = headId == null ? null : revWalk
  227. .parseCommit(headId);
  228. RevCommit newCommit = revWalk.parseCommit(branch);
  229. RevTree headTree = headCommit == null ? null : headCommit.getTree();
  230. DirCacheCheckout dco;
  231. DirCache dc = repo.lockDirCache();
  232. try {
  233. dco = new DirCacheCheckout(repo, headTree, dc,
  234. newCommit.getTree());
  235. dco.setFailOnConflict(true);
  236. try {
  237. dco.checkout();
  238. } catch (org.eclipse.jgit.errors.CheckoutConflictException e) {
  239. status = new CheckoutResult(Status.CONFLICTS,
  240. dco.getConflicts());
  241. throw new CheckoutConflictException(dco.getConflicts(), e);
  242. }
  243. } finally {
  244. dc.unlock();
  245. }
  246. Ref ref = repo.getRef(name);
  247. if (ref != null && !ref.getName().startsWith(Constants.R_HEADS))
  248. ref = null;
  249. String toName = Repository.shortenRefName(name);
  250. RefUpdate refUpdate = repo.updateRef(Constants.HEAD, ref == null);
  251. refUpdate.setForceUpdate(force);
  252. refUpdate.setRefLogMessage(refLogMessage + " to " + toName, false); //$NON-NLS-1$
  253. Result updateResult;
  254. if (ref != null)
  255. updateResult = refUpdate.link(ref.getName());
  256. else if (orphan) {
  257. updateResult = refUpdate.link(getBranchName());
  258. ref = repo.getRef(Constants.HEAD);
  259. } else {
  260. refUpdate.setNewObjectId(newCommit);
  261. updateResult = refUpdate.forceUpdate();
  262. }
  263. setCallable(false);
  264. boolean ok = false;
  265. switch (updateResult) {
  266. case NEW:
  267. ok = true;
  268. break;
  269. case NO_CHANGE:
  270. case FAST_FORWARD:
  271. case FORCED:
  272. ok = true;
  273. break;
  274. default:
  275. break;
  276. }
  277. if (!ok)
  278. throw new JGitInternalException(MessageFormat.format(JGitText
  279. .get().checkoutUnexpectedResult, updateResult.name()));
  280. if (!dco.getToBeDeleted().isEmpty()) {
  281. status = new CheckoutResult(Status.NONDELETED,
  282. dco.getToBeDeleted());
  283. } else
  284. status = new CheckoutResult(new ArrayList<String>(dco
  285. .getUpdated().keySet()), dco.getRemoved());
  286. return ref;
  287. } catch (IOException ioe) {
  288. throw new JGitInternalException(ioe.getMessage(), ioe);
  289. } finally {
  290. if (status == null)
  291. status = CheckoutResult.ERROR_RESULT;
  292. }
  293. }
  294. private String getShortBranchName(Ref headRef) {
  295. if (headRef.getTarget().getName().equals(headRef.getName()))
  296. return headRef.getTarget().getObjectId().getName();
  297. return Repository.shortenRefName(headRef.getTarget().getName());
  298. }
  299. /**
  300. * Add a single slash-separated path to the list of paths to check out. To
  301. * check out all paths, use {@link #setAllPaths(boolean)}.
  302. * <p>
  303. * If this option is set, neither the {@link #setCreateBranch(boolean)} nor
  304. * {@link #setName(String)} option is considered. In other words, these
  305. * options are exclusive.
  306. *
  307. * @param path
  308. * path to update in the working tree and index (with
  309. * <code>/</code> as separator)
  310. * @return {@code this}
  311. */
  312. public CheckoutCommand addPath(String path) {
  313. checkCallable();
  314. this.paths.add(path);
  315. return this;
  316. }
  317. /**
  318. * Set whether to checkout all paths.
  319. * <p>
  320. * This options should be used when you want to do a path checkout on the
  321. * entire repository and so calling {@link #addPath(String)} is not possible
  322. * since empty paths are not allowed.
  323. * <p>
  324. * If this option is set, neither the {@link #setCreateBranch(boolean)} nor
  325. * {@link #setName(String)} option is considered. In other words, these
  326. * options are exclusive.
  327. *
  328. * @param all
  329. * <code>true</code> to checkout all paths, <code>false</code>
  330. * otherwise
  331. * @return {@code this}
  332. * @since 2.0
  333. */
  334. public CheckoutCommand setAllPaths(boolean all) {
  335. checkoutAllPaths = all;
  336. return this;
  337. }
  338. /**
  339. * Checkout paths into index and working directory
  340. *
  341. * @return this instance
  342. * @throws IOException
  343. * @throws RefNotFoundException
  344. */
  345. protected CheckoutCommand checkoutPaths() throws IOException,
  346. RefNotFoundException {
  347. RevWalk revWalk = new RevWalk(repo);
  348. DirCache dc = repo.lockDirCache();
  349. try {
  350. TreeWalk treeWalk = new TreeWalk(revWalk.getObjectReader());
  351. treeWalk.setRecursive(true);
  352. if (!checkoutAllPaths)
  353. treeWalk.setFilter(PathFilterGroup.createFromStrings(paths));
  354. try {
  355. if (isCheckoutIndex())
  356. checkoutPathsFromIndex(treeWalk, dc);
  357. else {
  358. RevCommit commit = revWalk.parseCommit(getStartPointObjectId());
  359. checkoutPathsFromCommit(treeWalk, dc, commit);
  360. }
  361. } finally {
  362. treeWalk.release();
  363. }
  364. } finally {
  365. dc.unlock();
  366. revWalk.release();
  367. }
  368. return this;
  369. }
  370. private void checkoutPathsFromIndex(TreeWalk treeWalk, DirCache dc)
  371. throws IOException {
  372. DirCacheIterator dci = new DirCacheIterator(dc);
  373. treeWalk.addTree(dci);
  374. final ObjectReader r = treeWalk.getObjectReader();
  375. DirCacheEditor editor = dc.editor();
  376. while (treeWalk.next()) {
  377. DirCacheEntry entry = dci.getDirCacheEntry();
  378. // Only add one edit per path
  379. if (entry != null && entry.getStage() > DirCacheEntry.STAGE_1)
  380. continue;
  381. editor.add(new PathEdit(treeWalk.getPathString()) {
  382. public void apply(DirCacheEntry ent) {
  383. int stage = ent.getStage();
  384. if (stage > DirCacheEntry.STAGE_0) {
  385. if (checkoutStage != null) {
  386. if (stage == checkoutStage.number)
  387. checkoutPath(ent, r);
  388. } else {
  389. UnmergedPathException e = new UnmergedPathException(
  390. ent);
  391. throw new JGitInternalException(e.getMessage(), e);
  392. }
  393. } else {
  394. checkoutPath(ent, r);
  395. }
  396. }
  397. });
  398. }
  399. editor.commit();
  400. }
  401. private void checkoutPathsFromCommit(TreeWalk treeWalk, DirCache dc,
  402. RevCommit commit) throws IOException {
  403. treeWalk.addTree(commit.getTree());
  404. final ObjectReader r = treeWalk.getObjectReader();
  405. DirCacheEditor editor = dc.editor();
  406. while (treeWalk.next()) {
  407. final ObjectId blobId = treeWalk.getObjectId(0);
  408. final FileMode mode = treeWalk.getFileMode(0);
  409. editor.add(new PathEdit(treeWalk.getPathString()) {
  410. public void apply(DirCacheEntry ent) {
  411. ent.setObjectId(blobId);
  412. ent.setFileMode(mode);
  413. checkoutPath(ent, r);
  414. }
  415. });
  416. }
  417. editor.commit();
  418. }
  419. private void checkoutPath(DirCacheEntry entry, ObjectReader reader) {
  420. File file = new File(repo.getWorkTree(), entry.getPathString());
  421. File parentDir = file.getParentFile();
  422. try {
  423. FileUtils.mkdirs(parentDir, true);
  424. DirCacheCheckout.checkoutEntry(repo, file, entry, reader);
  425. } catch (IOException e) {
  426. throw new JGitInternalException(MessageFormat.format(
  427. JGitText.get().checkoutConflictWithFile,
  428. entry.getPathString()), e);
  429. }
  430. }
  431. private boolean isCheckoutIndex() {
  432. return startCommit == null && startPoint == null;
  433. }
  434. private ObjectId getStartPointObjectId() throws AmbiguousObjectException,
  435. RefNotFoundException, IOException {
  436. if (startCommit != null)
  437. return startCommit.getId();
  438. String startPointOrHead = (startPoint != null) ? startPoint
  439. : Constants.HEAD;
  440. ObjectId result = repo.resolve(startPointOrHead);
  441. if (result == null)
  442. throw new RefNotFoundException(MessageFormat.format(
  443. JGitText.get().refNotResolved, startPointOrHead));
  444. return result;
  445. }
  446. private void processOptions() throws InvalidRefNameException,
  447. RefAlreadyExistsException, IOException {
  448. if (((!checkoutAllPaths && paths.isEmpty()) || orphan)
  449. && (name == null || !Repository
  450. .isValidRefName(Constants.R_HEADS + name)))
  451. throw new InvalidRefNameException(MessageFormat.format(JGitText
  452. .get().branchNameInvalid, name == null ? "<null>" : name)); //$NON-NLS-1$
  453. if (orphan) {
  454. Ref refToCheck = repo.getRef(getBranchName());
  455. if (refToCheck != null)
  456. throw new RefAlreadyExistsException(MessageFormat.format(
  457. JGitText.get().refAlreadyExists, name));
  458. }
  459. }
  460. private String getBranchName() {
  461. if (name.startsWith(Constants.R_REFS))
  462. return name;
  463. return Constants.R_HEADS + name;
  464. }
  465. /**
  466. * Specify the name of the branch or commit to check out, or the new branch
  467. * name.
  468. * <p>
  469. * When only checking out paths and not switching branches, use
  470. * {@link #setStartPoint(String)} or {@link #setStartPoint(RevCommit)} to
  471. * specify from which branch or commit to check out files.
  472. * <p>
  473. * When {@link #setCreateBranch(boolean)} is set to <code>true</code>, use
  474. * this method to set the name of the new branch to create and
  475. * {@link #setStartPoint(String)} or {@link #setStartPoint(RevCommit)} to
  476. * specify the start point of the branch.
  477. *
  478. * @param name
  479. * the name of the branch or commit
  480. * @return this instance
  481. */
  482. public CheckoutCommand setName(String name) {
  483. checkCallable();
  484. this.name = name;
  485. return this;
  486. }
  487. /**
  488. * Specify whether to create a new branch.
  489. * <p>
  490. * If <code>true</code> is used, the name of the new branch must be set
  491. * using {@link #setName(String)}. The commit at which to start the new
  492. * branch can be set using {@link #setStartPoint(String)} or
  493. * {@link #setStartPoint(RevCommit)}; if not specified, HEAD is used. Also
  494. * see {@link #setUpstreamMode} for setting up branch tracking.
  495. *
  496. * @param createBranch
  497. * if <code>true</code> a branch will be created as part of the
  498. * checkout and set to the specified start point
  499. * @return this instance
  500. */
  501. public CheckoutCommand setCreateBranch(boolean createBranch) {
  502. checkCallable();
  503. this.createBranch = createBranch;
  504. return this;
  505. }
  506. /**
  507. * Specify whether to create a new orphan branch.
  508. * <p>
  509. * If <code>true</code> is used, the name of the new orphan branch must be
  510. * set using {@link #setName(String)}. The commit at which to start the new
  511. * orphan branch can be set using {@link #setStartPoint(String)} or
  512. * {@link #setStartPoint(RevCommit)}; if not specified, HEAD is used.
  513. *
  514. * @param orphan
  515. * if <code>true</code> a orphan branch will be created as part
  516. * of the checkout to the specified start point
  517. * @return this instance
  518. * @since 3.3
  519. */
  520. public CheckoutCommand setOrphan(boolean orphan) {
  521. checkCallable();
  522. this.orphan = orphan;
  523. return this;
  524. }
  525. /**
  526. * Specify to force the ref update in case of a branch switch.
  527. *
  528. * @param force
  529. * if <code>true</code> and the branch with the given name
  530. * already exists, the start-point of an existing branch will be
  531. * set to a new start-point; if false, the existing branch will
  532. * not be changed
  533. * @return this instance
  534. */
  535. public CheckoutCommand setForce(boolean force) {
  536. checkCallable();
  537. this.force = force;
  538. return this;
  539. }
  540. /**
  541. * Set the name of the commit that should be checked out.
  542. * <p>
  543. * When checking out files and this is not specified or <code>null</code>,
  544. * the index is used.
  545. * <p>
  546. * When creating a new branch, this will be used as the start point. If not
  547. * specified or <code>null</code>, the current HEAD is used.
  548. *
  549. * @param startPoint
  550. * commit name to check out
  551. * @return this instance
  552. */
  553. public CheckoutCommand setStartPoint(String startPoint) {
  554. checkCallable();
  555. this.startPoint = startPoint;
  556. this.startCommit = null;
  557. checkOptions();
  558. return this;
  559. }
  560. /**
  561. * Set the commit that should be checked out.
  562. * <p>
  563. * When creating a new branch, this will be used as the start point. If not
  564. * specified or <code>null</code>, the current HEAD is used.
  565. * <p>
  566. * When checking out files and this is not specified or <code>null</code>,
  567. * the index is used.
  568. *
  569. * @param startCommit
  570. * commit to check out
  571. * @return this instance
  572. */
  573. public CheckoutCommand setStartPoint(RevCommit startCommit) {
  574. checkCallable();
  575. this.startCommit = startCommit;
  576. this.startPoint = null;
  577. checkOptions();
  578. return this;
  579. }
  580. /**
  581. * When creating a branch with {@link #setCreateBranch(boolean)}, this can
  582. * be used to configure branch tracking.
  583. *
  584. * @param mode
  585. * corresponds to the --track/--no-track options; may be
  586. * <code>null</code>
  587. * @return this instance
  588. */
  589. public CheckoutCommand setUpstreamMode(
  590. CreateBranchCommand.SetupUpstreamMode mode) {
  591. checkCallable();
  592. this.upstreamMode = mode;
  593. return this;
  594. }
  595. /**
  596. * When checking out the index, check out the specified stage (ours or
  597. * theirs) for unmerged paths.
  598. * <p>
  599. * This can not be used when checking out a branch, only when checking out
  600. * the index.
  601. *
  602. * @param stage
  603. * the stage to check out
  604. * @return this
  605. */
  606. public CheckoutCommand setStage(Stage stage) {
  607. checkCallable();
  608. this.checkoutStage = stage;
  609. checkOptions();
  610. return this;
  611. }
  612. /**
  613. * @return the result, never <code>null</code>
  614. */
  615. public CheckoutResult getResult() {
  616. if (status == null)
  617. return CheckoutResult.NOT_TRIED_RESULT;
  618. return status;
  619. }
  620. private void checkOptions() {
  621. if (checkoutStage != null && !isCheckoutIndex())
  622. throw new IllegalStateException(
  623. "Checking out ours/theirs is only possible when checking out index, "
  624. + "not when switching branches.");
  625. }
  626. }