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.

CloneCommand.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C) 2011, 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.File;
  45. import java.io.IOException;
  46. import java.net.URISyntaxException;
  47. import java.util.concurrent.Callable;
  48. import org.eclipse.jgit.api.errors.InvalidRemoteException;
  49. import org.eclipse.jgit.api.errors.JGitInternalException;
  50. import org.eclipse.jgit.dircache.DirCache;
  51. import org.eclipse.jgit.dircache.DirCacheCheckout;
  52. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  53. import org.eclipse.jgit.errors.MissingObjectException;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.NullProgressMonitor;
  56. import org.eclipse.jgit.lib.ProgressMonitor;
  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.revwalk.RevCommit;
  61. import org.eclipse.jgit.revwalk.RevWalk;
  62. import org.eclipse.jgit.transport.CredentialsProvider;
  63. import org.eclipse.jgit.transport.FetchResult;
  64. import org.eclipse.jgit.transport.RefSpec;
  65. import org.eclipse.jgit.transport.RemoteConfig;
  66. import org.eclipse.jgit.transport.URIish;
  67. /**
  68. * Clone a repository into a new working directory
  69. *
  70. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html"
  71. * >Git documentation about Clone</a>
  72. */
  73. public class CloneCommand implements Callable<Git> {
  74. private String uri;
  75. private File directory;
  76. private boolean bare;
  77. private String remote = Constants.DEFAULT_REMOTE_NAME;
  78. private String branch = Constants.HEAD;
  79. private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
  80. private CredentialsProvider credentialsProvider;
  81. /**
  82. * Executes the {@code Clone} command.
  83. *
  84. * @throws JGitInternalException
  85. * if the repository can't be created
  86. * @return the newly created {@code Git} object with associated repository
  87. */
  88. public Git call() throws JGitInternalException {
  89. try {
  90. URIish u = new URIish(uri);
  91. Repository repository = init(u);
  92. FetchResult result = fetch(repository, u);
  93. checkout(repository, result);
  94. return new Git(repository);
  95. } catch (IOException ioe) {
  96. throw new JGitInternalException(ioe.getMessage(), ioe);
  97. } catch (InvalidRemoteException e) {
  98. throw new JGitInternalException(e.getMessage(), e);
  99. } catch (URISyntaxException e) {
  100. throw new JGitInternalException(e.getMessage(), e);
  101. }
  102. }
  103. private Repository init(URIish u) {
  104. InitCommand command = Git.init();
  105. command.setBare(bare);
  106. if (directory == null)
  107. directory = new File(u.getHumanishName(), Constants.DOT_GIT);
  108. command.setDirectory(directory);
  109. return command.call().getRepository();
  110. }
  111. private FetchResult fetch(Repository repo, URIish u)
  112. throws URISyntaxException,
  113. JGitInternalException,
  114. InvalidRemoteException, IOException {
  115. // create the remote config and save it
  116. RemoteConfig config = new RemoteConfig(repo.getConfig(), remote);
  117. config.addURI(u);
  118. final String dst = Constants.R_REMOTES + config.getName();
  119. RefSpec refSpec = new RefSpec();
  120. refSpec = refSpec.setForceUpdate(true);
  121. refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", dst + "/*"); //$NON-NLS-1$ //$NON-NLS-2$
  122. config.addFetchRefSpec(refSpec);
  123. config.update(repo.getConfig());
  124. repo.getConfig().save();
  125. // run the fetch command
  126. FetchCommand command = new FetchCommand(repo);
  127. command.setRemote(remote);
  128. command.setProgressMonitor(monitor);
  129. if (credentialsProvider != null)
  130. command.setCredentialsProvider(credentialsProvider);
  131. return command.call();
  132. }
  133. private void checkout(Repository repo, FetchResult result)
  134. throws JGitInternalException,
  135. MissingObjectException, IncorrectObjectTypeException, IOException {
  136. if (branch.startsWith(Constants.R_HEADS)) {
  137. final RefUpdate head = repo.updateRef(Constants.HEAD);
  138. head.disableRefLog();
  139. head.link(branch);
  140. }
  141. final Ref head = result.getAdvertisedRef(branch);
  142. if (head == null || head.getObjectId() == null)
  143. return; // throw exception?
  144. final RevCommit commit = parseCommit(repo, head);
  145. boolean detached = !head.getName().startsWith(Constants.R_HEADS);
  146. RefUpdate u = repo.updateRef(Constants.HEAD, detached);
  147. u.setNewObjectId(commit.getId());
  148. u.forceUpdate();
  149. DirCache dc = repo.lockDirCache();
  150. DirCacheCheckout co = new DirCacheCheckout(repo, dc, commit.getTree());
  151. co.checkout();
  152. }
  153. private RevCommit parseCommit(final Repository repo, final Ref ref)
  154. throws MissingObjectException, IncorrectObjectTypeException,
  155. IOException {
  156. final RevWalk rw = new RevWalk(repo);
  157. final RevCommit commit;
  158. try {
  159. commit = rw.parseCommit(ref.getObjectId());
  160. } finally {
  161. rw.release();
  162. }
  163. return commit;
  164. }
  165. /**
  166. * @param uri
  167. * the uri to clone from
  168. * @return this instance
  169. */
  170. public CloneCommand setURI(String uri) {
  171. this.uri = uri;
  172. return this;
  173. }
  174. /**
  175. * The optional directory associated with the clone operation. If the
  176. * directory isn't set, a name associated with the source uri will be used.
  177. *
  178. * @see URIish#getHumanishName()
  179. *
  180. * @param directory
  181. * the directory to clone to
  182. * @return this instance
  183. */
  184. public CloneCommand setDirectory(File directory) {
  185. this.directory = directory;
  186. return this;
  187. }
  188. /**
  189. * @param bare
  190. * whether the cloned repository is bare or not
  191. * @return this instance
  192. */
  193. public CloneCommand setBare(boolean bare) {
  194. this.bare = bare;
  195. return this;
  196. }
  197. /**
  198. * @param remote
  199. * the branch to keep track of in the origin repository
  200. * @return this instance
  201. */
  202. public CloneCommand setRemote(String remote) {
  203. this.remote = remote;
  204. return this;
  205. }
  206. /**
  207. * @param branch
  208. * the initial branch to check out when cloning the repository
  209. * @return this instance
  210. */
  211. public CloneCommand setBranch(String branch) {
  212. this.branch = branch;
  213. return this;
  214. }
  215. /**
  216. * The progress monitor associated with the clone operation. By default,
  217. * this is set to <code>NullProgressMonitor</code>
  218. *
  219. * @see NullProgressMonitor
  220. *
  221. * @param monitor
  222. * @return {@code this}
  223. */
  224. public CloneCommand setProgressMonitor(ProgressMonitor monitor) {
  225. this.monitor = monitor;
  226. return this;
  227. }
  228. /**
  229. * @param credentialsProvider
  230. * the {@link CredentialsProvider} to use
  231. * @return {@code this}
  232. */
  233. public CloneCommand setCredentialsProvider(
  234. CredentialsProvider credentialsProvider) {
  235. this.credentialsProvider = credentialsProvider;
  236. return this;
  237. }
  238. }