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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.pgm;
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.text.MessageFormat;
  14. import java.util.Collection;
  15. import org.eclipse.jgit.api.CloneCommand;
  16. import org.eclipse.jgit.api.Git;
  17. import org.eclipse.jgit.api.errors.InvalidRemoteException;
  18. import org.eclipse.jgit.api.errors.TransportException;
  19. import org.eclipse.jgit.lib.AnyObjectId;
  20. import org.eclipse.jgit.lib.Constants;
  21. import org.eclipse.jgit.lib.TextProgressMonitor;
  22. import org.eclipse.jgit.pgm.internal.CLIText;
  23. import org.eclipse.jgit.transport.URIish;
  24. import org.eclipse.jgit.util.SystemReader;
  25. import org.kohsuke.args4j.Argument;
  26. import org.kohsuke.args4j.Option;
  27. @Command(common = true, usage = "usage_cloneRepositoryIntoNewDir")
  28. class Clone extends AbstractFetchCommand implements CloneCommand.Callback {
  29. @Option(name = "--origin", aliases = { "-o" }, metaVar = "metaVar_remoteName", usage = "usage_useNameInsteadOfOriginToTrackUpstream")
  30. private String remoteName = Constants.DEFAULT_REMOTE_NAME;
  31. @Option(name = "--branch", aliases = { "-b" }, metaVar = "metaVar_branchName", usage = "usage_checkoutBranchAfterClone")
  32. private String branch;
  33. @Option(name = "--no-checkout", aliases = { "-n" }, usage = "usage_noCheckoutAfterClone")
  34. private boolean noCheckout;
  35. @Option(name = "--bare", usage = "usage_bareClone")
  36. private boolean isBare;
  37. @Option(name = "--mirror", usage = "usage_mirrorClone")
  38. private boolean isMirror;
  39. @Option(name = "--quiet", usage = "usage_quiet")
  40. private Boolean quiet;
  41. @Option(name = "--recurse-submodules", usage = "usage_recurseSubmodules")
  42. private boolean cloneSubmodules;
  43. @Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
  44. int timeout = -1;
  45. @Argument(index = 0, required = true, metaVar = "metaVar_uriish")
  46. private String sourceUri;
  47. @Argument(index = 1, metaVar = "metaVar_directory")
  48. private String localName;
  49. /** {@inheritDoc} */
  50. @Override
  51. protected final boolean requiresRepository() {
  52. return false;
  53. }
  54. /** {@inheritDoc} */
  55. @Override
  56. protected void run() throws Exception {
  57. if (localName != null && gitdir != null)
  58. throw die(CLIText.get().conflictingUsageOf_git_dir_andArguments);
  59. final URIish uri = new URIish(sourceUri);
  60. File localNameF;
  61. if (localName == null) {
  62. try {
  63. localName = uri.getHumanishName();
  64. if (isBare || isMirror) {
  65. localName = localName + Constants.DOT_GIT_EXT;
  66. }
  67. localNameF = new File(SystemReader.getInstance().getProperty(
  68. Constants.OS_USER_DIR), localName);
  69. } catch (IllegalArgumentException e) {
  70. throw die(MessageFormat.format(
  71. CLIText.get().cannotGuessLocalNameFrom, sourceUri), e);
  72. }
  73. } else
  74. localNameF = new File(localName);
  75. if (branch == null)
  76. branch = Constants.HEAD;
  77. CloneCommand command = Git.cloneRepository();
  78. command.setURI(sourceUri).setRemote(remoteName).setBare(isBare)
  79. .setMirror(isMirror).setNoCheckout(noCheckout).setBranch(branch)
  80. .setCloneSubmodules(cloneSubmodules).setTimeout(timeout);
  81. command.setGitDir(gitdir == null ? null : new File(gitdir));
  82. command.setDirectory(localNameF);
  83. boolean msgs = quiet == null || !quiet.booleanValue();
  84. if (msgs) {
  85. command.setProgressMonitor(new TextProgressMonitor(errw))
  86. .setCallback(this);
  87. outw.println(MessageFormat.format(
  88. CLIText.get().cloningInto, localName));
  89. outw.flush();
  90. }
  91. try {
  92. db = command.call().getRepository();
  93. if (msgs && db.resolve(Constants.HEAD) == null)
  94. outw.println(CLIText.get().clonedEmptyRepository);
  95. } catch (TransportException e) {
  96. throw die(e.getMessage(), e);
  97. } catch (InvalidRemoteException e) {
  98. throw die(MessageFormat.format(CLIText.get().doesNotExist,
  99. sourceUri), e);
  100. } finally {
  101. if (db != null)
  102. db.close();
  103. }
  104. if (msgs) {
  105. outw.println();
  106. outw.flush();
  107. }
  108. }
  109. /** {@inheritDoc} */
  110. @Override
  111. public void initializedSubmodules(Collection<String> submodules) {
  112. try {
  113. for (String submodule : submodules) {
  114. outw.println(MessageFormat
  115. .format(CLIText.get().submoduleRegistered, submodule));
  116. }
  117. outw.flush();
  118. } catch (IOException e) {
  119. // ignore
  120. }
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public void cloningSubmodule(String path) {
  125. try {
  126. outw.println(MessageFormat.format(
  127. CLIText.get().cloningInto, path));
  128. outw.flush();
  129. } catch (IOException e) {
  130. // ignore
  131. }
  132. }
  133. /** {@inheritDoc} */
  134. @Override
  135. public void checkingOut(AnyObjectId commit, String path) {
  136. try {
  137. outw.println(MessageFormat.format(CLIText.get().checkingOut,
  138. path, commit.getName()));
  139. outw.flush();
  140. } catch (IOException e) {
  141. // ignore
  142. }
  143. }
  144. }