Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2008-2009, 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.util.ArrayList;
  48. import java.util.Collections;
  49. import java.util.List;
  50. import org.kohsuke.args4j.Argument;
  51. import org.kohsuke.args4j.Option;
  52. import org.eclipse.jgit.errors.NotSupportedException;
  53. import org.eclipse.jgit.errors.TransportException;
  54. import org.eclipse.jgit.lib.Commit;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.GitIndex;
  57. import org.eclipse.jgit.lib.Ref;
  58. import org.eclipse.jgit.lib.RefComparator;
  59. import org.eclipse.jgit.lib.RefUpdate;
  60. import org.eclipse.jgit.lib.Repository;
  61. import org.eclipse.jgit.lib.TextProgressMonitor;
  62. import org.eclipse.jgit.lib.Tree;
  63. import org.eclipse.jgit.lib.WorkDirCheckout;
  64. import org.eclipse.jgit.transport.FetchResult;
  65. import org.eclipse.jgit.transport.RefSpec;
  66. import org.eclipse.jgit.transport.RemoteConfig;
  67. import org.eclipse.jgit.transport.Transport;
  68. import org.eclipse.jgit.transport.URIish;
  69. @Command(common = true, usage = "Clone a repository into a new directory")
  70. class Clone extends AbstractFetchCommand {
  71. @Option(name = "--origin", aliases = { "-o" }, metaVar = "name", usage = "use <name> instead of 'origin' to track upstream")
  72. private String remoteName = "origin";
  73. @Argument(index = 0, required = true, metaVar = "uri-ish")
  74. private String sourceUri;
  75. @Argument(index = 1, metaVar = "directory")
  76. private String localName;
  77. @Override
  78. protected final boolean requiresRepository() {
  79. return false;
  80. }
  81. @Override
  82. protected void run() throws Exception {
  83. if (localName != null && gitdir != null)
  84. throw die("conflicting usage of --git-dir and arguments");
  85. final URIish uri = new URIish(sourceUri);
  86. if (localName == null) {
  87. String p = uri.getPath();
  88. while (p.endsWith("/"))
  89. p = p.substring(0, p.length() - 1);
  90. final int s = p.lastIndexOf('/');
  91. if (s < 0)
  92. throw die("cannot guess local name from " + sourceUri);
  93. localName = p.substring(s + 1);
  94. if (localName.endsWith(".git"))
  95. localName = localName.substring(0, localName.length() - 4);
  96. }
  97. if (gitdir == null)
  98. gitdir = new File(localName, ".git");
  99. db = new Repository(gitdir);
  100. db.create();
  101. db.getConfig().setBoolean("core", null, "bare", false);
  102. db.getConfig().save();
  103. out.println("Initialized empty Git repository in "
  104. + gitdir.getAbsolutePath());
  105. out.flush();
  106. saveRemote(uri);
  107. final FetchResult r = runFetch();
  108. final Ref branch = guessHEAD(r);
  109. doCheckout(branch);
  110. }
  111. private void saveRemote(final URIish uri) throws URISyntaxException,
  112. IOException {
  113. final RemoteConfig rc = new RemoteConfig(db.getConfig(), remoteName);
  114. rc.addURI(uri);
  115. rc.addFetchRefSpec(new RefSpec().setForceUpdate(true)
  116. .setSourceDestination(Constants.R_HEADS + "*",
  117. Constants.R_REMOTES + remoteName + "/*"));
  118. rc.update(db.getConfig());
  119. db.getConfig().save();
  120. }
  121. private FetchResult runFetch() throws NotSupportedException,
  122. URISyntaxException, TransportException {
  123. final Transport tn = Transport.open(db, remoteName);
  124. final FetchResult r;
  125. try {
  126. r = tn.fetch(new TextProgressMonitor(), null);
  127. } finally {
  128. tn.close();
  129. }
  130. showFetchResult(tn, r);
  131. return r;
  132. }
  133. private Ref guessHEAD(final FetchResult result) {
  134. final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD);
  135. final List<Ref> availableRefs = new ArrayList<Ref>();
  136. Ref head = null;
  137. for (final Ref r : result.getAdvertisedRefs()) {
  138. final String n = r.getName();
  139. if (!n.startsWith(Constants.R_HEADS))
  140. continue;
  141. availableRefs.add(r);
  142. if (idHEAD == null || head != null)
  143. continue;
  144. if (r.getObjectId().equals(idHEAD.getObjectId()))
  145. head = r;
  146. }
  147. Collections.sort(availableRefs, RefComparator.INSTANCE);
  148. if (idHEAD != null && head == null)
  149. head = idHEAD;
  150. return head;
  151. }
  152. private void doCheckout(final Ref branch) throws IOException {
  153. if (branch == null)
  154. throw die("cannot checkout; no HEAD advertised by remote");
  155. if (!Constants.HEAD.equals(branch.getName()))
  156. db.writeSymref(Constants.HEAD, branch.getName());
  157. final Commit commit = db.mapCommit(branch.getObjectId());
  158. final RefUpdate u = db.updateRef(Constants.HEAD);
  159. u.setNewObjectId(commit.getCommitId());
  160. u.forceUpdate();
  161. final GitIndex index = new GitIndex(db);
  162. final Tree tree = commit.getTree();
  163. final WorkDirCheckout co;
  164. co = new WorkDirCheckout(db, db.getWorkDir(), index, tree);
  165. co.checkout();
  166. index.write();
  167. }
  168. }