Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Clone.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. @Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
  43. int timeout = -1;
  44. @Argument(index = 0, required = true, metaVar = "metaVar_uriish")
  45. private String sourceUri;
  46. @Argument(index = 1, metaVar = "metaVar_directory")
  47. private String localName;
  48. /** {@inheritDoc} */
  49. @Override
  50. protected final boolean requiresRepository() {
  51. return false;
  52. }
  53. /** {@inheritDoc} */
  54. @Override
  55. protected void run() throws Exception {
  56. if (localName != null && gitdir != null)
  57. throw die(CLIText.get().conflictingUsageOf_git_dir_andArguments);
  58. final URIish uri = new URIish(sourceUri);
  59. File localNameF;
  60. if (localName == null) {
  61. try {
  62. localName = uri.getHumanishName();
  63. if (isBare || isMirror) {
  64. localName = localName + Constants.DOT_GIT_EXT;
  65. }
  66. localNameF = new File(SystemReader.getInstance().getProperty(
  67. Constants.OS_USER_DIR), localName);
  68. } catch (IllegalArgumentException e) {
  69. throw die(MessageFormat.format(
  70. CLIText.get().cannotGuessLocalNameFrom, sourceUri), e);
  71. }
  72. } else
  73. localNameF = new File(localName);
  74. if (branch == null)
  75. branch = Constants.HEAD;
  76. CloneCommand command = Git.cloneRepository();
  77. command.setURI(sourceUri).setRemote(remoteName).setBare(isBare)
  78. .setMirror(isMirror).setNoCheckout(noCheckout).setBranch(branch)
  79. .setCloneSubmodules(cloneSubmodules).setTimeout(timeout);
  80. command.setGitDir(gitdir == null ? null : new File(gitdir));
  81. command.setDirectory(localNameF);
  82. boolean msgs = quiet == null || !quiet.booleanValue();
  83. if (msgs) {
  84. command.setProgressMonitor(new TextProgressMonitor(errw))
  85. .setCallback(this);
  86. outw.println(MessageFormat.format(
  87. CLIText.get().cloningInto, localName));
  88. outw.flush();
  89. }
  90. try {
  91. db = command.call().getRepository();
  92. if (msgs && db.resolve(Constants.HEAD) == null)
  93. outw.println(CLIText.get().clonedEmptyRepository);
  94. } catch (InvalidRemoteException e) {
  95. throw die(MessageFormat.format(CLIText.get().doesNotExist,
  96. sourceUri), e);
  97. } finally {
  98. if (db != null)
  99. db.close();
  100. }
  101. if (msgs) {
  102. outw.println();
  103. outw.flush();
  104. }
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public void initializedSubmodules(Collection<String> submodules) {
  109. try {
  110. for (String submodule : submodules) {
  111. outw.println(MessageFormat
  112. .format(CLIText.get().submoduleRegistered, submodule));
  113. }
  114. outw.flush();
  115. } catch (IOException e) {
  116. // ignore
  117. }
  118. }
  119. /** {@inheritDoc} */
  120. @Override
  121. public void cloningSubmodule(String path) {
  122. try {
  123. outw.println(MessageFormat.format(
  124. CLIText.get().cloningInto, path));
  125. outw.flush();
  126. } catch (IOException e) {
  127. // ignore
  128. }
  129. }
  130. /** {@inheritDoc} */
  131. @Override
  132. public void checkingOut(AnyObjectId commit, String path) {
  133. try {
  134. outw.println(MessageFormat.format(CLIText.get().checkingOut,
  135. path, commit.getName()));
  136. outw.flush();
  137. } catch (IOException e) {
  138. // ignore
  139. }
  140. }
  141. }