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

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