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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import java.io.IOException;
  45. import java.text.MessageFormat;
  46. import org.eclipse.jgit.JGitText;
  47. import org.eclipse.jgit.api.CheckoutResult.Status;
  48. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  49. import org.eclipse.jgit.api.errors.JGitInternalException;
  50. import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
  51. import org.eclipse.jgit.api.errors.RefNotFoundException;
  52. import org.eclipse.jgit.dircache.DirCacheCheckout;
  53. import org.eclipse.jgit.errors.AmbiguousObjectException;
  54. import org.eclipse.jgit.errors.CheckoutConflictException;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.lib.Ref;
  58. import org.eclipse.jgit.lib.RefUpdate;
  59. import org.eclipse.jgit.lib.Repository;
  60. import org.eclipse.jgit.lib.RefUpdate.Result;
  61. import org.eclipse.jgit.revwalk.RevCommit;
  62. import org.eclipse.jgit.revwalk.RevWalk;
  63. /**
  64. * Checkout a branch to the working tree
  65. *
  66. * @see <a
  67. * href="http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html"
  68. * >Git documentation about Checkout</a>
  69. */
  70. public class CheckoutCommand extends GitCommand<Ref> {
  71. private String name;
  72. private boolean force = false;
  73. private boolean createBranch = false;
  74. private CreateBranchCommand.SetupUpstreamMode upstreamMode;
  75. private String startPoint = Constants.HEAD;
  76. private RevCommit startCommit;
  77. private CheckoutResult status;
  78. /**
  79. * @param repo
  80. */
  81. protected CheckoutCommand(Repository repo) {
  82. super(repo);
  83. }
  84. /**
  85. * @throws RefAlreadyExistsException
  86. * when trying to create (without force) a branch with a name
  87. * that already exists
  88. * @throws RefNotFoundException
  89. * if the start point or branch can not be found
  90. * @throws InvalidRefNameException
  91. * if the provided name is <code>null</code> or otherwise
  92. * invalid
  93. * @return the newly created branch
  94. */
  95. public Ref call() throws JGitInternalException, RefAlreadyExistsException,
  96. RefNotFoundException, InvalidRefNameException {
  97. checkCallable();
  98. processOptions();
  99. try {
  100. if (createBranch) {
  101. Git git = new Git(repo);
  102. CreateBranchCommand command = git.branchCreate();
  103. command.setName(name);
  104. command.setStartPoint(getStartPoint().name());
  105. if (upstreamMode != null)
  106. command.setUpstreamMode(upstreamMode);
  107. command.call();
  108. }
  109. RevWalk revWalk = new RevWalk(repo);
  110. Ref headRef = repo.getRef(Constants.HEAD);
  111. RevCommit headCommit = revWalk.parseCommit(headRef.getObjectId());
  112. String refLogMessage = "checkout: moving from "
  113. + headRef.getTarget().getName();
  114. ObjectId branch = repo.resolve(name);
  115. if (branch == null)
  116. throw new RefNotFoundException(MessageFormat.format(JGitText
  117. .get().refNotResolved, name));
  118. RevCommit newCommit = revWalk.parseCommit(branch);
  119. DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit
  120. .getTree(), repo.lockDirCache(), newCommit.getTree());
  121. dco.setFailOnConflict(true);
  122. try {
  123. dco.checkout();
  124. } catch (CheckoutConflictException e) {
  125. status = new CheckoutResult(Status.CONFLICTS, dco
  126. .getConflicts());
  127. throw e;
  128. }
  129. Ref ref = repo.getRef(name);
  130. if (ref != null && !ref.getName().startsWith(Constants.R_HEADS))
  131. ref = null;
  132. RefUpdate refUpdate = repo.updateRef(Constants.HEAD, ref == null);
  133. refUpdate.setForceUpdate(force);
  134. refUpdate.setRefLogMessage(refLogMessage + " to "
  135. + newCommit.getName(), false);
  136. Result updateResult;
  137. if (ref != null)
  138. updateResult = refUpdate.link(ref.getName());
  139. else {
  140. refUpdate.setNewObjectId(newCommit);
  141. updateResult = refUpdate.forceUpdate();
  142. }
  143. setCallable(false);
  144. boolean ok = false;
  145. switch (updateResult) {
  146. case NEW:
  147. ok = true;
  148. break;
  149. case NO_CHANGE:
  150. case FAST_FORWARD:
  151. case FORCED:
  152. ok = true;
  153. break;
  154. default:
  155. break;
  156. }
  157. if (!ok)
  158. throw new JGitInternalException(MessageFormat.format(JGitText
  159. .get().checkoutUnexpectedResult, updateResult.name()));
  160. if (!dco.getToBeDeleted().isEmpty()) {
  161. status = new CheckoutResult(Status.NONDELETED, dco
  162. .getToBeDeleted());
  163. }
  164. else
  165. status = CheckoutResult.OK_RESULT;
  166. return ref;
  167. } catch (IOException ioe) {
  168. throw new JGitInternalException(ioe.getMessage(), ioe);
  169. } finally {
  170. if (status == null)
  171. status = CheckoutResult.ERROR_RESULT;
  172. }
  173. }
  174. private ObjectId getStartPoint() throws AmbiguousObjectException,
  175. RefNotFoundException, IOException {
  176. if (startCommit != null)
  177. return startCommit.getId();
  178. ObjectId result = null;
  179. try {
  180. result = repo.resolve((startPoint == null) ? Constants.HEAD
  181. : startPoint);
  182. } catch (AmbiguousObjectException e) {
  183. throw e;
  184. }
  185. if (result == null)
  186. throw new RefNotFoundException(MessageFormat.format(
  187. JGitText.get().refNotResolved,
  188. startPoint != null ? startPoint : Constants.HEAD));
  189. return result;
  190. }
  191. private void processOptions() throws InvalidRefNameException {
  192. if (name == null
  193. || !Repository.isValidRefName(Constants.R_HEADS + name))
  194. throw new InvalidRefNameException(MessageFormat.format(JGitText
  195. .get().branchNameInvalid, name == null ? "<null>" : name));
  196. }
  197. /**
  198. * @param name
  199. * the name of the new branch
  200. * @return this instance
  201. */
  202. public CheckoutCommand setName(String name) {
  203. checkCallable();
  204. this.name = name;
  205. return this;
  206. }
  207. /**
  208. * @param createBranch
  209. * if <code>true</code> a branch will be created as part of the
  210. * checkout and set to the specified start point
  211. * @return this instance
  212. */
  213. public CheckoutCommand setCreateBranch(boolean createBranch) {
  214. checkCallable();
  215. this.createBranch = createBranch;
  216. return this;
  217. }
  218. /**
  219. * @param force
  220. * if <code>true</code> and the branch with the given name
  221. * already exists, the start-point of an existing branch will be
  222. * set to a new start-point; if false, the existing branch will
  223. * not be changed
  224. * @return this instance
  225. */
  226. public CheckoutCommand setForce(boolean force) {
  227. checkCallable();
  228. this.force = force;
  229. return this;
  230. }
  231. /**
  232. * @param startPoint
  233. * corresponds to the start-point option; if <code>null</code>,
  234. * the current HEAD will be used
  235. * @return this instance
  236. */
  237. public CheckoutCommand setStartPoint(String startPoint) {
  238. checkCallable();
  239. this.startPoint = startPoint;
  240. this.startCommit = null;
  241. return this;
  242. }
  243. /**
  244. * @param startCommit
  245. * corresponds to the start-point option; if <code>null</code>,
  246. * the current HEAD will be used
  247. * @return this instance
  248. */
  249. public CheckoutCommand setStartPoint(RevCommit startCommit) {
  250. checkCallable();
  251. this.startCommit = startCommit;
  252. this.startPoint = null;
  253. return this;
  254. }
  255. /**
  256. * @param mode
  257. * corresponds to the --track/--no-track options; may be
  258. * <code>null</code>
  259. * @return this instance
  260. */
  261. public CheckoutCommand setUpstreamMode(
  262. CreateBranchCommand.SetupUpstreamMode mode) {
  263. checkCallable();
  264. this.upstreamMode = mode;
  265. return this;
  266. }
  267. /**
  268. * @return the result
  269. */
  270. public CheckoutResult getResult() {
  271. if (status == null)
  272. return CheckoutResult.NOT_TRIED_RESULT;
  273. return status;
  274. }
  275. }