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

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