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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.lib.AnyObjectId;
  19. import org.eclipse.jgit.lib.Constants;
  20. import org.eclipse.jgit.lib.TextProgressMonitor;
  21. import org.eclipse.jgit.pgm.internal.CLIText;
  22. import org.eclipse.jgit.transport.URIish;
  23. import org.eclipse.jgit.util.SystemReader;
  24. import org.kohsuke.args4j.Argument;
  25. import org.kohsuke.args4j.Option;
  26. @Command(common = true, usage = "usage_cloneRepositoryIntoNewDir")
  27. class Clone extends AbstractFetchCommand implements CloneCommand.Callback {
  28. @Option(name = "--origin", aliases = { "-o" }, metaVar = "metaVar_remoteName", usage = "usage_useNameInsteadOfOriginToTrackUpstream")
  29. private String remoteName = Constants.DEFAULT_REMOTE_NAME;
  30. @Option(name = "--branch", aliases = { "-b" }, metaVar = "metaVar_branchName", usage = "usage_checkoutBranchAfterClone")
  31. private String branch;
  32. @Option(name = "--no-checkout", aliases = { "-n" }, usage = "usage_noCheckoutAfterClone")
  33. private boolean noCheckout;
  34. @Option(name = "--bare", usage = "usage_bareClone")
  35. private boolean isBare;
  36. @Option(name = "--mirror", usage = "usage_mirrorClone")
  37. private boolean isMirror;
  38. @Option(name = "--quiet", usage = "usage_quiet")
  39. private Boolean quiet;
  40. @Option(name = "--recurse-submodules", usage = "usage_recurseSubmodules")
  41. private boolean cloneSubmodules;
  42. @Argument(index = 0, required = true, metaVar = "metaVar_uriish")
  43. private String sourceUri;
  44. @Argument(index = 1, metaVar = "metaVar_directory")
  45. private String localName;
  46. /** {@inheritDoc} */
  47. @Override
  48. protected final boolean requiresRepository() {
  49. return false;
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. protected void run() throws Exception {
  54. if (localName != null && gitdir != null)
  55. throw die(CLIText.get().conflictingUsageOf_git_dir_andArguments);
  56. final URIish uri = new URIish(sourceUri);
  57. File localNameF;
  58. if (localName == null) {
  59. try {
  60. localName = uri.getHumanishName();
  61. if (isBare || isMirror) {
  62. localName = localName + Constants.DOT_GIT_EXT;
  63. }
  64. localNameF = new File(SystemReader.getInstance().getProperty(
  65. Constants.OS_USER_DIR), localName);
  66. } catch (IllegalArgumentException e) {
  67. throw die(MessageFormat.format(
  68. CLIText.get().cannotGuessLocalNameFrom, sourceUri), e);
  69. }
  70. } else
  71. localNameF = new File(localName);
  72. if (branch == null)
  73. branch = Constants.HEAD;
  74. CloneCommand command = Git.cloneRepository();
  75. command.setURI(sourceUri).setRemote(remoteName).setBare(isBare)
  76. .setMirror(isMirror)
  77. .setNoCheckout(noCheckout).setBranch(branch)
  78. .setCloneSubmodules(cloneSubmodules);
  79. command.setGitDir(gitdir == null ? null : new File(gitdir));
  80. command.setDirectory(localNameF);
  81. boolean msgs = quiet == null || !quiet.booleanValue();
  82. if (msgs) {
  83. command.setProgressMonitor(new TextProgressMonitor(errw))
  84. .setCallback(this);
  85. outw.println(MessageFormat.format(
  86. CLIText.get().cloningInto, localName));
  87. outw.flush();
  88. }
  89. try {
  90. db = command.call().getRepository();
  91. if (msgs && db.resolve(Constants.HEAD) == null)
  92. outw.println(CLIText.get().clonedEmptyRepository);
  93. } catch (InvalidRemoteException e) {
  94. throw die(MessageFormat.format(CLIText.get().doesNotExist,
  95. sourceUri), e);
  96. } finally {
  97. if (db != null)
  98. db.close();
  99. }
  100. if (msgs) {
  101. outw.println();
  102. outw.flush();
  103. }
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public void initializedSubmodules(Collection<String> submodules) {
  108. try {
  109. for (String submodule : submodules) {
  110. outw.println(MessageFormat
  111. .format(CLIText.get().submoduleRegistered, submodule));
  112. }
  113. outw.flush();
  114. } catch (IOException e) {
  115. // ignore
  116. }
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public void cloningSubmodule(String path) {
  121. try {
  122. outw.println(MessageFormat.format(
  123. CLIText.get().cloningInto, path));
  124. outw.flush();
  125. } catch (IOException e) {
  126. // ignore
  127. }
  128. }
  129. /** {@inheritDoc} */
  130. @Override
  131. public void checkingOut(AnyObjectId commit, String path) {
  132. try {
  133. outw.println(MessageFormat.format(CLIText.get().checkingOut,
  134. path, commit.getName()));
  135. outw.flush();
  136. } catch (IOException e) {
  137. // ignore
  138. }
  139. }
  140. }