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