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.

Clone.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  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.pgm;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.net.URISyntaxException;
  47. import java.text.MessageFormat;
  48. import java.util.ArrayList;
  49. import java.util.Collections;
  50. import java.util.List;
  51. import org.eclipse.jgit.dircache.DirCache;
  52. import org.eclipse.jgit.dircache.DirCacheCheckout;
  53. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  54. import org.eclipse.jgit.errors.MissingObjectException;
  55. import org.eclipse.jgit.errors.NotSupportedException;
  56. import org.eclipse.jgit.errors.TransportException;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.Ref;
  59. import org.eclipse.jgit.lib.RefComparator;
  60. import org.eclipse.jgit.lib.RefUpdate;
  61. import org.eclipse.jgit.lib.TextProgressMonitor;
  62. import org.eclipse.jgit.revwalk.RevCommit;
  63. import org.eclipse.jgit.revwalk.RevWalk;
  64. import org.eclipse.jgit.storage.file.FileBasedConfig;
  65. import org.eclipse.jgit.storage.file.FileRepository;
  66. import org.eclipse.jgit.transport.FetchResult;
  67. import org.eclipse.jgit.transport.RefSpec;
  68. import org.eclipse.jgit.transport.RemoteConfig;
  69. import org.eclipse.jgit.transport.TagOpt;
  70. import org.eclipse.jgit.transport.Transport;
  71. import org.eclipse.jgit.transport.URIish;
  72. import org.kohsuke.args4j.Argument;
  73. import org.kohsuke.args4j.Option;
  74. @Command(common = true, usage = "usage_cloneRepositoryIntoNewDir")
  75. class Clone extends AbstractFetchCommand {
  76. @Option(name = "--origin", aliases = { "-o" }, metaVar = "metaVar_remoteName", usage = "usage_useNameInsteadOfOriginToTrackUpstream")
  77. private String remoteName = Constants.DEFAULT_REMOTE_NAME;
  78. @Argument(index = 0, required = true, metaVar = "metaVar_uriish")
  79. private String sourceUri;
  80. @Argument(index = 1, metaVar = "metaVar_directory")
  81. private String localName;
  82. private FileRepository dst;
  83. @Override
  84. protected final boolean requiresRepository() {
  85. return false;
  86. }
  87. @Override
  88. protected void run() throws Exception {
  89. if (localName != null && gitdir != null)
  90. throw die(CLIText.get().conflictingUsageOf_git_dir_andArguments);
  91. final URIish uri = new URIish(sourceUri);
  92. if (localName == null) {
  93. try {
  94. localName = uri.getHumanishName();
  95. } catch (IllegalArgumentException e) {
  96. throw die(MessageFormat.format(CLIText.get().cannotGuessLocalNameFrom, sourceUri));
  97. }
  98. }
  99. if (gitdir == null)
  100. gitdir = new File(localName, Constants.DOT_GIT).getAbsolutePath();
  101. dst = new FileRepository(gitdir);
  102. dst.create();
  103. final FileBasedConfig dstcfg = dst.getConfig();
  104. dstcfg.setBoolean("core", null, "bare", false);
  105. dstcfg.save();
  106. db = dst;
  107. out.print(MessageFormat.format(
  108. CLIText.get().initializedEmptyGitRepositoryIn, gitdir));
  109. out.println();
  110. out.flush();
  111. saveRemote(uri);
  112. final FetchResult r = runFetch();
  113. final Ref branch = guessHEAD(r);
  114. doCheckout(branch);
  115. }
  116. private void saveRemote(final URIish uri) throws URISyntaxException,
  117. IOException {
  118. final FileBasedConfig dstcfg = dst.getConfig();
  119. final RemoteConfig rc = new RemoteConfig(dstcfg, remoteName);
  120. rc.addURI(uri);
  121. rc.addFetchRefSpec(new RefSpec().setForceUpdate(true)
  122. .setSourceDestination(Constants.R_HEADS + "*",
  123. Constants.R_REMOTES + remoteName + "/*"));
  124. rc.update(dstcfg);
  125. dstcfg.save();
  126. }
  127. private FetchResult runFetch() throws NotSupportedException,
  128. URISyntaxException, TransportException {
  129. final Transport tn = Transport.open(db, remoteName);
  130. final FetchResult r;
  131. try {
  132. tn.setTagOpt(TagOpt.FETCH_TAGS);
  133. r = tn.fetch(new TextProgressMonitor(), null);
  134. } finally {
  135. tn.close();
  136. }
  137. showFetchResult(r);
  138. return r;
  139. }
  140. private Ref guessHEAD(final FetchResult result) {
  141. final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD);
  142. final List<Ref> availableRefs = new ArrayList<Ref>();
  143. Ref head = null;
  144. for (final Ref r : result.getAdvertisedRefs()) {
  145. final String n = r.getName();
  146. if (!n.startsWith(Constants.R_HEADS))
  147. continue;
  148. availableRefs.add(r);
  149. if (idHEAD == null || head != null)
  150. continue;
  151. if (r.getObjectId().equals(idHEAD.getObjectId()))
  152. head = r;
  153. }
  154. Collections.sort(availableRefs, RefComparator.INSTANCE);
  155. if (idHEAD != null && head == null)
  156. head = idHEAD;
  157. return head;
  158. }
  159. private void doCheckout(final Ref branch) throws IOException {
  160. if (branch == null)
  161. throw die(CLIText.get().cannotChekoutNoHeadsAdvertisedByRemote);
  162. if (!Constants.HEAD.equals(branch.getName())) {
  163. RefUpdate u = db.updateRef(Constants.HEAD);
  164. u.disableRefLog();
  165. u.link(branch.getName());
  166. }
  167. final RevCommit commit = parseCommit(branch);
  168. final RefUpdate u = db.updateRef(Constants.HEAD);
  169. u.setNewObjectId(commit);
  170. u.forceUpdate();
  171. DirCache dc = db.lockDirCache();
  172. DirCacheCheckout co = new DirCacheCheckout(db, dc, commit.getTree());
  173. co.checkout();
  174. }
  175. private RevCommit parseCommit(final Ref branch)
  176. throws MissingObjectException, IncorrectObjectTypeException,
  177. IOException {
  178. final RevWalk rw = new RevWalk(db);
  179. final RevCommit commit;
  180. try {
  181. commit = rw.parseCommit(branch.getObjectId());
  182. } finally {
  183. rw.release();
  184. }
  185. return commit;
  186. }
  187. }