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.

Fetch.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.pgm;
  46. import java.io.IOException;
  47. import java.text.MessageFormat;
  48. import java.util.List;
  49. import org.eclipse.jgit.api.FetchCommand;
  50. import org.eclipse.jgit.api.Git;
  51. import org.eclipse.jgit.api.errors.GitAPIException;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.SubmoduleConfig.FetchRecurseSubmodulesMode;
  54. import org.eclipse.jgit.lib.TextProgressMonitor;
  55. import org.eclipse.jgit.pgm.internal.CLIText;
  56. import org.eclipse.jgit.transport.FetchResult;
  57. import org.eclipse.jgit.transport.RefSpec;
  58. import org.eclipse.jgit.transport.TagOpt;
  59. import org.kohsuke.args4j.Argument;
  60. import org.kohsuke.args4j.Option;
  61. @Command(common = true, usage = "usage_updateRemoteRefsFromAnotherRepository")
  62. class Fetch extends AbstractFetchCommand implements FetchCommand.Callback {
  63. @Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
  64. int timeout = -1;
  65. @Option(name = "--fsck", usage = "usage_performFsckStyleChecksOnReceive")
  66. private Boolean fsck;
  67. @Option(name = "--no-fsck")
  68. void nofsck(@SuppressWarnings("unused") final boolean ignored) {
  69. fsck = Boolean.FALSE;
  70. }
  71. @Option(name = "--prune", usage = "usage_pruneStaleTrackingRefs")
  72. private Boolean prune;
  73. @Option(name = "--dry-run")
  74. private boolean dryRun;
  75. @Option(name = "--thin", usage = "usage_fetchThinPack")
  76. private Boolean thin;
  77. @Option(name = "--no-thin")
  78. void nothin(@SuppressWarnings("unused") final boolean ignored) {
  79. thin = Boolean.FALSE;
  80. }
  81. @Option(name = "--quiet", usage = "usage_quiet")
  82. private Boolean quiet;
  83. @Option(name = "--tags", usage="usage_tags", aliases = { "-t" })
  84. private Boolean tags;
  85. @Option(name = "--no-tags", usage = "usage_notags", aliases = { "-n" })
  86. void notags(@SuppressWarnings("unused")
  87. final boolean ignored) {
  88. tags = Boolean.FALSE;
  89. }
  90. @Option(name = "--force", usage = "usage_forcedFetch", aliases = { "-f" })
  91. private Boolean force;
  92. private FetchRecurseSubmodulesMode recurseSubmodules;
  93. @Option(name = "--recurse-submodules", usage = "usage_recurseSubmodules")
  94. void recurseSubmodules(String mode) {
  95. if (mode == null || mode.isEmpty()) {
  96. recurseSubmodules = FetchRecurseSubmodulesMode.YES;
  97. } else {
  98. for (FetchRecurseSubmodulesMode m : FetchRecurseSubmodulesMode
  99. .values()) {
  100. if (m.matchConfigValue(mode)) {
  101. recurseSubmodules = m;
  102. return;
  103. }
  104. }
  105. throw die(MessageFormat
  106. .format(CLIText.get().invalidRecurseSubmodulesMode, mode));
  107. }
  108. }
  109. @Option(name = "--no-recurse-submodules", usage = "usage_noRecurseSubmodules")
  110. void noRecurseSubmodules(@SuppressWarnings("unused")
  111. final boolean ignored) {
  112. recurseSubmodules = FetchRecurseSubmodulesMode.NO;
  113. }
  114. @Argument(index = 0, metaVar = "metaVar_uriish")
  115. private String remote = Constants.DEFAULT_REMOTE_NAME;
  116. @Argument(index = 1, metaVar = "metaVar_refspec")
  117. private List<RefSpec> toget;
  118. /** {@inheritDoc} */
  119. @Override
  120. protected void run() {
  121. try (Git git = new Git(db)) {
  122. FetchCommand fetch = git.fetch();
  123. if (fsck != null) {
  124. fetch.setCheckFetchedObjects(fsck.booleanValue());
  125. }
  126. if (prune != null) {
  127. fetch.setRemoveDeletedRefs(prune.booleanValue());
  128. }
  129. if (toget != null) {
  130. fetch.setRefSpecs(toget);
  131. }
  132. if (tags != null) {
  133. fetch.setTagOpt(tags.booleanValue() ? TagOpt.FETCH_TAGS
  134. : TagOpt.NO_TAGS);
  135. }
  136. if (0 <= timeout) {
  137. fetch.setTimeout(timeout);
  138. }
  139. fetch.setDryRun(dryRun);
  140. fetch.setRemote(remote);
  141. if (thin != null) {
  142. fetch.setThin(thin.booleanValue());
  143. }
  144. if (quiet == null || !quiet.booleanValue()) {
  145. fetch.setProgressMonitor(new TextProgressMonitor(errw));
  146. }
  147. fetch.setRecurseSubmodules(recurseSubmodules).setCallback(this);
  148. if (force != null) {
  149. fetch.setForceUpdate(force.booleanValue());
  150. }
  151. FetchResult result = fetch.call();
  152. if (result.getTrackingRefUpdates().isEmpty()
  153. && result.submoduleResults().isEmpty()) {
  154. return;
  155. }
  156. showFetchResult(result);
  157. } catch (GitAPIException | IOException e) {
  158. throw die(e.getMessage(), e);
  159. }
  160. }
  161. /** {@inheritDoc} */
  162. @Override
  163. public void fetchingSubmodule(String name) {
  164. try {
  165. outw.println(MessageFormat.format(CLIText.get().fetchingSubmodule,
  166. name));
  167. outw.flush();
  168. } catch (IOException e) {
  169. // ignore
  170. }
  171. }
  172. }